Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions client/src/components/AbsenceReasonInput.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { useInput, FieldTitle, InputHelperText, useResourceContext } from 'react-admin';
import Autocomplete from '@mui/material/Autocomplete';
import TextField from '@mui/material/TextField';
import { useAbsenceReasons } from 'src/settings/settingsUtil';

// Pick a reason from the user's settings list, or type any other text. Saves plain text either way.
export const AbsenceReasonAutocomplete = ({ value, onChange, label, helperText, error, onBlur, ...props }) => {
const reasons = useAbsenceReasons();
return (
<Autocomplete
freeSolo
options={reasons}
value={value || null}
inputValue={value ?? ''}
onChange={(event, newValue) => onChange(newValue ?? '')}
onInputChange={(event, newValue, reason) => reason === 'input' && onChange(newValue)}
onBlur={onBlur}
renderInput={(params) => (
<TextField {...params} label={label} helperText={helperText} error={error} {...props} />
)}
/>
);
};

export const AbsenceReasonInput = ({ source = 'reason', label, validate, ...props }) => {
const resource = useResourceContext();
const {
field,
fieldState: { error, invalid },
isRequired,
} = useInput({ source, validate });
return (
<AbsenceReasonAutocomplete
value={field.value}
onChange={field.onChange}
onBlur={field.onBlur}
label={<FieldTitle label={label} source={source} resource={resource} isRequired={isRequired} />}
error={invalid}
helperText={invalid && <InputHelperText error={error?.message} />}
variant="filled"
size="small"
margin="dense"
{...props}
/>
);
};
49 changes: 49 additions & 0 deletions client/src/components/AbsenceReasonInput.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { useState } from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import { AbsenceReasonAutocomplete } from './AbsenceReasonInput';

jest.mock('src/settings/settingsUtil', () => ({
useAbsenceReasons: () => ['חתונה', 'מחלה'],
}));

const Wrapper = ({ initialValue = '', onChange }) => {
const [value, setValue] = useState(initialValue);
return (
<AbsenceReasonAutocomplete
label="סיבה"
value={value}
onChange={(newValue) => {
setValue(newValue);
onChange(newValue);
}}
/>
);
};

describe('AbsenceReasonAutocomplete', () => {
it('picks a reason from the settings list', () => {
const onChange = jest.fn();
render(<Wrapper onChange={onChange} />);
const input = screen.getByLabelText('סיבה');
fireEvent.mouseDown(input);
fireEvent.click(screen.getByText('מחלה'));
expect(onChange).toHaveBeenLastCalledWith('מחלה');
expect(input.value).toBe('מחלה');
});

it('keeps free text that is not in the list', () => {
const onChange = jest.fn();
render(<Wrapper onChange={onChange} />);
const input = screen.getByLabelText('סיבה');
fireEvent.change(input, { target: { value: 'סיבה אחרת' } });
expect(onChange).toHaveBeenLastCalledWith('סיבה אחרת');
expect(input.value).toBe('סיבה אחרת');
});

it('shows an existing value without changing it', () => {
const onChange = jest.fn();
render(<Wrapper initialValue="ברית" onChange={onChange} />);
expect(screen.getByLabelText('סיבה').value).toBe('ברית');
expect(onChange).not.toHaveBeenCalled();
});
});
3 changes: 2 additions & 1 deletion client/src/components/ApproveFullDays.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from 'react-admin';
import { ActionOrDialogButton } from '@shared/components/crudContainers/ActionOrDialogButton';
import CommonReferenceInput from '@shared/components/fields/CommonReferenceInput';
import { AbsenceReasonInput } from 'src/components/AbsenceReasonInput';
import { filterByUserIdAndYear } from '@shared/components/fields/CommonReferenceInputFilter';
import { handleActionSuccess, handleError } from '@shared/utils/notifyUtil';
import { defaultYearFilter } from '@shared/utils/yearFilter';
Expand Down Expand Up @@ -63,7 +64,7 @@ export const ApproveFullDays = ({ label }) => {
dynamicFilter={filterByUserIdAndYear}
label="כיתה (לא חובה)"
/>
<TextInput source="reason" validate={maxLength(500)} label="סיבה" />
<AbsenceReasonInput validate={maxLength(500)} label="סיבה" />
<TextInput source="senderName" validate={maxLength(100)} label="שולחת" />
<TextInput source="comment" validate={maxLength(500)} label="הערה" />
</SimpleForm>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Box from '@mui/material/Box';
import Stack from '@mui/material/Stack';
import TextField from '@mui/material/TextField';
import Typography from '@mui/material/Typography';
import { AbsenceReasonAutocomplete } from 'src/components/AbsenceReasonInput';

const ConfigurationForm = ({ configValues, onChange }) => {
const handleChange = (event) => {
Expand All @@ -14,7 +15,12 @@ const ConfigurationForm = ({ configValues, onChange }) => {
שלב 2: הגדרות
</Typography>
<Stack spacing={2} sx={{ maxWidth: 600 }}>
<TextField name="reason" label="סיבה" value={configValues.reason} onChange={handleChange} fullWidth />
<AbsenceReasonAutocomplete
label="סיבה"
value={configValues.reason}
onChange={(reason) => onChange({ ...configValues, reason })}
fullWidth
/>
<TextField
name="senderName"
label="שם השולח"
Expand Down
1 change: 1 addition & 0 deletions client/src/domainTranslations.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export default {
'lessonTimes.periods': 'שיעורים',
'lessonTimes.periods.start': 'שעת התחלה',
'lessonTimes.periods.end': 'שעת סיום',
absenceReasons: 'סיבות חיסור',
phoneNumber: 'מספר טלפון',
mailAddressTitle: 'שם השולח',
mailAddressAlias: 'כתובת השולח',
Expand Down
5 changes: 3 additions & 2 deletions client/src/entities/known-absence.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { CommonYearField, CommonYearInput, CommonYearInputFilter } from '@shared
import { CommonHebrewDateField } from '@shared/components/fields/CommonHebrewDateField';
import { BulkApproveAbsences } from 'src/components/BulkApproveAbsences';
import { ApproveFullDays } from 'src/components/ApproveFullDays';
import { AbsenceReasonInput } from 'src/components/AbsenceReasonInput';
import { commonAdminFilters } from '@shared/components/fields/PermissionFilter';
import { BulkEditButton } from '@shared/components/crudContainers/BulkEditButton';
import { useIsAbsenceType } from 'src/utils/appPermissions';
Expand Down Expand Up @@ -73,7 +74,7 @@ const additionalBulkButtons = [
<CommonReferenceInput source="lessonReferenceId" reference="lesson" dynamicFilter={filterByUserIdAndYear} />
<NumberInput source="absnceCount" />
<NumberInput source="absnceCode" />
<TextInput source="reason" validate={maxLength(500)} />
<AbsenceReasonInput validate={maxLength(500)} />
<TextInput source="senderName" validate={maxLength(100)} />
<TextInput source="comment" validate={maxLength(500)} />
<NullableBooleanInput source="isApproved" />
Expand Down Expand Up @@ -160,7 +161,7 @@ const Inputs = ({ isCreate, isAdmin }) => {
<NumberInput source="absnceCount" validate={required()} />
<NumberInput source="absnceCode" />
<TextInput source="senderName" validate={maxLength(100)} />
<TextInput source="reason" validate={maxLength(500)} />
<AbsenceReasonInput validate={maxLength(500)} />
<TextInput source="comment" validate={maxLength(500)} />
<CommonFileField source="fileData" />
<CommonFileDownloadButton source="fileData" label="הורד קובץ" />
Expand Down
1 change: 1 addition & 0 deletions client/src/roadmapFeatures.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions client/src/settings/GeneralSettingsInput.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ArrayInput, CheckboxGroupInput, NumberInput, SimpleFormIterator, TimeInput, required } from 'react-admin';
import { ArrayInput, CheckboxGroupInput, NumberInput, SimpleFormIterator, TextInput, TimeInput, maxLength, required } from 'react-admin';
import { CommonSettingsAccordion } from '@shared/components/settings/CommonSettingsAccordion';
import CommonAutocompleteInput from '@shared/components/fields/CommonAutocompleteInput';
import { PAGE_SIZE_OPTIONS } from '@shared/config/settings';
Expand All @@ -22,7 +22,7 @@ export function GeneralSettingsInput() {
<CommonSettingsAccordion
id="general-settings"
title="הגדרות כלליות"
subtitle="מספר שורות בטבלה, שווי איחור וזמני שיעורים"
subtitle="מספר שורות בטבלה, שווי איחור, זמני שיעורים וסיבות חיסור"
>
<CommonAutocompleteInput
source="defaultPageSize"
Expand Down Expand Up @@ -51,6 +51,14 @@ export function GeneralSettingsInput() {
</ArrayInput>
</SimpleFormIterator>
</ArrayInput>
<ArrayInput
source="absenceReasons"
helperText="רשימת סיבות לבחירה בשדה סיבה בחיסורים מאושרים. אפשר עדיין להקליד סיבה אחרת"
>
<SimpleFormIterator inline disableReordering>
<TextInput label="סיבה" validate={[required(), maxLength(500)]} />
</SimpleFormIterator>
</ArrayInput>
</CommonSettingsAccordion>
);
}
3 changes: 2 additions & 1 deletion client/src/settings/Settings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { useNavigate } from 'react-router-dom';
import { getDefaultPageSize } from '@shared/utils/settingsUtil';
import { CommonSettingsAccordion } from '@shared/components/settings/CommonSettingsAccordion';
import { DashboardItemsInput } from '@shared/components/settings/DashboardItemsInput';
import { getLateValue, getLessonTimes, getDashboardItems, getReportStyles, getReportCardSettings, normalizeReportStyles } from './settingsUtil';
import { getLateValue, getLessonTimes, getAbsenceReasons, getDashboardItems, getReportStyles, getReportCardSettings, normalizeReportStyles } from './settingsUtil';
import { ReportStylesInput } from './ReportStylesInput';
import { GeneralSettingsInput } from './GeneralSettingsInput';
import { MailSettingsInput } from './MailSettingsInput';
Expand All @@ -40,6 +40,7 @@ export default function Settings() {
defaultPageSize: getDefaultPageSize(identity),
lateValue: getLateValue(identity),
lessonTimes: getLessonTimes(identity),
absenceReasons: getAbsenceReasons(identity),
dashboardItems: getDashboardItems(identity),
reportStyles: normalizeReportStyles(getReportStyles(identity)),
reportCardSettings: getReportCardSettings(identity),
Expand Down
9 changes: 9 additions & 0 deletions client/src/settings/settingsUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ export function getLessonTimes(identity) {
return identity?.additionalData?.lessonTimes || [];
}

export function useAbsenceReasons() {
const { identity } = useGetIdentity();
return getAbsenceReasons(identity);
}

export function getAbsenceReasons(identity) {
return identity?.additionalData?.absenceReasons || [];
}

export const useDashboardItems = () => {
const { identity } = useGetIdentity();
return getDashboardItems(identity);
Expand Down
Loading