Skip to content

Commit

Permalink
Merge pull request #102 from suvarnakale/release-1.0.0
Browse files Browse the repository at this point in the history
Issue #PS-1662 feat: functionality changes for credential modal done
  • Loading branch information
itsvick authored Aug 8, 2024
2 parents 04d6e03 + 57de73f commit dee8898
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 106 deletions.
246 changes: 176 additions & 70 deletions src/components/AddFacilitator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@ import { editEditUser } from '@/services/ProfileService';
import { tenantId } from '../../app.config';
import FormButtons from './FormButtons';
import { sendCredentialService } from '@/services/NotificationService';

import {
Box,
Button,
Divider,
Modal,
Typography,
useTheme,
} from '@mui/material';
import CloseIcon from '@mui/icons-material/Close';
import { modalStyles } from '@/styles/modalStyles';
import useSubmittedButtonStore from '@/store/useSubmittedButtonStore';
interface AddFacilitatorModalprops {
open: boolean;
onClose: () => void;
Expand All @@ -39,7 +49,8 @@ const AddFacilitatorModal: React.FC<AddFacilitatorModalprops> = ({
onFacilitatorAdded,
}) => {
const [schema, setSchema] = React.useState<any>();
const [openModal, setOpenModal] = React.useState(false);
const [openSendCredModal, setOpenSendCredModal] = React.useState(false);
const [createFacilitator, setCreateFacilitator] = React.useState(false);
const [uiSchema, setUiSchema] = React.useState<any>();
const [reloadProfile, setReloadProfile] = React.useState(false);
const [email, setEmail] = React.useState('');
Expand All @@ -50,7 +61,10 @@ const AddFacilitatorModal: React.FC<AddFacilitatorModalprops> = ({
const [coreFields, setCoreFields] = React.useState<string[]>([]);

const { t } = useTranslation();

const theme = useTheme<any>();
const setSubmittedButtonStatus = useSubmittedButtonStore(
(state: any) => state.setSubmittedButtonStatus
);
useEffect(() => {
const getAddFacilitatorFormData = async () => {
try {
Expand Down Expand Up @@ -122,34 +136,17 @@ const AddFacilitatorModal: React.FC<AddFacilitatorModalprops> = ({
setTimeout(() => {
setFormData(data.formData);
});
const target = event.target as HTMLFormElement;
const elementsArray = Array.from(target.elements);

// for (const element of elementsArray) {
// if (
// (element instanceof HTMLInputElement ||
// element instanceof HTMLSelectElement ||
// element instanceof HTMLTextAreaElement) &&
// (element.value === '' ||
// (Array.isArray(element.value) && element.value.length === 0))
// ) {
// element.focus();
// return;
// }
// }

const formData = data.formData;
console.log('Form data submitted:', formData);
};

useEffect(() => {
if (formData) {
handleButtonClick();
}
}, [formData]);
}, [formData, createFacilitator]);

const handleButtonClick = async () => {
console.log('Form data:', formData);
setSubmittedButtonStatus(true);
if (formData) {
const schemaProperties = schema.properties;
setEmail(formData?.email);
Expand Down Expand Up @@ -262,60 +259,70 @@ const AddFacilitatorModal: React.FC<AddFacilitatorModalprops> = ({
}
} else {
if (formData?.assignCenters?.length > 0) {
const response = await createUser(apiBody);
console.log(response);
if (response) {
onFacilitatorAdded?.();
onClose();
showToastMessage(
t('COMMON.FACILITATOR_ADDED_SUCCESSFULLY'),
'success'
);

const isQueue = false;
const context = 'USER';
let createrName;
if (typeof window !== 'undefined' && window.localStorage) {
createrName = localStorage.getItem('userName');
}
const key = 'onFacilitatorCreated';
const replacements = [apiBody['name'], username, password];
setOpenSendCredModal(true);
if (createFacilitator) {
try {
const response = await createUser(apiBody);

const sendTo = {
receipients: [formData?.email],
};
if (replacements && sendTo) {
const response = await sendCredentialService({
isQueue,
context,
key,
replacements,
email: sendTo,
});
if (response.result[0].data[0].status === 'success') {
if (response) {
onFacilitatorAdded?.();
onClose();
showToastMessage(
t('COMMON.USER_CREDENTIAL_SEND_SUCCESSFULLY'),
t('COMMON.FACILITATOR_ADDED_SUCCESSFULLY'),
'success'
);

const isQueue = false;
const context = 'USER';
const key = 'onFacilitatorCreated';
const replacements = [apiBody['name'], username, password];
const sendTo = {
receipients: [formData?.email],
};

let createrName;
if (typeof window !== 'undefined' && window.localStorage) {
createrName = localStorage.getItem('userName');
}

if (replacements && sendTo) {
const credentialResponse = await sendCredentialService({
isQueue,
context,
key,
replacements,
email: sendTo,
});

if (
credentialResponse?.result[0]?.data[0]?.status ===
'success'
) {
showToastMessage(
t('COMMON.USER_CREDENTIAL_SEND_SUCCESSFULLY'),
'success'
);
} else {
showToastMessage(
t('COMMON.USER_CREDENTIALS_WILL_BE_SEND_SOON'),
'success'
);
}
} else {
showToastMessage(t('COMMON.SOMETHING_WENT_WRONG'), 'error');
}
} else {
showToastMessage(
t('COMMON.USER_CREDENTIALS_WILL_BE_SEND_SOON'),
'success'
);
showToastMessage(t('COMMON.SOMETHING_WENT_WRONG'), 'error');
}
} else {
} catch (error) {
console.error(error);
showToastMessage(t('COMMON.SOMETHING_WENT_WRONG'), 'error');
}

setOpenModal(true);
} else {
showToastMessage(t('COMMON.SOMETHING_WENT_WRONG'), 'error');
}
} else {
showToastMessage(t('COMMON.PLEASE_SELECT_THE_CENTER'), 'error');
}
}
onClose();
} catch (error) {
onClose();
showToastMessage(t('COMMON.SOMETHING_WENT_WRONG'), 'error');
Expand All @@ -332,8 +339,16 @@ const AddFacilitatorModal: React.FC<AddFacilitatorModalprops> = ({
console.log('Form errors:', errors);
};

const onCloseModal = () => {
setOpenModal(false);
const handleBackAction = () => {
setCreateFacilitator(false);
setOpenSendCredModal(false);
};

const handleAction = () => {
setTimeout(() => {
setCreateFacilitator(true);
});
setOpenSendCredModal(false);
};

return (
Expand Down Expand Up @@ -381,6 +396,7 @@ const AddFacilitatorModal: React.FC<AddFacilitatorModalprops> = ({
widgets={{}}
showErrorList={true}
customFields={customFields}
formData={createFacilitator ? '' : formData}
>
<FormButtons
formData={formData}
Expand All @@ -391,11 +407,101 @@ const AddFacilitatorModal: React.FC<AddFacilitatorModalprops> = ({
</DynamicForm>
)}
</SimpleModal>
<SendCredentialModal
open={openModal}
onClose={onCloseModal}
email={email}
/>
<Modal
open={openSendCredModal}
aria-labelledby="send credential modal"
aria-describedby="to send credentials"
>
<Box sx={modalStyles(theme, '65%')}>
<Box
display={'flex'}
justifyContent={'space-between'}
sx={{ padding: '18px 16px' }}
>
<Box marginBottom={'0px'}>
<Typography
variant="h2"
sx={{
color: theme.palette.warning['A200'],
fontSize: '14px',
}}
component="h2"
>
{t('COMMON.NEW', { role: 'Learner' })}
</Typography>
</Box>
<CloseIcon
sx={{
cursor: 'pointer',
color: theme.palette.warning['A200'],
}}
onClick={onClose}
/>
</Box>
<Divider />
{/* {isButtonAbsent ? ( */}
<Box
sx={{
padding: '18px 16px',
width: '100%',
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
textAlign: 'center',
}}
>
<Box>
<Typography
variant="h2"
sx={{
color: theme.palette.warning['400'],
fontSize: '14px',
}}
component="h2"
>
{t('COMMON.CREDENTIALS_EMAILED')}
</Typography>
</Box>
<Box sx={{ padding: '0 1rem' }}>
<Typography
variant="h2"
sx={{
color: theme.palette.warning['400'],
fontSize: '14px',
}}
component="h2"
>
{email}
</Typography>
</Box>
</Box>

<>
<Box mt={1.5}>
<Divider />
</Box>
<Box p={'18px'} display={'flex'} gap={'1rem'}>
<Button
className="w-100"
sx={{ boxShadow: 'none' }}
variant="outlined"
onClick={() => handleBackAction()}
>
{t('COMMON.BACK')}
</Button>
<Button
className="w-100"
sx={{ boxShadow: 'none' }}
variant="contained"
onClick={() => handleAction()}
>
{t('COMMON.SEND_CREDENTIALS')}
</Button>
</Box>
</>
</Box>
</Modal>
</>
);
};
Expand Down
7 changes: 5 additions & 2 deletions src/components/AddLeanerModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { createUser, getFormRead } from '@/services/CreateUserService';
import { generateUsernameAndPassword } from '@/utils/Helper';
import { FormData } from '@/utils/Interfaces';
import { FormContext, FormContextType, RoleId } from '@/utils/app.constant';
import { Button, useTheme } from '@mui/material';
import { IChangeEvent } from '@rjsf/core';
import { RJSFSchema } from '@rjsf/utils';
import React, { useEffect } from 'react';
Expand All @@ -19,6 +18,7 @@ import { tenantId } from '../../app.config';
import SendCredentialModal from './SendCredentialModal';
import FormButtons from './FormButtons';
import { sendCredentialService } from '@/services/NotificationService';
import useSubmittedButtonStore from '@/store/useSubmittedButtonStore';

interface AddLearnerModalProps {
open: boolean;
Expand Down Expand Up @@ -46,7 +46,9 @@ const AddLearnerModal: React.FC<AddLearnerModalProps> = ({
const [fullname, setFullname] = React.useState<any>();

const { t } = useTranslation();
const theme = useTheme<any>();
const setSubmittedButtonStatus = useSubmittedButtonStore(
(state: any) => state.setSubmittedButtonStatus
);
let userEmail: string = '';
if (typeof window !== 'undefined' && window.localStorage) {
userEmail = localStorage.getItem('userEmail') || '';
Expand Down Expand Up @@ -108,6 +110,7 @@ const AddLearnerModal: React.FC<AddLearnerModalProps> = ({

const handleButtonClick = async () => {
console.log('Form data:', formData);
setSubmittedButtonStatus(true);
if (learnerFormData) {
const schemaProperties = schema.properties;
let cohortId, fieldData;
Expand Down
Loading

0 comments on commit dee8898

Please sign in to comment.