Skip to content

Commit

Permalink
feat: various fixes to auth
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinWu098 committed Oct 3, 2024
1 parent 343435a commit a72e7ef
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 51 deletions.
114 changes: 70 additions & 44 deletions src/components/AdminSettings/AdminModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { CloseIcon } from '@chakra-ui/icons';
import {
Alert,
AlertDescription,
AlertIcon,
AlertTitle,
Button,
Flex,
HStack,
IconButton,
Input,
Modal,
Expand All @@ -14,16 +16,19 @@ import {
ModalFooter,
ModalHeader,
ModalOverlay,
Stack,
Text,
} from '@chakra-ui/react';
import PropTypes from 'prop-types';
import { renderEmail } from 'react-html-email';

// import { renderEmail } from 'react-html-email';

import { useBackend } from '../../contexts/BackendContext';
import {
approvedEmailTemplateAdmin,
editedEmailTemplateAdmin,
} from './AdminSettingsEmailTemplates';

// import {
// approvedEmailTemplateAdmin,
// editedEmailTemplateAdmin,
// } from './AdminSettingsEmailTemplates';

// TODO: Refactor states such that its a nested state
const AdminModal = ({ isOpen, onClose, data, loadInfo, toast }) => {
Expand All @@ -46,7 +51,7 @@ const AdminModal = ({ isOpen, onClose, data, loadInfo, toast }) => {
}
};
getData();
}, [isOpen]);
}, [data, isOpen]);

const closeAndReset = () => {
setNameData('');
Expand All @@ -69,16 +74,16 @@ const AdminModal = ({ isOpen, onClose, data, loadInfo, toast }) => {
setAlertVisible(true);
return;
} else {
const emailSubject =
data == null ? `FPH Has Added You As A User` : `FPH Has Edited Your Account`;
try {
const emailInfo = {
email: emailData,
messageHtml: renderEmail(
data == null ? approvedEmailTemplateAdmin : editedEmailTemplateAdmin,
),
subject: emailSubject,
};
// const emailSubject =
// data == null ? `FPH Has Added You As A User` : `FPH Has Edited Your Account`;
// const emailInfo = {
// email: emailData,
// messageHtml: renderEmail(
// data == null ? approvedEmailTemplateAdmin : editedEmailTemplateAdmin,
// ),
// subject: emailSubject,
// };

if (data == null) {
await backend.post(`/adminUser`, body);
Expand All @@ -99,7 +104,9 @@ const AdminModal = ({ isOpen, onClose, data, loadInfo, toast }) => {
isClosable: true,
});
}
await backend.post('/email/send', emailInfo);

// ! not implemented
// await backend.post('/email/send', emailInfo);
} catch (error) {
console.error(error);
toast({
Expand All @@ -111,48 +118,67 @@ const AdminModal = ({ isOpen, onClose, data, loadInfo, toast }) => {
});
}
}

closeAndReset();
};

return (
<Modal isOpen={isOpen} onClose={closeAndReset}>
<Modal isOpen={isOpen} onClose={closeAndReset} size={'xl'}>
<ModalOverlay />
<ModalContent>
<ModalHeader>
<Flex justifyContent="space-between">
{data ? 'Edit Admin' : 'Add Admin'}
<IconButton aria-label="Close" icon={<CloseIcon />} onClick={closeAndReset} />

<IconButton
variant={'ghost'}
aria-label="Close"
icon={<CloseIcon />}
onClick={closeAndReset}
/>
</Flex>
</ModalHeader>

<ModalBody>
<Text> First and Last Name </Text>
<Input
type="text"
name="name"
value={nameData}
onChange={(e) => setNameData(e.target.value)}
/>
<Text> Email </Text>
<Input
type="text"
name="email"
value={emailData}
onChange={(e) => setEmailData(e.target.value)}
/>
<Stack spacing={2}>
<Stack spacing={1}>
<Text> First and Last Name </Text>
<Input
type="text"
name="name"
value={nameData}
onChange={(e) => setNameData(e.target.value)}
/>
</Stack>
<Stack spacing={1}>
<Text> Email </Text>
<Input
type="text"
name="email"
value={emailData}
onChange={(e) => setEmailData(e.target.value)}
/>
</Stack>
</Stack>

{alertVisible && (
<Alert mt={4}>
<AlertIcon />
<Stack spacing={0}>
<AlertTitle>Missing necessary data!</AlertTitle>
<AlertDescription>Please make sure all textboxes are filed out</AlertDescription>
</Stack>
</Alert>
)}
</ModalBody>
{alertVisible && (
<Alert>
<AlertTitle>Missing necessary data!</AlertTitle>
<AlertDescription>Please make sure all textboxes are filed out</AlertDescription>
</Alert>
)}

<ModalFooter>
<Button mr={3} onClick={closeAndReset}>
Cancel
</Button>
<Button colorScheme="teal" mr={3} onClick={(e) => submitForm(e)}>
Submit
</Button>
<HStack spacing={3}>
<Button onClick={closeAndReset}>Cancel</Button>
<Button colorScheme="teal" onClick={(e) => submitForm(e)}>
Submit
</Button>
</HStack>
</ModalFooter>
</ModalContent>
</Modal>
Expand Down
15 changes: 14 additions & 1 deletion src/components/Authentication/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
HStack,
Image,
Input,
Text,
useToast,
VStack,
} from '@chakra-ui/react';
Expand Down Expand Up @@ -39,7 +40,7 @@ export const Login = () => {
const response = await backend.get(`/adminUser/${userCredential.user.email}`);
const user = response.data.at(0);

if (user.name) {
if (user?.name) {
navigate('/AdminDashboard');
} else {
navigate('/BusinessDashboard');
Expand Down Expand Up @@ -93,9 +94,21 @@ export const Login = () => {
Forgot Password?
</ChakraLink>
</Flex>

<Button colorScheme="teal" w="full" disabled={loading} onClick={(e) => handleSubmit(e)}>
Login
</Button>

<Text mt={4}>
Don't have an account? Sign up as an&nbsp;
<ChakraLink as={Link} to={'/signupadmin'} color="blue.500" fontWeight="semibold">
Admin
</ChakraLink>
&nbsp;or as a&nbsp;
<ChakraLink as={Link} to={'/signupbusiness'} color="blue.500" fontWeight="semibold">
Business
</ChakraLink>
</Text>
</VStack>
</Box>
</HStack>
Expand Down
5 changes: 2 additions & 3 deletions src/components/Authentication/Signup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ const Signup = ({ isAdmin }) => {
const [password, setPassword] = useState('');
const [confirmPassword, setConfirmPassword] = useState('');
const { signup } = useAuth();
// TODO: Setup Error Alert
// eslint-disable-next-line no-unused-vars

const [error, setError] = useState('');
const [loading, setLoading] = useState(false);
const toast = useToast();
Expand All @@ -37,7 +36,7 @@ const Signup = ({ isAdmin }) => {
duration: 3000,
isClosable: true,
});
}, [error]);
}, [error, toast]);

const handleSubmit = async (e) => {
e.preventDefault();
Expand Down
2 changes: 1 addition & 1 deletion src/components/BusinessDashboard/BusinessDashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export const BusinessDashboard = () => {
<Flex sx={pageStyle}>
<Flex justifyContent="space-between" alignItems="center" paddingY={4}>
<Heading color="teal" fontWeight="bold">
Welcome Back, {userName === '' ? '...' : userName}
Welcome Back{userName === '' ? '!' : `, ${userName}`}
</Heading>

<Button
Expand Down
2 changes: 1 addition & 1 deletion src/components/BusinessSetup/BusinessSetupPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { SetupSignup } from './SetupSignup';
import { Tips } from './Tips';
import { Welcome } from './Welcome';

export const BusinessSetupPage = ({ isAdmin }) => {
export const BusinessSetupPage = ({ isAdmin }: { isAdmin: boolean }) => {
const navigate = useNavigate();
const steps = [
{ title: 'First', description: 'Contact Info' },
Expand Down
6 changes: 5 additions & 1 deletion src/components/BusinessSetup/SetupSignup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,12 @@ export const SetupSignup = ({ admin, nextStep }: SetUpFirstFormProps) => {
setLoading(true);

if (admin) {
if (isAdmin) {
const response = await backend.get(`/adminUser/${email}`);
const user = response.data.at(0);

if (user) {
await signup({ email, password });

navigate('/AdminDashboard');
} else {
throw new Error(
Expand Down

0 comments on commit a72e7ef

Please sign in to comment.