forked from os2display/display-api-service
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivation-code-list.jsx
More file actions
215 lines (193 loc) · 5.56 KB
/
Copy pathactivation-code-list.jsx
File metadata and controls
215 lines (193 loc) · 5.56 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { useEffect, useState, useContext } from "react";
import { useTranslation } from "react-i18next";
import { useDispatch } from "react-redux";
import { Button } from "react-bootstrap";
import List from "../util/list/list";
import ListContext from "../../context/list-context";
import UserContext from "../../context/user-context";
import useModal from "../../context/modal-context/modal-context-hook";
import { ActivationCodeColumns } from "./activation-code-columns";
import ContentHeader from "../util/content-header/content-header";
import ContentBody from "../util/content-body/content-body";
import idFromUrl from "../util/helpers/id-from-url";
import {
displaySuccess,
displayError,
} from "../util/list/toast-component/display-toast";
import {
enhancedApi,
useDeleteV2UserActivationCodesByIdMutation,
useGetV2UserActivationCodesQuery,
} from "../../redux/enhanced-api.ts";
/**
* The Activation Code list component.
*
* @returns {object} The users list.
*/
function ActivationCodeList() {
const { t } = useTranslation("common", { keyPrefix: "activation-code-list" });
const { selected, setSelected } = useModal();
const {
searchText: { get: searchText },
page: { get: page },
createdBy: { get: createdBy },
} = useContext(ListContext);
const context = useContext(UserContext);
// Local state
const [items, setItems] = useState([]);
const [isDeleting, setIsDeleting] = useState(false);
const [listData, setListData] = useState();
const [loadingMessage, setLoadingMessage] = useState(
t("loading-messages.loading-activation-code"),
);
// Remove from tenant call
const [
DeleteV2UserActivationCode,
{ isSuccess: isDeleteSuccess, error: isDeleteError },
] = useDeleteV2UserActivationCodesByIdMutation();
// Get method
const {
data,
error: activationCodeGetError,
isLoading,
refetch,
} = useGetV2UserActivationCodesQuery({
page,
order: { createdAt: "desc" },
title: searchText,
createdBy,
});
useEffect(() => {
if (data) {
setListData(data);
}
}, [data]);
useEffect(() => {
refetch();
}, [searchText, page, createdBy]);
/** Deletes multiple codes. */
useEffect(() => {
if (isDeleting && selected.length > 0) {
const codeToDelete = selected[0];
setSelected(selected.slice(1));
const codeToDeleteId = idFromUrl(codeToDelete.id);
DeleteV2UserActivationCode({ id: codeToDeleteId });
}
}, [isDeleting, isDeleteSuccess]);
// Sets success messages in local storage, because the page is reloaded
useEffect(() => {
if (isDeleteSuccess && selected.length === 0) {
displaySuccess(t("success-messages.activation-code-delete"));
refetch();
setIsDeleting(false);
}
}, [isDeleteSuccess]);
// If the tenant is changed, data should be refetched
useEffect(() => {
if (context.selectedTenant.get) {
refetch();
}
}, [context.selectedTenant.get]);
// Display error on unsuccessful deletion
useEffect(() => {
if (isDeleteError) {
setIsDeleting(false);
displayError(
t("error-messages.activation-code-delete-error"),
isDeleteError,
);
}
}, [isDeleteError]);
/** Starts the deletion process. */
const handleDelete = () => {
setIsDeleting(true);
setLoadingMessage(t("loading-messages.deleting-activation-code"));
};
// Error with retrieving list of users
useEffect(() => {
if (activationCodeGetError) {
displayError(
t("error-messages.activation-code-load-error"),
activationCodeGetError,
);
}
}, [activationCodeGetError]);
const dispatch = useDispatch();
const refreshCallback = (id) => {
const item = items.filter((e) => e["@id"] === id);
if (item.length !== 1) {
return;
}
dispatch(
enhancedApi.endpoints.postV2UserActivationCodesRefresh.initiate({
userActivationCodeActivationCode: JSON.stringify({
activationCode: item[0].code,
}),
}),
)
.then((response) => {
if (response.data) {
refetch();
}
})
.catch((err) => {
displayError(t("error-refreshing-code"), err);
});
};
// The columns for the table.
const columns = ActivationCodeColumns({ handleDelete });
columns.push({
path: "@id",
dataFunction: (id) => {
return (
<Button
variant="primary"
className="refresh-activation-code"
onClick={() => refreshCallback(id)}
>
{t("refresh-button")}
</Button>
);
},
label: "",
});
useEffect(() => {
if (listData) {
// Set title from code, for use with delete modal.
const newItems = [...(listData["hydra:member"] ?? [])].map((el) => {
return {
...el,
title: el.code,
};
});
setItems(newItems);
}
}, [listData]);
return (
<div className="p-3">
<ContentHeader
title={t("header")}
newBtnTitle={t("create-activation-codes")}
newBtnLink="/activation/create"
/>
<ContentBody>
<>
{listData && (
<List
columns={columns}
totalItems={listData["hydra:totalItems"]}
data={items}
handleDelete={handleDelete}
deleteSuccess={isDeleteSuccess || false}
isLoading={isLoading || isDeleting}
loadingMessage={loadingMessage}
showCreatedByFilter={false}
displaySearch={false}
/>
)}
</>
</ContentBody>
</div>
);
}
export default ActivationCodeList;