forked from expertiza/reimplementation-front-end
/
ParticipantEditor.tsx
229 lines (213 loc) · 7.27 KB
/
ParticipantEditor.tsx
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import React, { useEffect } from "react";
import { Formik, Form, Field, FormikHelpers } from "formik";
import {
Button,
InputGroup,
Row,
Col,
Form as BootstrapForm,
OverlayTrigger,
Tooltip,
} from "react-bootstrap";
import FormSelect from "components/Form/FormSelect";
import useAPI from "hooks/useAPI";
import { useDispatch, useSelector } from "react-redux";
import { useLoaderData, useLocation, useNavigate, useParams } from "react-router-dom";
import { alertActions } from "store/slices/alertSlice";
import { HttpMethod } from "utils/httpMethods";
import { RootState } from "store/store";
import { ROLE } from "utils/interfaces";
import { IParticipantFormValues, transformParticipantRequest } from "./participantUtil";
/**
* @author Mrityunjay Joshi on October, 2023
*/
const initialValues: IParticipantFormValues = {
type: "",
assignment_id: -1,
course_id: -1,
user_id: -1,
name: "",
};
interface IParticipantEditor {
mode: "create" | "update";
type: string;
}
const ParticipantEditor: React.FC<IParticipantEditor> = ({ mode, type }) => {
const roleOptions = [
{
label: "Participant",
value: "participant",
description:
"Engages fully in project activities, submitting work and participating in peer reviews.",
},
{ label: "Reader", value: "reader", description: "Just takes the quiz on the artifact" },
{
label: "Reviewer",
value: "reviewer",
description:
"Assesses the work of peers, providing feedback and recommendations for improvement.",
},
{
label: "Submitter",
value: "submitter",
description:
"Responsible for uploading content or documents pertinent to the project or course requirements.",
},
{
label: "Mentor",
value: "mentor",
description:
"Guides participants through advice, expertise, and feedback to enhance their learning and project outcomes.",
},
];
const { data: participantResponse, error: participantError, sendRequest } = useAPI();
const auth = useSelector(
(state: RootState) => state.authentication,
(prev, next) => prev.isAuthenticated === next.isAuthenticated
);
const { participantData, users }: any = useLoaderData();
const dispatch = useDispatch();
const navigate = useNavigate();
const location = useLocation();
const { typeId } = useParams();
// logged-in participant is the parent of the participant being created and the institution is the same as the parent's
// Close the modal if the participant is updated successfully and navigate to the participants page
useEffect(() => {
if (
participantResponse &&
participantResponse.status >= 200 &&
participantResponse.status < 300
) {
dispatch(
alertActions.showAlert({
variant: "success",
message: `Participant ${participantData.name} ${mode}d successfully!`,
})
);
navigate(location.state?.from ? location.state.from : `/${type}/${typeId}/participants/new`);
}
}, [
dispatch,
mode,
navigate,
participantData.name,
participantResponse,
location.state?.from,
type,
]);
// Show the error message if the participant is not updated successfully
useEffect(() => {
participantError &&
dispatch(alertActions.showAlert({ variant: "danger", message: participantError }));
}, [participantError, dispatch]);
const onSubmit = (
values: IParticipantFormValues,
submitProps: FormikHelpers<IParticipantFormValues>
) => {
let method: HttpMethod = HttpMethod.POST;
let url: string = "/participants";
if (mode === "update") {
url = `/participants/${values.id}`;
method = HttpMethod.PATCH;
}
values.type = type;
if (type === "assignments" || type === "student_tasks") {
values.assignment_id = typeId as unknown as number;
} else if (type === "courses") {
values.course_id = typeId as unknown as number;
}
// to be used to display message when participant is created
participantData.name = values.name;
sendRequest({
url: url,
method: method,
data: values,
transformRequest: transformParticipantRequest,
});
submitProps.setSubmitting(false);
};
return (
<>
{participantError && <p className="text-danger">{participantError}</p>}
<Formik
initialValues={mode === "update" ? participantData : initialValues}
onSubmit={onSubmit}
validateOnChange={false}
enableReinitialize={true}
>
{(formik) => (
<Form>
<Row className="align-items-center">
<Col xs={12} md={3}>
<InputGroup className="d-flex align-items-center">
<InputGroup.Text id="user-prepend">Users</InputGroup.Text>
<FormSelect
controlId="participant-user"
name="user_id"
options={users}
disabled={mode === "update" || auth.user.role !== ROLE.ADMIN.valueOf()}
/>
<OverlayTrigger
placement="top"
overlay={<Tooltip id="tooltip-add-participant">Add Participant</Tooltip>}
>
<Button
variant="outline-success"
type="submit"
disabled={
!(
formik.isValid &&
formik.dirty &&
Object.keys(formik.touched).length > 0
) || formik.isSubmitting
}
className="ms-2"
>
Add
</Button>
</OverlayTrigger>
</InputGroup>
</Col>
<Col xs={12} md={6}>
{roleOptions.map((role) => (
<div key={role.value} className="mb-3 ms-3 d-inline-flex align-items-center">
<Field
type="radio"
id={role.value}
name="role"
value={role.value}
style={{ marginRight: "5px" }}
/>
<BootstrapForm.Label
htmlFor={role.value}
className="ms-1"
style={{ marginRight: "5px" }}
>
{role.label}
</BootstrapForm.Label>
<OverlayTrigger
placement="top"
overlay={<Tooltip id={`tooltip-${role.value}`}>{role.description}</Tooltip>}
>
<img
src="/assets/icons/info.png" // Correct path
alt="Info"
style={{
width: "16px",
height: "16px",
verticalAlign: "middle", // Aligns the icon vertically with adjacent text
cursor: "pointer",
}}
/>
</OverlayTrigger>
</div>
))}
</Col>
</Row>
</Form>
)}
</Formik>
</>
);
};
export default ParticipantEditor;