forked from os2display/display-api-service
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivation-code-create.jsx
More file actions
83 lines (74 loc) · 2.28 KB
/
Copy pathactivation-code-create.jsx
File metadata and controls
83 lines (74 loc) · 2.28 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
import { useState, useEffect } from "react";
import { useTranslation } from "react-i18next";
import { useNavigate } from "react-router-dom";
import { usePostV2UserActivationCodesMutation } from "../../redux/enhanced-api.ts";
import ActivationCodeForm from "./activation-code-form";
import {
displaySuccess,
displayError,
} from "../util/list/toast-component/display-toast";
/**
* The user create component.
*
* @returns {object} The user create page.
*/
function ActivationCodeCreate() {
const { t } = useTranslation("common", {
keyPrefix: "activation-code-create",
});
const navigate = useNavigate();
const headerText = t("create-new-activation-code-header");
const [formStateObject, setFormStateObject] = useState({
displayName: "",
role: "",
});
const [
PostV2UserActivationCode,
{ error: saveError, isLoading: isSaving, isSuccess: isSaveSuccess },
] = usePostV2UserActivationCodesMutation();
/** Handle submitting is done. */
useEffect(() => {
if (isSaveSuccess) {
displaySuccess(t("success-messages.saved-activation-code"));
navigate("/activation/list");
}
}, [isSaveSuccess]);
/** If the user is saved with error, display the error message */
useEffect(() => {
if (saveError) {
displayError(t("error-messages.save-activation-code-error"), saveError);
}
}, [saveError]);
/**
* Set state on change in input field
*
* @param {object} props The props.
* @param {object} props.target Event target.
*/
const handleInput = ({ target }) => {
const localFormStateObject = { ...formStateObject };
localFormStateObject[target.id] = target.value;
setFormStateObject(localFormStateObject);
};
/** Handles submit. */
const handleSubmit = () => {
const saveData = {
displayName: formStateObject.displayName,
roles: [formStateObject.role],
};
PostV2UserActivationCode({
userActivationCodeUserActivationCodeInputJsonld: JSON.stringify(saveData),
});
};
return (
<ActivationCodeForm
activationCode={formStateObject}
headerText={headerText}
handleInput={handleInput}
handleSubmit={handleSubmit}
isLoading={isSaving}
loadingMessage={t("loading-messages.saving-activation-code")}
/>
);
}
export default ActivationCodeCreate;