diff --git a/public/locales/en/common.json b/public/locales/en/common.json index 01c1f3cc5..2ce900ec6 100644 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -435,7 +435,9 @@ "MIN_LENGTH_CHARACTERS_ERROR": "Minimum {{minLength}} characters required", "MAX_LENGTH_CHARACTERS_ERROR": "Maximum {{maxLength}} characters allowed", "NUMBER_AND_SPECIAL_CHARACTERS_NOT_ALLOWED": "Numbers and special characters are not allowed", - "ENTER_VALID_MOBILE_NUMBER": "Enter Valid mobile number" + "ENTER_VALID_MOBILE_NUMBER": "Enter Valid mobile number", + "ENTER_VALID_EMAIL": "Enter valid Email", + "ENTER_VALID_YEAR": "Enter valid Year" }, "COURSE_PLANNER": { "COURSE_PLANNER": "Course Plan", diff --git a/src/components/AddFacilitator.tsx b/src/components/AddFacilitator.tsx index 3d1a57df5..b3de99fb0 100644 --- a/src/components/AddFacilitator.tsx +++ b/src/components/AddFacilitator.tsx @@ -161,10 +161,17 @@ const AddFacilitatorModal: React.FC = ({ value: [String(fieldValue)], }); } else { - apiBody.customFields.push({ - fieldId: fieldId, - value: String(fieldValue), - }); + if (fieldSchema.checkbox && fieldSchema.type === 'array') { + apiBody.customFields.push({ + fieldId: fieldId, + value: String(fieldValue).split(','), + }); + } else { + apiBody.customFields.push({ + fieldId: fieldId, + value: String(fieldValue), + }); + } } } }); @@ -214,7 +221,14 @@ const AddFacilitatorModal: React.FC = ({ } else { const response = await createUser(apiBody); console.log(response); - showToastMessage(t('COMMON.FACILITATOR_ADDED_SUCCESSFULLY'), 'success'); + if (response) { + showToastMessage( + t('COMMON.FACILITATOR_ADDED_SUCCESSFULLY'), + 'success' + ); + } else { + showToastMessage(t('COMMON.SOMETHING_WENT_WRONG'), 'error'); + } } onClose(); } catch (error) { diff --git a/src/components/DynamicForm.tsx b/src/components/DynamicForm.tsx index 5398d3ebb..6d57c9827 100644 --- a/src/components/DynamicForm.tsx +++ b/src/components/DynamicForm.tsx @@ -9,6 +9,7 @@ import MultiSelectCheckboxes from './MultiSelectCheckboxes'; import { Button, Divider } from '@mui/material'; import { useTheme } from '@mui/material/styles'; import MultiSelectDropdown from './MultiSelectDropdown'; +import { getCurrentYearPattern } from '@/utils/Helper'; const FormWithMaterialUI = withTheme(MaterialUITheme); @@ -73,6 +74,8 @@ const DynamicForm: React.FC = ({ function transformErrors(errors: any) { console.log('errors', errors); console.log('schema', schema); + const currentYearPattern = new RegExp(getCurrentYearPattern()); + return errors.map((error: any) => { switch (error.name) { case 'required': { @@ -110,6 +113,13 @@ const DynamicForm: React.FC = ({ ); break; } + default: { + const validRange = currentYearPattern.test(pattern); + if (!validRange) { + error.message = t('FORM_ERROR_MESSAGES.ENTER_VALID_YEAR'); + } + break; + } } break; } diff --git a/src/components/GeneratedSchemas.ts b/src/components/GeneratedSchemas.ts index 0a7c942b8..3ad570f9c 100644 --- a/src/components/GeneratedSchemas.ts +++ b/src/components/GeneratedSchemas.ts @@ -2,6 +2,7 @@ import { UiSchema } from '@rjsf/utils'; import { JSONSchema7 } from 'json-schema'; import NumberInputField from './form/NumberInputField'; import { FormData, Field, FieldOption } from '@/utils/Interfaces'; +import { getCurrentYearPattern } from '@/utils/Helper'; export const customFields = { NumberInputField: NumberInputField, @@ -216,6 +217,10 @@ export const GenerateSchemaAndUiSchema = ( fieldSchema.minLength = Number(field.minLength); } + if (field?.default) { + fieldSchema.default = field.default; + } + if (field?.maxLength) { fieldSchema.maxLength = Number(field.maxLength); } @@ -224,6 +229,9 @@ export const GenerateSchemaAndUiSchema = ( if (field?.validation?.includes('numeric')) { // fieldUiSchema['ui:field'] = 'NumberInputField'; } + if (field?.validation?.includes('currentYear')) { + fieldSchema.pattern = getCurrentYearPattern(); + } fieldSchema.validation = field.validation; } diff --git a/src/utils/Helper.ts b/src/utils/Helper.ts index 3ec5f6f6d..afffd742f 100644 --- a/src/utils/Helper.ts +++ b/src/utils/Helper.ts @@ -324,3 +324,8 @@ export const convertLocalToUTC = (localDateTime: any) => { const utcDateTime = localDate.toISOString(); return utcDateTime; }; + +export const getCurrentYearPattern = () => { + const currentYear = new Date().getFullYear(); + return `^(19[0-9][0-9]|20[0-${Math.floor(currentYear / 10) % 10}][0-${currentYear % 10}])$`; +}; diff --git a/src/utils/Interfaces.ts b/src/utils/Interfaces.ts index 08d07cd57..b914f08f7 100644 --- a/src/utils/Interfaces.ts +++ b/src/utils/Interfaces.ts @@ -378,6 +378,7 @@ export interface Field { minLength?: number | null; fieldId: string; isRequired?: boolean; + default?: any; } export interface FormData {