import { get , post , put , patch , httpDelete } from '../utils' ; import { format } from 'date-fns' ; import { Board , TaskGroup , Task } from '../types' ; /**
 * NOTICE
 *
 * Some of the work here would realistically be done server side
 * but we don't have a real server, we have a tool called json-server
 * which allows us to play with a JSON file as if it were a restful API
 */ /**
 * Boards
 */ export function createBoard ( name ? ) { const defaultBoardData = { name : name || '' , } ; return post < Board > ( `/boards` , defaultBoardData ) ; } export function getBoards ( ) { return get < Board [ ] > ( `/boards` ) ; } export function getBoard ( boardId ) { return get < Board & { taskGroups : TaskGroup [ ] ; tasks : Task [ ] ; } > ( `/boards/${ boardId }?_embed=taskGroups&_embed=tasks` ) ; } export async function updateBoard ( boardId , data ) { return patch < any > ( `/boards/${ boardId }` , data ) ; } export function removeBoard ( boardId ) { return httpDelete < any > ( `/boards/${ boardId }` ) ; } /**
 * Task Groups
 */ export function createTaskGroup ( boardId , name ? ) { const defaultTaskGroupData = { name : name || '' , boardId , taskIds : [ ] as number [ ] , } ; return post < TaskGroup > ( `/taskGroups` , defaultTaskGroupData ) ; } export function getTaskGroups ( boardId ) { return get < TaskGroup [ ] > ( `/taskGroups?boardId=${ boardId }` ) ; } export function updateTaskGroup ( taskGroupId , data ) { return patch < any > ( `/taskGroups/${ taskGroupId }` , data ) ; } export function updateTaskGroups ( taskGroups ) { const p = [ ] ; taskGroups . forEach ( ( group ) => { p . push ( put < any > ( `/taskGroups/${ group . id }` , group ) ) ; } ) ; return Promise . all ( p ) ; } export async function removeTaskGroup ( taskGroupId ) { const taskGroup = await get ( `/taskGroups/${ taskGroupId }` ) ; await httpDelete < any > ( `/taskGroups/${ taskGroupId }` ) ; const p = [ ] ; taskGroup . taskIds . forEach ( ( taskId ) => { p . push ( httpDelete < any > ( `/tasks/${ taskId }` ) ) ; } ) ; await Promise . all ( p ) ; } /**
 * Tasks
 */ export function getTasks ( boardId ) { return get < Task [ ] > ( `/tasks?boardId=${ boardId }` ) ; } export function getTask ( taskId ) { return get < Task > ( `/tasks/${ taskId }` ) ; } export async function createTask ( boardId , taskGroupId , data ? ) { const defaultTaskData = { name : '' , content : '' , minutes : 0 , completedMinutes : 0 , assignedTo : [ ] , ... data , boardId , date : format ( Date . now ( ) , 'yyyy-MM-dd' ) , } ; const task = await post < Task > ( `/tasks` , defaultTaskData ) ; const taskGroup = await get < TaskGroup > ( `/taskGroups/${ taskGroupId }` ) ; const newTaskIds = taskGroup . taskIds . concat ( task . id ) ; await patch < any > ( `/taskGroups/${ taskGroupId }` , { taskIds : newTaskIds } ) ; return task ; } export function updateTask ( taskId , task ) { return put ( `/tasks/${ taskId }` , task ) ; } export async function removeTask ( boardId , taskId ) { await httpDelete ( `/tasks/${ taskId }` ) ; const taskGroups = await get ( `/taskGroups/?boardId=${ boardId }` ) ; const newTaskGroups = taskGroups . map ( ( taskGroup ) => { return { ... taskGroup , taskIds : taskGroup . taskIds . filter ( ( id ) => id !== taskId ) } ; } ) ; await updateTaskGroups ( newTaskGroups ) ; return null ; }