-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCreateInstructorForm.jsx
More file actions
135 lines (123 loc) · 5.1 KB
/
CreateInstructorForm.jsx
File metadata and controls
135 lines (123 loc) · 5.1 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
import { useState } from 'react';
import { Alert, Box, Button, Typography } from '@mui/material';
import RequiredTextField from '../../../src/components/RequiredTextField';
import ImageUpload from '../../../src/components/ImageUpload.jsx';
import { useAppContext } from '../../../src/render.jsx';
import { getCookie } from '../../../src/utils';
const CreateInstructorForm = ({ onSuccess, activeOrganizationId }) => {
const [email, setEmail] = useState('');
const [emailHelperText, setEmailHelperText] = useState('');
const [displayName, setDisplayName] = useState('');
const [displayNameHelperText, setDisplayNameHelperText] = useState('');
const [photoPath, setPhotoPath] = useState(null);
const [photoUrl, setPhotoUrl] = useState(null);
const [errorMessage, setErrorMessage] = useState('');
const { localeMessages, apiBaseUrl } = useAppContext();
const isValidEmail = (value) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
const handleSubmit = () => {
const trimmedEmail = email.trim();
const trimmedDisplayName = displayName.trim();
let valid = true;
if (!trimmedEmail) {
setEmailHelperText(localeMessages['email_required_helper_text']);
valid = false;
} else if (!isValidEmail(trimmedEmail)) {
setEmailHelperText(localeMessages['invalid_email_helper_text']);
valid = false;
} else {
setEmailHelperText('');
}
if (!trimmedDisplayName) {
setDisplayNameHelperText(localeMessages['instructor_display_name_required']);
valid = false;
} else {
setDisplayNameHelperText('');
}
if (!valid) return;
setErrorMessage('');
fetch(`${apiBaseUrl}/users/get-or-create-by-email/`, {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': getCookie('csrftoken'),
},
body: JSON.stringify({ email: trimmedEmail, organization_id: activeOrganizationId }),
})
.then((response) => {
if (!response.ok) throw new Error('Failed to get or create user');
return response.json();
})
.then((userData) =>
fetch(`${apiBaseUrl}/organizations/${activeOrganizationId}/users/`, {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': getCookie('csrftoken'),
},
body: JSON.stringify({
user_id: userData.id,
role: 'instructor',
display_name: trimmedDisplayName,
photo: photoPath,
}),
})
)
.then((response) => {
if (!response.ok) throw new Error('Failed to add instructor to organization');
return response.json();
})
.then((orgUserData) => {
if (onSuccess) onSuccess(orgUserData);
setEmail('');
setDisplayName('');
setPhotoPath(null);
setPhotoUrl(null);
})
.catch((error) => {
console.error('Error adding instructor:', error);
setErrorMessage(localeMessages['instructor_add_failed']);
});
};
return (
<Box>
{errorMessage && <Alert severity="error" sx={{ mb: 2 }}>{errorMessage}</Alert>}
<RequiredTextField
label={localeMessages['instructor_email']}
helperText={emailHelperText}
fullWidth
margin="normal"
value={email}
onChange={(e) => setEmail(e.target.value)}
type="email"
/>
<RequiredTextField
label={localeMessages['instructor_display_name']}
helperText={displayNameHelperText}
fullWidth
margin="normal"
value={displayName}
onChange={(e) => {
setDisplayName(e.target.value);
if (displayNameHelperText) setDisplayNameHelperText('');
}}
/>
<Typography variant="body2" sx={{ mt: 1, mb: 0.5 }}>
{localeMessages['instructor_photo']}
</Typography>
<ImageUpload
initialUrl={photoUrl}
onUploadSuccess={(data) => {
setPhotoUrl(data.file_url);
setPhotoPath(data.file_path);
}}
onUploadError={() => setErrorMessage(localeMessages['instructor_add_failed'])}
/>
<Button variant="contained" onClick={handleSubmit} sx={{ mt: 1, boxShadow: 'none', display: 'block', ml: 'auto' }}>
{localeMessages['add_instructor']}
</Button>
</Box>
);
};
export default CreateInstructorForm;