Skip to content

Commit

Permalink
Merge pull request #13 from Rushikesh-Sonawane99/release-1.0.0
Browse files Browse the repository at this point in the history
Issue #PS-1253:feat: API Integration to render add learner form
  • Loading branch information
itsvick authored Jul 18, 2024
2 parents 4184cb1 + 71fb40f commit e46b15f
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 55 deletions.
46 changes: 8 additions & 38 deletions src/components/GeneratedSchemas.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,16 @@
import { UiSchema } from '@rjsf/utils';
import { JSONSchema7 } from 'json-schema';
import { apiResponse } from '@/utils/schema';
import NumberInputField from './form/NumberInputField';
import { FormData, Field, FieldOption } from '@/utils/Interfaces';

interface FieldOption {
label: string;
value: string;
}

interface Field {
label: string;
name: string;
type: 'text' | 'numeric' | 'drop_down' | 'checkbox' | 'radio';
isEditable: boolean;
isPIIField?: boolean | null;
placeholder?: string;
validation?: string[];
options: FieldOption[];
isMultiSelect: boolean;
maxSelections?: number | null;
hint?: string | null;
pattern?: string | null;
maxLength?: number | null;
minLength?: number | null;
fieldId: string;
dependsOn: boolean;
required?: boolean;
}

const customFields = {
export const customFields = {
NumberInputField: NumberInputField
};

const GenerateSchemaAndUiSchema = (apiResponse: any) => {

export const GenerateSchemaAndUiSchema = (formData: FormData) => {
const schema: JSONSchema7 = { //Form schema
title: 'A registration form',
description: 'A simple form example',
title: formData.title,
description: '',
type: 'object',
required: [],
properties: {},
Expand All @@ -45,7 +19,8 @@ const GenerateSchemaAndUiSchema = (apiResponse: any) => {

const uiSchema: UiSchema = {}; //form ui schema

apiResponse.fields.forEach((field: Field) => {
// console.log('FormData', formData)
formData?.fields?.forEach((field: Field) => {
const {
label,
name,
Expand Down Expand Up @@ -180,10 +155,5 @@ const GenerateSchemaAndUiSchema = (apiResponse: any) => {
}
});

return { schema, uiSchema };
return { schema, uiSchema, customFields };
};

const { schema, uiSchema } = GenerateSchemaAndUiSchema(apiResponse);
console.log(schema, uiSchema);

export { schema, uiSchema, customFields };
57 changes: 42 additions & 15 deletions src/pages/add-learner.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
import DynamicForm from '@/components/DynamicForm';
import React from 'react';
import { schema, uiSchema, customFields } from '@/components/GeneratedSchemas';
import React, { useEffect } from 'react';
import { GenerateSchemaAndUiSchema, customFields } from '@/components/GeneratedSchemas';
import { IChangeEvent } from '@rjsf/core';
import ISubmitEvent from '@rjsf/core';
import { Box } from '@mui/material';
import { RJSFSchema } from '@rjsf/utils';
import SendCredentialModal from '@/components/SendCredentialModal';
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
import { getFormRead } from '@/services/CreateUserService';
import { FormData } from '@/utils/Interfaces';
import { FormContext, FormContextType } from '@/utils/app.constant';

const addLearner = () => {
const [openModal, setOpenModal] = React.useState(false);
const [schema, setSchema] = React.useState<any>();
const [uiSchema, setUiSchema] = React.useState<any>();

useEffect(() => {
const getAddLearnerFormData = async () => {
try {
const response: FormData = await getFormRead(FormContext.USERS, FormContextType.STUDENT);
console.log('sortedFields', response);

if (response) {
const { schema, uiSchema } = GenerateSchemaAndUiSchema(response);
setSchema(schema);
setUiSchema(uiSchema);
}
} catch (error) {
console.error('Error fetching form data:', error);
}
};
getAddLearnerFormData();
}, []);

const handleSubmit = (
data: IChangeEvent<any, RJSFSchema, any>,
Expand Down Expand Up @@ -47,20 +70,24 @@ const addLearner = () => {
};

return (
<Box margin={'5rem'}>
<DynamicForm
schema={schema}
uiSchema={uiSchema}
onSubmit={handleSubmit}
onChange={handleChange}
onError={handleError}
widgets={{}}
showErrorList={true}
customFields={customFields}
/>
<>
{schema && uiSchema && (
<Box margin={'5rem'}>
<DynamicForm
schema={schema}
uiSchema={uiSchema}
onSubmit={handleSubmit}
onChange={handleChange}
onError={handleError}
widgets={{}}
showErrorList={true}
customFields={customFields}
/>

<SendCredentialModal open={openModal} onClose={handleCloseModal} />
</Box>
<SendCredentialModal open={openModal} onClose={handleCloseModal} />
</Box>
)}
</>
);
};

Expand Down
20 changes: 20 additions & 0 deletions src/services/CreateUserService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { get } from "./RestClient";

export const getFormRead = async (context: string, contextType: string): Promise<any> => {
const apiUrl: string = `${process.env.NEXT_PUBLIC_BASE_URL}/form/read?context=${context}&contextType=${contextType}`;
try {
let response = await get(apiUrl);

const sortedFields = response?.data?.result.fields?.sort((a: { order: string; }, b: { order: string; }) => parseInt(a.order) - parseInt(b.order));
const formData = {
formid: response?.data?.result?.formid,
title: response?.data?.result?.title,
fields: sortedFields
};
return formData;

} catch (error) {
console.error('error in getting cohort details', error);
// throw error;
}
};
4 changes: 2 additions & 2 deletions src/utils/Helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ export const accessGranted = (

export function getTranslatedText(key: string, options?: any): string {
if (!i18n?.t) {
throw new Error('i18n instance is not initialized');
// throw new Error('i18n instance is not initialized');
}
return i18n.t(key, options) as string;
// return i18n.t(key, options) as string;
}
35 changes: 35 additions & 0 deletions src/utils/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,38 @@ export interface Session {
export interface SessionCardFooterProps {
item: Session;
}


export interface FieldOption {
label: string;
value: string;
}

export interface Field {
name: string;
type: 'text' | 'numeric' | 'drop_down' | 'checkbox' | 'radio';
label: string;
order: string;
coreField: number;
dependsOn: string | boolean | null;
isEditable: boolean;
isPIIField: boolean | null;
validation?: string[];
placeholder: string;
isMultiSelect: boolean;
maxSelections: number | null;
sourceDetails: Record<string, any>;
options: FieldOption[];
hint?: string | null;
pattern?: string | null;
maxLength?: number | null;
minLength?: number | null;
fieldId: string;
required?: boolean;
}

export interface FormData {
formid: string;
title: string;
fields: Field[];
}
10 changes: 10 additions & 0 deletions src/utils/app.constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,13 @@ export enum cohortPrivileges {
STUDENT = 'student',

}

export enum FormContext {
USERS = 'USERS'
}

export enum FormContextType {
STUDENT = 'STUDENT',
TEACHER = 'TEACHER',
TEAM_LEADER = 'TEAMLEADER',
}

0 comments on commit e46b15f

Please sign in to comment.