Repository navigation
add Student Event Report create form with dynamic fields #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4c381af
add Student Event Report create form with dynamic fields
yael-miller fe49d0a
Merge branch 'buzz-code:main' into main
yael-miller 6ba2dc0
Implementing authorization logic and updating the identity of a logge…
yael-miller 91ef040
Fix small notes in student view
yael-miller 46c5d6e
fix: StudentEventReport runtime error
yael-miller 45f4b4e
Permisions
yael-miller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import React from "react"; | ||
| import { useDataProvider, SimpleForm, TextInput, useNotify, Toolbar, SaveButton, useResetStore } from "react-admin"; | ||
| import { useObjectStore } from "src/utils/storeUtil"; | ||
| import Button from "@mui/material/Button"; | ||
| import Card from "@mui/material/Card"; | ||
| import CardContent from "@mui/material/CardContent"; | ||
| import Box from "@mui/material/Box"; | ||
| import Typography from "@mui/material/Typography"; | ||
| import Avatar from "@mui/material/Avatar"; | ||
| import LockOutlinedIcon from "@mui/icons-material/LockOutlined"; | ||
| import LogoutIcon from "@mui/icons-material/Logout"; | ||
| import Paper from "@mui/material/Paper"; | ||
|
|
||
| export const StudentGuard = ({ children }) => { | ||
| const dataProvider = useDataProvider(); | ||
| const notify = useNotify(); | ||
| const reset = useResetStore(); | ||
| const { value: student, set: setStudent, clear } = useObjectStore("student"); | ||
|
|
||
| const handleLogout = () => { | ||
| clear(); | ||
| reset(); | ||
| }; | ||
|
|
||
| if (student) { | ||
| return ( | ||
| <Box display="flex" flexDirection="column" width="100%"> | ||
| <Paper | ||
| elevation={1} | ||
| sx={{ | ||
| p: 2, | ||
| mb: 2, | ||
| display: "flex", | ||
| justifyContent: "space-between", | ||
| alignItems: "center", | ||
| }} | ||
| > | ||
| <Typography variant="h6" component="div"> | ||
| שלום התלמידה <strong>{student.name}</strong> | ||
| </Typography> | ||
| <Button | ||
| variant="outlined" | ||
| color="secondary" | ||
| onClick={handleLogout} | ||
| startIcon={<LogoutIcon />} | ||
| > | ||
| התנתק | ||
| </Button> | ||
| </Paper> | ||
| <Box flexGrow={1}>{children}</Box> | ||
| </Box> | ||
| ); | ||
| } | ||
|
|
||
| const handleSubmit = async (values) => { | ||
| const { tz } = values; | ||
| const result = await dataProvider.getList("student", { | ||
| pagination: { page: 1, perPage: 1 }, | ||
| filter: { tz }, | ||
| }); | ||
| const student = result.data[0] || null; | ||
|
|
||
| if (student) { | ||
| setStudent(student); | ||
| } else { | ||
| notify("Student not found", { type: "warning" }); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <Box | ||
| display="flex" | ||
| flexDirection="column" | ||
| alignItems="center" | ||
| justifyContent="center" | ||
| minHeight="100vh" | ||
| bgcolor="grey.100" | ||
| > | ||
| <Card sx={{ minWidth: 300, maxWidth: 400, p: 2, boxShadow: 3 }}> | ||
| <Box display="flex" flexDirection="column" alignItems="center" mb={2}> | ||
| <Avatar sx={{ m: 1, bgcolor: "secondary.main" }}> | ||
| <LockOutlinedIcon /> | ||
| </Avatar> | ||
| <Typography component="h1" variant="h5"> | ||
| כניסת תלמידה | ||
| </Typography> | ||
| </Box> | ||
| <CardContent> | ||
| <SimpleForm | ||
| key="view-search-form" | ||
| onSubmit={handleSubmit} | ||
| resource="student" | ||
| toolbar={ | ||
| <Toolbar | ||
| sx={{ | ||
| display: "flex", | ||
| justifyContent: "center", | ||
| width: "100%", | ||
| bgcolor: "transparent", | ||
| p: 0, | ||
| }} | ||
| > | ||
| <SaveButton | ||
| label="כניסה" | ||
| variant="contained" | ||
| fullWidth | ||
| color="secondary" | ||
| /> | ||
| </Toolbar> | ||
| } | ||
| > | ||
| <TextInput source="tz" label="ת.ז" fullWidth variant="outlined" /> | ||
| </SimpleForm> | ||
| </CardContent> | ||
| </Card> | ||
| </Box> | ||
| ); | ||
| }; |
145 changes: 145 additions & 0 deletions
145
client/src/components/StudentView/student-event-report.jsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| import React from 'react'; | ||
| import { | ||
| TextInput, | ||
| NumberInput, | ||
| DateInput, | ||
| BooleanInput, | ||
| required, | ||
| useNotify, | ||
| useResetStore, | ||
| useDataProvider, | ||
| useGetOne, | ||
| Create, | ||
| SimpleForm, | ||
| FormDataConsumer, | ||
| Loading, | ||
| } from 'react-admin'; | ||
| import { filterByUserIdAndYear } from '@shared/components/fields/CommonReferenceInputFilter'; | ||
| import CommonReferenceInput from '@shared/components/fields/CommonReferenceInput'; | ||
| import { defaultYearFilter, yearChoices } from '@shared/utils/yearFilter'; | ||
| import CommonAutocompleteInput from '@shared/components/fields/CommonAutocompleteInput'; | ||
| import { handleActionSuccess, handleError } from '@shared/utils/notifyUtil'; | ||
| import { useObjectStore } from "src/utils/storeUtil"; | ||
|
|
||
| const DynamicFields = ({ absenceType, isLoading }) => { | ||
|
|
||
| if (isLoading) return <Loading loadingPrimary="טוען שדות..." />; | ||
| if (!absenceType?.requiredLabels?.length) return null; | ||
| return ( | ||
| <div style={{ padding: '10px 0', width: '100%', display: 'flex', gap: '10px', flexWrap: 'wrap' }}> | ||
| {absenceType.requiredLabels.map((label, index) => ( | ||
| <TextInput | ||
| key={`${absenceType.id}-${label}`} | ||
| source={`dynamic_${label}`} | ||
| label={label} | ||
| validate={required()} | ||
| style={{ flex: 1, minWidth: '200px' }} | ||
| /> | ||
| ))} | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| const formatPayload = (values, userId, studentId, absenceType) => { | ||
| const allowedLabels = absenceType?.requiredLabels || []; | ||
|
|
||
| const extraInfo = Object.keys(values) | ||
| .filter(key => { | ||
| if (!key.startsWith('dynamic_')) return false; | ||
| const label = key.replace('dynamic_', ''); | ||
| return allowedLabels.includes(label) && values[key]; | ||
| }) | ||
| .map(key => { | ||
| const label = key.replace('dynamic_', ''); | ||
| return `${label}: ${values[key]}`; | ||
| }) | ||
| .join(' | '); | ||
|
|
||
| const cleanValues = { ...values }; | ||
| Object.keys(cleanValues).forEach(key => { | ||
| if (key.startsWith('dynamic_')) delete cleanValues[key]; | ||
| }); | ||
|
|
||
| return { | ||
| ...cleanValues, | ||
| userId: userId, | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. userid is added by default so not needed here (unless in isAdmin mode, if relevant) |
||
| studentReferenceId: studentId, | ||
| year: parseInt(defaultYearFilter.year), | ||
| reportDate: cleanValues.reportDate, | ||
| absnceCount: parseFloat(cleanValues.absnceCount) || 1, | ||
| isApproved: true, | ||
| reason: cleanValues.reason ? `${cleanValues.reason} [${extraInfo}]` : extraInfo, | ||
| }; | ||
| }; | ||
|
|
||
| const StudentEventReport = (props) => { | ||
| const dataProvider = useDataProvider(); | ||
| const notify = useNotify(); | ||
| const reset = useResetStore(); | ||
| const { value: student, clear } = useObjectStore("student"); | ||
| const [selectedAbsenceTypeId, setSelectedAbsenceTypeId] = React.useState(null); | ||
| const { data: absenceType, isLoading } = useGetOne( | ||
| 'absence_type', | ||
| { id: selectedAbsenceTypeId }, | ||
| { enabled: !!selectedAbsenceTypeId } | ||
| ); | ||
|
|
||
| const handleSave = async (values) => { | ||
| try { | ||
| const quota = absenceType?.quota; | ||
|
|
||
| if (quota) { | ||
| const { data: existingAbsences } = await dataProvider.getList('known_absence', { | ||
| pagination: { page: 1, perPage: 1000 }, | ||
| filter: { | ||
| studentReferenceId: student.id, | ||
| absenceTypeId: values.absenceTypeId, | ||
| year: values.year | ||
| }, | ||
| }); | ||
|
|
||
| const currentTotal = existingAbsences.reduce((sum, item) => sum + (parseFloat(item.absnceCount) || 0), 0); | ||
| const requestedAmount = parseFloat(values.absnceCount || 1); | ||
|
|
||
| if (currentTotal + requestedAmount > quota) { | ||
| notify( | ||
| `חריגה מהמכסה! המכסה היא ${quota}. נוצלו ${currentTotal}, ניסיון להוסיף ${requestedAmount}.`, | ||
| { type: 'error', autoHideDuration: 10000 } | ||
| ); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| const formattedData = formatPayload(values, student.userId, student.id, absenceType) | ||
| const response = await dataProvider.create('known_absence', { data: formattedData }); | ||
| handleActionSuccess(notify)(response); | ||
| clear(); | ||
| reset(); | ||
| } catch (error) { | ||
| handleError(notify)(error); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <Create resource="known_absence" {...props} {...props} title="הוספת דיווח אירוע"> | ||
| <SimpleForm onSubmit={handleSave} sanitizeEmptyValues> | ||
| <CommonReferenceInput source="klassReferenceId" reference="klass" validate={required()} dynamicFilter={filterByUserIdAndYear} /> | ||
| <CommonReferenceInput source="absenceTypeId" reference="absence_type" dynamicFilter={filterByUserIdAndYear} validate={required()} | ||
| onChange={(e) => setSelectedAbsenceTypeId(e?.target?.value ?? e)} /> | ||
| <FormDataConsumer> | ||
| {() => ( | ||
| <DynamicFields absenceType={absenceType} isLoading={isLoading} /> | ||
| )} | ||
| </FormDataConsumer> | ||
| <DateInput source="reportDate" validate={required()} defaultValue={new Date()} /> | ||
| <NumberInput source="absnceCount" defaultValue={1} min={0} step={0.5} /> | ||
| <TextInput source="senderName" /> | ||
| <TextInput source="reason" multiline fullWidth /> | ||
| <CommonAutocompleteInput source="year" choices={yearChoices} defaultValue={defaultYearFilter.year} disabled /> | ||
| <BooleanInput source="isApproved" defaultValue={true} disabled /> | ||
| </SimpleForm> | ||
| </Create> | ||
| ); | ||
| }; | ||
|
|
||
| export default StudentEventReport; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dont you need to have StudentEventReport here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
לא כי אני לא רוצה שיהיה ניתוב בדף הבית של תלמידה לדף דיווח.