2016-12-03 23:28:43 +01:00
|
|
|
import request from 'request'
|
|
|
|
import style from './style.js'
|
|
|
|
|
|
|
|
export class ApiStyleStore {
|
2016-12-16 14:49:25 +01:00
|
|
|
supported(cb) {
|
|
|
|
request('http://localhost:8000/styles', (error, response, body) => {
|
|
|
|
cb(error === undefined)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-12-03 23:28:43 +01:00
|
|
|
latestStyle(cb) {
|
|
|
|
if(this.latestStyleId) {
|
|
|
|
request('http://localhost:8000/styles/' + this.latestStyleId, (error, response, body) => {
|
|
|
|
cb(JSON.parse(body))
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
request('http://localhost:8000/styles', (error, response, body) => {
|
|
|
|
if (!error && response.statusCode == 200) {
|
|
|
|
const styleIds = JSON.parse(body);
|
|
|
|
this.latestStyleId = styleIds[0];
|
|
|
|
request('http://localhost:8000/styles/' + this.latestStyleId, (error, response, body) => {
|
|
|
|
cb(style.fromJSON(JSON.parse(body)))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save current style replacing previous version
|
|
|
|
save(mapStyle) {
|
|
|
|
const id = mapStyle.get('id')
|
|
|
|
request.put({
|
|
|
|
url: 'http://localhost:8000/styles/' + id,
|
|
|
|
json: true,
|
|
|
|
body: style.toJSON(mapStyle)
|
|
|
|
}, (error, response, body) => {
|
|
|
|
console.log('Saved style');
|
|
|
|
})
|
|
|
|
return mapStyle
|
|
|
|
}
|
|
|
|
}
|