Files
planka/client/src/api/cards.js

88 lines
1.9 KiB
JavaScript
Raw Normal View History

2019-08-31 04:07:25 +05:00
import socket from './socket';
import { transformAttachment } from './attachments';
2019-08-31 04:07:25 +05:00
/* Transformers */
2020-03-25 00:15:47 +05:00
export const transformCard = (card) => ({
2019-08-31 04:07:25 +05:00
...card,
...(card.dueDate && {
dueDate: new Date(card.dueDate),
}),
...(card.stopwatch && {
stopwatch: {
...card.stopwatch,
...(card.stopwatch.startedAt && {
startedAt: new Date(card.stopwatch.startedAt),
}),
},
}),
2019-08-31 04:07:25 +05:00
});
2020-03-25 00:15:47 +05:00
export const transformCardData = (data) => ({
2019-08-31 04:07:25 +05:00
...data,
...(data.dueDate && {
dueDate: data.dueDate.toISOString(),
2019-08-31 04:07:25 +05:00
}),
...(data.stopwatch && {
stopwatch: {
...data.stopwatch,
...(data.stopwatch.startedAt && {
startedAt: data.stopwatch.startedAt.toISOString(),
}),
},
2019-08-31 04:07:25 +05:00
}),
});
/* Actions */
2022-12-26 21:10:50 +01:00
const createCard = (listId, data, headers) =>
socket.post(`/lists/${listId}/cards`, transformCardData(data), headers).then((body) => ({
...body,
item: transformCard(body.item),
}));
2019-08-31 04:07:25 +05:00
const getCard = (id, headers) =>
socket.get(`/cards/${id}`, undefined, headers).then((body) => ({
...body,
item: transformCard(body.item),
2022-12-26 21:10:50 +01:00
included: {
...body.included,
attachments: body.included.attachments.map(transformAttachment),
},
}));
2019-08-31 04:07:25 +05:00
const updateCard = (id, data, headers) =>
socket.patch(`/cards/${id}`, transformCardData(data), headers).then((body) => ({
...body,
item: transformCard(body.item),
}));
2019-08-31 04:07:25 +05:00
const deleteCard = (id, headers) =>
socket.delete(`/cards/${id}`, undefined, headers).then((body) => ({
...body,
item: transformCard(body.item),
}));
2019-08-31 04:07:25 +05:00
/* Event handlers */
2020-03-25 00:15:47 +05:00
const makeHandleCardCreate = (next) => (body) => {
2019-08-31 04:07:25 +05:00
next({
...body,
item: transformCard(body.item),
});
};
2022-08-04 13:31:14 +02:00
const makeHandleCardUpdate = makeHandleCardCreate;
2019-08-31 04:07:25 +05:00
2022-08-04 13:31:14 +02:00
const makeHandleCardDelete = makeHandleCardCreate;
2019-08-31 04:07:25 +05:00
export default {
createCard,
getCard,
updateCard,
deleteCard,
makeHandleCardCreate,
makeHandleCardUpdate,
makeHandleCardDelete,
};