-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.ts
90 lines (71 loc) · 2.6 KB
/
actions.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
import { createAsyncThunk } from "@reduxjs/toolkit";
import { NotificationMessage } from "~/libs/enums/enums.js";
import {
type AsyncThunkConfig,
type PaginationQueryParameters,
} from "~/libs/types/types.js";
import { actions as authActions } from "~/modules/auth/auth.js";
import {
type UserGetAllQueryParameters,
type UserGetAllResponseDto,
} from "~/modules/users/users.js";
import {
type ProjectGroupCreateRequestDto,
type ProjectGroupGetAllItemResponseDto,
type ProjectGroupGetAllResponseDto,
type ProjectGroupPatchRequestDto,
} from "../libs/types/types.js";
import { name as sliceName } from "./project-group.slice.js";
const loadUsers = createAsyncThunk<
UserGetAllResponseDto,
UserGetAllQueryParameters,
AsyncThunkConfig
>(`${sliceName}/load-users`, (query, { extra }) => {
const { userApi } = extra;
return userApi.getAll(query);
});
const loadAllByProjectId = createAsyncThunk<
ProjectGroupGetAllResponseDto,
{ projectId: string; query: PaginationQueryParameters },
AsyncThunkConfig
>(`${sliceName}/load-all`, async ({ projectId, query }, { extra }) => {
const { projectGroupApi } = extra;
return await projectGroupApi.getAllByProjectId(projectId, query);
});
const deleteById = createAsyncThunk<boolean, { id: number }, AsyncThunkConfig>(
`${sliceName}/delete-by-id`,
async ({ id }, { dispatch, extra }) => {
const { projectGroupApi, toastNotifier } = extra;
const isDeleted = await projectGroupApi.deleteById(id);
if (isDeleted) {
toastNotifier.showSuccess(
NotificationMessage.PROJECT_GROUP_DELETE_SUCCESS,
);
void dispatch(authActions.getAuthenticatedUser());
}
return isDeleted;
},
);
const create = createAsyncThunk<
ProjectGroupGetAllItemResponseDto,
ProjectGroupCreateRequestDto,
AsyncThunkConfig
>(`${sliceName}/create`, async (payload, { dispatch, extra }) => {
const { projectGroupApi, toastNotifier } = extra;
const response = await projectGroupApi.create(payload);
toastNotifier.showSuccess(NotificationMessage.PROJECT_GROUP_CREATE_SUCCESS);
void dispatch(authActions.getAuthenticatedUser());
return response;
});
const patch = createAsyncThunk<
ProjectGroupGetAllItemResponseDto,
{ id: number; payload: ProjectGroupPatchRequestDto },
AsyncThunkConfig
>(`${sliceName}/update`, async ({ id, payload }, { dispatch, extra }) => {
const { projectGroupApi, toastNotifier } = extra;
const response = await projectGroupApi.patch(id, payload);
toastNotifier.showSuccess(NotificationMessage.PROJECT_GROUP_UPDATE_SUCCESS);
void dispatch(authActions.getAuthenticatedUser());
return response;
});
export { create, deleteById, loadAllByProjectId, loadUsers, patch };