2019-03-02 11:25:10 +01:00
|
|
|
import AbstractService from './abstractService'
|
|
|
|
import ListModel from '../models/list'
|
|
|
|
import TaskService from './task'
|
2020-03-02 21:55:22 +01:00
|
|
|
import {formatISO} from 'date-fns'
|
2021-01-17 11:51:07 +01:00
|
|
|
import {colorFromHex} from '@/helpers/color/colorFromHex'
|
2019-03-02 11:25:10 +01:00
|
|
|
|
|
|
|
export default class ListService extends AbstractService {
|
|
|
|
constructor() {
|
|
|
|
super({
|
2020-04-17 12:19:53 +02:00
|
|
|
create: '/namespaces/{namespaceId}/lists',
|
2019-03-02 11:25:10 +01:00
|
|
|
get: '/lists/{id}',
|
2020-04-18 14:39:56 +02:00
|
|
|
getAll: '/lists',
|
2019-03-02 11:25:10 +01:00
|
|
|
update: '/lists/{id}',
|
|
|
|
delete: '/lists/{id}',
|
|
|
|
})
|
|
|
|
}
|
2020-02-08 14:16:06 +01:00
|
|
|
|
|
|
|
processModel(model) {
|
2020-09-04 22:01:02 +02:00
|
|
|
model.created = formatISO(new Date(model.created))
|
|
|
|
model.updated = formatISO(new Date(model.updated))
|
2020-02-08 14:16:06 +01:00
|
|
|
return model
|
|
|
|
}
|
|
|
|
|
2019-03-02 11:25:10 +01:00
|
|
|
modelFactory(data) {
|
|
|
|
return new ListModel(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeUpdate(model) {
|
2022-02-15 13:07:34 +01:00
|
|
|
const taskService = new TaskService()
|
2019-03-02 11:25:10 +01:00
|
|
|
model.tasks = model.tasks.map(task => {
|
|
|
|
return taskService.beforeUpdate(task)
|
|
|
|
})
|
2021-01-17 11:51:07 +01:00
|
|
|
model.hexColor = colorFromHex(model.hexColor)
|
2019-03-02 11:25:10 +01:00
|
|
|
return model
|
|
|
|
}
|
2020-03-25 22:27:29 +01:00
|
|
|
|
|
|
|
beforeCreate(list) {
|
2021-01-17 11:51:07 +01:00
|
|
|
list.hexColor = colorFromHex(list.hexColor)
|
2020-03-25 22:27:29 +01:00
|
|
|
return list
|
|
|
|
}
|
2020-05-31 21:17:10 +02:00
|
|
|
|
2021-08-23 21:24:52 +02:00
|
|
|
update(model) {
|
|
|
|
const newModel = { ... model }
|
|
|
|
return super.update(newModel)
|
|
|
|
}
|
|
|
|
|
2021-10-11 19:37:20 +02:00
|
|
|
async background(list) {
|
2020-05-31 21:17:10 +02:00
|
|
|
if (list.background === null) {
|
2021-10-09 16:34:57 +02:00
|
|
|
return ''
|
2020-05-31 21:17:10 +02:00
|
|
|
}
|
|
|
|
|
2021-10-11 19:37:20 +02:00
|
|
|
const response = await this.http({
|
2020-05-31 21:17:10 +02:00
|
|
|
url: `/lists/${list.id}/background`,
|
|
|
|
method: 'GET',
|
|
|
|
responseType: 'blob',
|
|
|
|
})
|
2021-10-11 19:37:20 +02:00
|
|
|
return window.URL.createObjectURL(new Blob([response.data]))
|
2020-05-31 21:17:10 +02:00
|
|
|
}
|
2021-03-21 18:11:24 +01:00
|
|
|
|
2021-10-11 19:37:20 +02:00
|
|
|
async removeBackground(list) {
|
2021-03-21 18:11:24 +01:00
|
|
|
const cancel = this.setLoading()
|
|
|
|
|
2021-10-11 19:37:20 +02:00
|
|
|
try {
|
|
|
|
const response = await this.http.delete(`/lists/${list.id}/background`, list)
|
|
|
|
return response.data
|
|
|
|
} finally {
|
|
|
|
cancel()
|
|
|
|
}
|
2021-03-21 18:11:24 +01:00
|
|
|
}
|
2019-03-02 11:25:10 +01:00
|
|
|
}
|