-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy patheligibilityCheckResource.ts
More file actions
68 lines (59 loc) · 1.89 KB
/
eligibilityCheckResource.ts
File metadata and controls
68 lines (59 loc) · 1.89 KB
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
import { createResource, createEffect, Accessor, createSignal } from "solid-js";
import { createStore } from "solid-js/store";
import type { EligibilityCheck, CreateCheckRequest } from "@/types";
import { addCheck, archiveCheck, fetchUserDefinedChecks } from "@/api/check";
export interface EligibilityCheckResource {
checks: () => EligibilityCheck[];
actions: {
addNewCheck: (check: CreateCheckRequest) => Promise<void>;
removeCheck: (checkIdToRemove: string) => Promise<void>;
};
actionInProgress: Accessor<boolean>;
initialLoadStatus: {
loading: Accessor<boolean>;
error: Accessor<unknown>;
};
}
const eligibilityCheckResource = (): EligibilityCheckResource => {
const [checksResource, { refetch }] = createResource(fetchUserDefinedChecks);
const [actionInProgress, setActionInProgress] = createSignal<boolean>(false);
// Local fine-grained store
const [checks, setChecks] = createStore<EligibilityCheck[]>([]);
// When resource resolves, sync it into the store
createEffect(() => {
if (checksResource()) {
setChecks(checksResource()!);
}
});
// Actions
const addNewCheck = async (check: CreateCheckRequest) => {
setActionInProgress(true);
try {
await addCheck(check);
await refetch();
} catch (e) {
console.error("Failed to add new check", e);
}
setActionInProgress(false);
};
const removeCheck = async (checkIdToRemove: string) => {
setActionInProgress(true);
try {
await archiveCheck(checkIdToRemove);
await refetch();
} catch (e) {
console.error("Failed to archive check", e);
}
setActionInProgress(false);
};
return {
checks: () => checks,
actions: { addNewCheck, removeCheck },
actionInProgress,
initialLoadStatus: {
loading: () => checksResource.loading,
error: () => checksResource.error,
},
};
};
export default eligibilityCheckResource;