2017-04-13 23:24:31 +02:00
|
|
|
import moment from 'moment'
|
|
|
|
|
|
|
|
require('es6-promise').polyfill()
|
|
|
|
require('isomorphic-fetch')
|
|
|
|
|
2017-05-02 12:18:11 +02:00
|
|
|
const postProcessAPIResults = function (flat) {
|
|
|
|
/* eslint-disable camelcase */
|
|
|
|
if (flat.date) {
|
2017-11-09 17:33:39 +01:00
|
|
|
flat.date = moment.utc(flat.date)
|
|
|
|
}
|
|
|
|
if (flat.visit_date) {
|
|
|
|
flat.visit_date = moment.utc(flat.visit_date)
|
2017-05-02 12:18:11 +02:00
|
|
|
}
|
|
|
|
if (flat.flatisfy_time_to) {
|
|
|
|
const momentifiedTimeTo = {}
|
|
|
|
Object.keys(flat.flatisfy_time_to).forEach(key => {
|
|
|
|
const value = flat.flatisfy_time_to[key]
|
|
|
|
momentifiedTimeTo[key] = Object.assign(
|
|
|
|
{},
|
|
|
|
value,
|
|
|
|
{ time: moment.duration(value.time, 'seconds') }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
flat.flatisfy_time_to = momentifiedTimeTo
|
|
|
|
}
|
2017-06-13 17:21:02 +02:00
|
|
|
|
|
|
|
// Fill cost per square meter.
|
|
|
|
flat.sqCost = Math.round(flat.cost * 100 / flat.area) / 100 | 0
|
|
|
|
|
2017-05-02 12:18:11 +02:00
|
|
|
/* eslint-enable camelcase */
|
|
|
|
return flat
|
|
|
|
}
|
|
|
|
|
2017-04-13 23:24:31 +02:00
|
|
|
export const getFlats = function (callback) {
|
2017-04-26 20:02:55 +02:00
|
|
|
fetch('/api/v1/flats', { credentials: 'same-origin' })
|
2017-04-13 23:24:31 +02:00
|
|
|
.then(function (response) {
|
|
|
|
return response.json()
|
|
|
|
}).then(function (json) {
|
|
|
|
const flats = json.data
|
2017-05-02 12:18:11 +02:00
|
|
|
flats.map(postProcessAPIResults)
|
2017-04-13 23:24:31 +02:00
|
|
|
callback(flats)
|
|
|
|
}).catch(function (ex) {
|
|
|
|
console.error('Unable to parse flats: ' + ex)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getFlat = function (flatId, callback) {
|
2017-04-26 20:02:55 +02:00
|
|
|
fetch(
|
2017-12-05 12:20:40 +01:00
|
|
|
'/api/v1/flats/' + encodeURIComponent(flatId),
|
2017-04-26 20:02:55 +02:00
|
|
|
{ credentials: 'same-origin' }
|
|
|
|
)
|
2017-04-13 23:24:31 +02:00
|
|
|
.then(function (response) {
|
|
|
|
return response.json()
|
|
|
|
}).then(function (json) {
|
2017-05-02 12:18:11 +02:00
|
|
|
const flat = postProcessAPIResults(json.data)
|
|
|
|
callback(flat)
|
2017-04-13 23:24:31 +02:00
|
|
|
}).catch(function (ex) {
|
|
|
|
console.error('Unable to parse flats: ' + ex)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export const updateFlatStatus = function (flatId, newStatus, callback) {
|
|
|
|
fetch(
|
2017-12-05 12:20:40 +01:00
|
|
|
'/api/v1/flats/' + encodeURIComponent(flatId),
|
2017-04-13 23:24:31 +02:00
|
|
|
{
|
2017-04-26 20:02:55 +02:00
|
|
|
credentials: 'same-origin',
|
2017-12-05 12:20:40 +01:00
|
|
|
method: 'PATCH',
|
2017-04-13 23:24:31 +02:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
status: newStatus
|
|
|
|
})
|
|
|
|
}
|
2017-05-02 18:35:34 +02:00
|
|
|
).then(callback).catch(function (ex) {
|
|
|
|
console.error('Unable to update flat status: ' + ex)
|
|
|
|
})
|
2017-05-03 19:17:19 +02:00
|
|
|
}
|
2017-05-02 18:35:34 +02:00
|
|
|
|
2017-05-03 19:17:19 +02:00
|
|
|
export const updateFlatNotes = function (flatId, newNotes, callback) {
|
|
|
|
fetch(
|
2017-12-05 12:20:40 +01:00
|
|
|
'/api/v1/flats/' + encodeURIComponent(flatId),
|
2017-05-03 19:17:19 +02:00
|
|
|
{
|
|
|
|
credentials: 'same-origin',
|
2017-12-05 12:20:40 +01:00
|
|
|
method: 'PATCH',
|
2017-05-03 19:17:19 +02:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
notes: newNotes
|
|
|
|
})
|
|
|
|
}
|
|
|
|
).then(callback).catch(function (ex) {
|
|
|
|
console.error('Unable to update flat notes: ' + ex)
|
|
|
|
})
|
2017-04-13 23:24:31 +02:00
|
|
|
}
|
|
|
|
|
2017-05-03 22:10:24 +02:00
|
|
|
export const updateFlatNotation = function (flatId, newNotation, callback) {
|
|
|
|
fetch(
|
2017-12-05 12:20:40 +01:00
|
|
|
'/api/v1/flats/' + encodeURIComponent(flatId),
|
2017-05-03 22:10:24 +02:00
|
|
|
{
|
|
|
|
credentials: 'same-origin',
|
2017-12-05 12:20:40 +01:00
|
|
|
method: 'PATCH',
|
2017-05-03 22:10:24 +02:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
notation: newNotation
|
|
|
|
})
|
|
|
|
}
|
|
|
|
).then(callback).catch(function (ex) {
|
|
|
|
console.error('Unable to update flat notation: ' + ex)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-11-09 17:33:39 +01:00
|
|
|
export const updateFlatVisitDate = function (flatId, newVisitDate, callback) {
|
|
|
|
fetch(
|
2017-12-05 12:20:40 +01:00
|
|
|
'/api/v1/flats/' + encodeURIComponent(flatId),
|
2017-11-09 17:33:39 +01:00
|
|
|
{
|
|
|
|
credentials: 'same-origin',
|
2017-12-05 12:20:40 +01:00
|
|
|
method: 'PATCH',
|
2017-11-09 17:33:39 +01:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
2017-11-27 10:42:22 +01:00
|
|
|
visit_date: newVisitDate // eslint-disable-line camelcase
|
2017-11-09 17:33:39 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
).then(callback).catch(function (ex) {
|
|
|
|
console.error('Unable to update flat date of visit: ' + ex)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-04-13 23:24:31 +02:00
|
|
|
export const getTimeToPlaces = function (callback) {
|
2017-06-16 16:56:59 +02:00
|
|
|
fetch('/api/v1/time_to_places', { credentials: 'same-origin' })
|
2017-04-13 23:24:31 +02:00
|
|
|
.then(function (response) {
|
|
|
|
return response.json()
|
|
|
|
}).then(function (json) {
|
|
|
|
callback(json.data)
|
|
|
|
}).catch(function (ex) {
|
|
|
|
console.error('Unable to fetch time to places: ' + ex)
|
|
|
|
})
|
|
|
|
}
|
2017-05-02 18:35:34 +02:00
|
|
|
|
|
|
|
export const doSearch = function (query, callback) {
|
|
|
|
fetch(
|
|
|
|
'/api/v1/search',
|
|
|
|
{
|
|
|
|
credentials: 'same-origin',
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
query: query
|
|
|
|
})
|
|
|
|
}
|
|
|
|
).then(response => response.json()).then(json => {
|
|
|
|
callback(json.data)
|
|
|
|
}).catch(function (ex) {
|
|
|
|
console.error('Unable to perform search: ' + ex)
|
|
|
|
})
|
|
|
|
}
|
2019-01-13 20:22:57 +01:00
|
|
|
|
|
|
|
export const getMetadata = function (callback) {
|
|
|
|
fetch('/api/v1/metadata', { credentials: 'same-origin' })
|
|
|
|
.then(response => response.json())
|
|
|
|
.then(json => callback(json.data))
|
|
|
|
.catch(ex => console.error('Unable to fetch application metadata: ' + ex))
|
|
|
|
}
|