diff --git a/client/src/App.jsx b/client/src/App.jsx index 30399a52..52d44214 100644 --- a/client/src/App.jsx +++ b/client/src/App.jsx @@ -40,6 +40,7 @@ import teacher from "src/entities/teacher"; import transportation from 'src/entities/transportation'; import absenceType from 'src/entities/absenceType'; import studentKlassesReport from "src/entities/student-klasses-report"; +import StudentEventReport from "src/components/StudentView/student-event-report"; import reportMonth from "src/entities/report-month"; import teacherReportStatus from "src/entities/teacher-report-status"; import teacherGradeReportStatus from "src/entities/teacher-grade-report-status"; @@ -68,7 +69,7 @@ import paymentTrack from '@shared/components/common-entities/payment-track'; import yemotCall from '@shared/components/common-entities/yemot-call'; import { isShowUsersData, isEditPagesData, isEditPaymentTracksData, isAdmin } from "@shared/utils/permissionsUtil"; -import { isLessonSignature, isOnlyInLessonReport, isTeacherView, isTransportation, isAbsenceType } from 'src/utils/appPermissions'; +import { isLessonSignature, isOnlyInLessonReport, isTeacherView, isTransportation, isAbsenceType, isStudentView } from 'src/utils/appPermissions'; import YemotSimulator from "@shared/components/views/YemotSimulator"; import { RegisterPage } from '@shared/components/layout/RegisterPage'; import { LoginPage } from '@shared/components/layout/LoginPage'; @@ -123,6 +124,7 @@ const App = () => ( {permissions => { const onlyInLesson = isOnlyInLessonReport(permissions) && !isAdmin(permissions); const teacherView = isTeacherView(permissions) && !isAdmin(permissions); + const studentView = isStudentView(permissions) && !isAdmin(permissions); if (onlyInLesson) { return ( <> @@ -151,8 +153,11 @@ const App = () => ( ); } - - + if (studentView) { + return( + <> + ); + } return ( <> @@ -168,7 +173,6 @@ const App = () => ( - diff --git a/client/src/GeneralLayout.jsx b/client/src/GeneralLayout.jsx index 9d1d69a1..151421b1 100644 --- a/client/src/GeneralLayout.jsx +++ b/client/src/GeneralLayout.jsx @@ -19,11 +19,11 @@ import ViewListIcon from '@mui/icons-material/ViewList'; import BaseLayout from "@shared/components/layout/Layout"; import BaseDashboard from '@shared/components/views/Dashboard'; -import { isInLessonReport, isScannerUpload, isOnlyInLessonReport, isTeacherView } from './utils/appPermissions'; +import { isInLessonReport, isScannerUpload, isOnlyInLessonReport, isTeacherView, isStudentView } from './utils/appPermissions'; import { useDashboardItems } from './settings/settingsUtil'; -const isStandardView = (permissions) => !isOnlyInLessonReport(permissions) && !isTeacherView(permissions); -const shouldShowLessonReports = (permissions) => isInLessonReport(permissions) || isTeacherView(permissions); +const isStandardView = (permissions) => !isOnlyInLessonReport(permissions) && !isTeacherView(permissions) && !isStudentView(permissions); +const shouldShowLessonReports = (permissions) => (isInLessonReport(permissions) || isTeacherView(permissions)); const customMenuItems = [ // static items: render only when NOT restricted to lesson-report-only @@ -47,7 +47,7 @@ const menuGroups = [ } /> ] }), - ({ permissions }) => !isOnlyInLessonReport(permissions) && ({ + ({ permissions }) => (!isOnlyInLessonReport(permissions) && (!isStudentView(permissions))) && ({ name: 'report', icon: , routes: [ ({ permissions }) => !isTeacherView(permissions) && } />, } />, diff --git a/client/src/RootDashboard.jsx b/client/src/RootDashboard.jsx index 9e7a81bb..aad18c49 100644 --- a/client/src/RootDashboard.jsx +++ b/client/src/RootDashboard.jsx @@ -1,10 +1,14 @@ import React from 'react'; import { Dashboard } from 'src/GeneralLayout'; -import { useIsTeacherView } from 'src/utils/appPermissions'; +import { useIsTeacherView, useIsStudentView } from 'src/utils/appPermissions'; import { TeacherGuard } from 'src/components/TeacherView/TeacherAccess'; +import { StudentGuard } from './components/StudentView/StudentAccess'; +import StudentEventReport from './components/StudentView/student-event-report'; export const RootDashboard = (props) => { const isTeacherView = useIsTeacherView(); + const isStudentView = useIsStudentView(); + if (isTeacherView) { return ( @@ -13,7 +17,13 @@ export const RootDashboard = (props) => { ); } - else { - return ; + else if (isStudentView) { + return ( + + + + ); } + else return ; + } diff --git a/client/src/components/StudentView/StudentAccess.jsx b/client/src/components/StudentView/StudentAccess.jsx new file mode 100644 index 00000000..bb1ccbfa --- /dev/null +++ b/client/src/components/StudentView/StudentAccess.jsx @@ -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 ( + + + + שלום התלמידה {student.name} + + + + {children} + + ); + } + + 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 ( + + + + + + + + כניסת תלמידה + + + + + + + } + > + + + + + + ); +}; diff --git a/client/src/components/StudentView/student-event-report.jsx b/client/src/components/StudentView/student-event-report.jsx new file mode 100644 index 00000000..79f1dfb0 --- /dev/null +++ b/client/src/components/StudentView/student-event-report.jsx @@ -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 ; + if (!absenceType?.requiredLabels?.length) return null; + return ( +
+ {absenceType.requiredLabels.map((label, index) => ( + + ))} +
+ ); +}; + +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, + 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 ( + + + + setSelectedAbsenceTypeId(e?.target?.value ?? e)} /> + + {() => ( + + )} + + + + + + + + + + ); +}; + +export default StudentEventReport; \ No newline at end of file diff --git a/client/src/domainTranslations.js b/client/src/domainTranslations.js index 45972b2b..9f0cc81e 100644 --- a/client/src/domainTranslations.js +++ b/client/src/domainTranslations.js @@ -141,7 +141,7 @@ export default { reason: 'סיבה', comment: 'הערה', isApproved: 'מאושר', - absenceTypeId: 'סוג אירוע', + absenceTypeId: 'סוג אירוע', } }, lesson: { @@ -213,6 +213,7 @@ export default { name: 'אירוע |||| אירועים', fields: { ...generalResourceFieldsTranslation, + absenceTypeId: 'סוג אירוע', name: 'שם האירוע', quota: 'מכסה שנתית', requiredLabels: 'שדות חובה לדיווח', diff --git a/client/src/utils/appPermissions.js b/client/src/utils/appPermissions.js index c938a286..b7c25e46 100644 --- a/client/src/utils/appPermissions.js +++ b/client/src/utils/appPermissions.js @@ -12,6 +12,7 @@ export const appPermissions = { teacherView: 'teacherView', transportation: 'transportation', absenceType: 'absenceType', + studentView: 'studentView', }; export const isScannerUpload = (permissions) => hasPermissionLogic(permissions, appPermissions.scannerUpload); @@ -45,5 +46,8 @@ export const isTransportation = (permissions) => hasPermissionLogic(permissions, export const useIsTransportation = () => useHasPermission(appPermissions.transportation); export const isAbsenceType = (permissions) => hasPermissionLogic(permissions, appPermissions.absenceType); -export const useIsisAbsenceType = () => useHasPermission(appPermissions.absenceType); +export const useIsAbsenceType = () => useHasPermission(appPermissions.absenceType); + +export const isStudentView = (permissions) => hasPermissionLogic(permissions, appPermissions.studentView); +export const useIsStudentView = () => useHasPermission(appPermissions.studentView);