This repository has been archived by the owner on Jan 20, 2024. It is now read-only.
/
tasks.ts
114 lines (108 loc) · 3.33 KB
/
tasks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import req from "../axios";
import { AxiosResponse } from "axios";
import { Dispatch } from "redux";
import Actions from "./list";
import { TaskAction } from "../typings/actions";
import { TaskRecord, Task } from "../typings/tasks";
import {
taskFromRecord,
taskToRecord,
mergeTasks,
TaskMergeResult,
} from "../helpers/tasks";
/**
* Loads the merge results into Redux store. Calls the corresponding action
* creators for tasks to be deleted/updated/created remotely.
* @param merge The result of `mergeTasks` on remote and local tasks
* @param dispatch Dispatching function passed by the action creator
*/
const resolveUpdates = (merge: TaskMergeResult, dispatch: Dispatch) => {
dispatch({
type: Actions.TasksFetch,
data: merge.result,
} as TaskAction);
merge.toDelete.forEach((task) => deleteTask(task)(dispatch));
merge.toSync.forEach((task) => createTask(task)(dispatch));
merge.toUpdate.forEach((task) => updateTask(task)(dispatch));
};
/**
* Redux action creator. Fetches the task list from the server and
* deserializes task data from JSON. Loads the just built task list into
* the store. Sends the corresponding update requests for the tasks to be
* deleted, updated, etc.
* @param localTasks The local task list
*/
export const refetchTasks = (localTasks: Task[]) => (dispatch: Dispatch) => {
req.get("/task").then(
(res: AxiosResponse) => {
const rm = (res.data as TaskRecord[]).map((x) => taskFromRecord(x));
const merge = mergeTasks(rm, localTasks);
resolveUpdates(merge, dispatch);
},
() => {
dispatch({
type: Actions.TasksFetchError,
} as TaskAction);
}
);
};
/**
* Redux action creator. Sends a task creation request to the server,
* fetches the ID it receives, and updates the task with that ID.
*/
export const createTask = (task: Task) => (dispatch: Dispatch) => {
req.post("/task", taskToRecord(task)).then(
(res: AxiosResponse) => {
task.ID = res.data;
dispatch({
type: Actions.TaskCreate,
data: task,
} as TaskAction);
},
() => {
dispatch({
type: Actions.TaskCreateError,
data: task,
} as TaskAction);
}
);
};
/**
* Redux action creator. Sends a PATCH request to the server with the
* new task details.
*/
export const updateTask = (task: Task) => (dispatch: Dispatch) => {
req.patch(`/task/${task.ID}`, taskToRecord(task)).then(
() => {
dispatch({
type: Actions.TaskUpdate,
data: task,
} as TaskAction);
},
() => {
dispatch({
type: Actions.TaskUpdateError,
data: task,
} as TaskAction);
}
);
};
/**
* Redux action creator. Sends a DELETE request to the server.
*/
export const deleteTask = (task: Task) => (dispatch: Dispatch) => {
req.delete(`/task/${task.ID}`).then(
() => {
dispatch({
type: Actions.TaskDelete,
data: task,
} as TaskAction);
},
() => {
dispatch({
type: Actions.TaskDeleteError,
data: task,
});
}
);
};