-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
ProjectActions.ts
92 lines (77 loc) · 2.92 KB
/
ProjectActions.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
import { type Action, type PayloadAction } from "@reduxjs/toolkit";
import { type Project, type Speaker, type User } from "api/models";
import {
getAllProjectUsers,
getAllSemanticDomainNames,
updateProject,
} from "backend";
import { setProjectId } from "backend/localStorage";
import {
resetAction,
setProjectAction,
setSemanticDomainsAction,
setSpeakerAction,
setUsersAction,
} from "components/Project/ProjectReducer";
import i18n from "i18n";
import { type StoreState } from "types";
import { type StoreStateDispatch } from "types/Redux/actions";
import { type Hash } from "types/hash";
import { newProject } from "types/project";
// Action Creation Functions
export function resetCurrentProject(): Action {
return resetAction();
}
export function setCurrentProject(project?: Project): PayloadAction {
return setProjectAction(project ?? newProject());
}
export function setCurrentSemDoms(semDoms?: Hash<string>): PayloadAction {
return setSemanticDomainsAction(semDoms);
}
export function setCurrentSpeaker(speaker?: Speaker): PayloadAction {
return setSpeakerAction(speaker);
}
export function setCurrentUsers(users?: User[]): PayloadAction {
return setUsersAction(users ?? []);
}
// Dispatch Functions
/** If the project's sem dom language is set to (Default to user interface language) and
* the UI language changes, the user must leave the project and re-enter for that change
* to take effect on the semantic domains (a page refresh is not sufficient). */
export function asyncLoadSemanticDomains(lang?: string) {
return async (dispatch: StoreStateDispatch, getState: () => StoreState) => {
const langParts = (lang || i18n.language).split("-");
const langRoot = langParts.length ? langParts[0] : "";
const { project, semanticDomains } = getState().currentProjectState;
if (!semanticDomains || project.semDomWritingSystem.bcp47 !== langRoot) {
dispatch(setCurrentSemDoms(await getAllSemanticDomainNames(langRoot)));
}
};
}
export function asyncRefreshProjectUsers(projectId: string) {
return async (dispatch: StoreStateDispatch) => {
dispatch(setCurrentUsers(await getAllProjectUsers(projectId)));
};
}
export function asyncSetNewCurrentProject(proj?: Project) {
return async (dispatch: StoreStateDispatch) => {
// Update semantic domains before setting the project.
await dispatch(asyncLoadSemanticDomains(proj?.semDomWritingSystem.bcp47));
setProjectId(proj?.id);
dispatch(setCurrentProject(proj));
};
}
export function asyncUpdateCurrentProject(proj: Project) {
return async (dispatch: StoreStateDispatch) => {
// Update semantic domains before setting the project.
await dispatch(asyncLoadSemanticDomains(proj.semDomWritingSystem.bcp47));
await updateProject(proj);
dispatch(setCurrentProject(proj));
};
}
export function clearCurrentProject() {
return (dispatch: StoreStateDispatch) => {
setProjectId();
dispatch(resetCurrentProject());
};
}