Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

173 feature rough department and position onboarding page #185

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
34 changes: 28 additions & 6 deletions apps/web/src/context/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { loginRequest } from '@web/authConfig';
import { callMsGraph } from '@web/graph';
import { useMutation } from '@tanstack/react-query';
import { RegisterEmployeeDto } from '@web/client';

// Reference: https://blog.finiam.com/blog/predictable-react-authentication-with-the-context-api

export const AuthContext = createContext<AuthContextType>(
Expand Down Expand Up @@ -132,19 +131,41 @@ export const AuthProvider = ({ children }: any) => {
}
};

// Register the user in the database
// Direct registering a user in the database to either fill positon + department
// fields, or proceed to completeRegistration
const register = async (email: string, password: string) => {
const userData = await requestProfileData();
const departmentName = userData.department || 'Default Department';
const positionName = userData.jobTitle || 'Default Position';
const departmentName = userData.department;
const positionName = userData.jobTitle;

if (!departmentName || !positionName) {
router.push('/register');
} else {
completeRegistration(
userData,
email,
password,
departmentName,
positionName,
);
}
};

// Register a user with provided information to the database
const completeRegistration = (
userData: any,
email: string,
password: string,
department: string,
position: string,
) => {
const employee: RegisterEmployeeDto = {
email: email,
password: password,
firstName: userData.givenName || userData.displayName.split(' ')[0],
lastName: userData.surname || userData.displayName.split(' ')[1],
departmentName: departmentName,
positionName: positionName,
departmentName: department,
positionName: position,
};

registerEmployeeMutation.mutate(employee, {
Expand Down Expand Up @@ -180,6 +201,7 @@ export const AuthProvider = ({ children }: any) => {
loading,
error,
login,
completeRegistration,
logout,
}),
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
7 changes: 7 additions & 0 deletions apps/web/src/context/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,12 @@ export interface AuthContextType {
loading: boolean;
error?: any;
login: (email: string, password: string) => void;
completeRegistration: (
userData: any,
email: string,
password: string,
position: string,
department: string,
) => void;
logout: () => void;
}
2 changes: 1 addition & 1 deletion apps/web/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default function App({
OpenAPI.CREDENTIALS = 'include';
OpenAPI.WITH_CREDENTIALS = true;

const excludeLayoutPaths = ['/signin'];
const excludeLayoutPaths = ['/signin', '/register'];

const head = (
<Head>
Expand Down
147 changes: 147 additions & 0 deletions apps/web/src/pages/register.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { Button, Flex } from '@chakra-ui/react';
import { Heading } from '@chakra-ui/react';
import { Select } from '@chakra-ui/react';
import { DepartmentsService, PositionsService } from '@web/client';
import { DepartmentEntity } from '@web/client';
import { PositionEntity } from '@web/client';
import { useEffect, useState } from 'react';
import { useRouter } from 'next/router';
import { useAuth } from '@web/hooks/useAuth';

export default function Register(
userData: any,
email: string,
password: string,
departmentName: any,
positionName: any,
) {
const [positions, setPositions] = useState<PositionEntity[]>([]);
const [departments, setDepartments] = useState<DepartmentEntity[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [currentDepartment, setCurrentDepartment] = useState(departmentName);
const [currentPosition, setCurrentPosition] = useState(positionName);
const router = useRouter();
const { completeRegistration } = useAuth();

// fetch list of all positions
useEffect(() => {
async function fetchPositions() {
setIsLoading(true);
try {
const request: PositionEntity[] =
await PositionsService.positionsControllerFindAll(1000);
setPositions(request);
} catch (error) {
console.log(error);
} finally {
setIsLoading(false);
}
}
fetchPositions();
}, []);

// fetch list of all departments
useEffect(() => {
async function fetchDepartments() {
setIsLoading(true);
try {
const request: DepartmentEntity[] =
await DepartmentsService.departmentsControllerFindAll(1000);
setDepartments(request);
} catch (error) {
console.log(error);
} finally {
setIsLoading(false);
console.log(departments);
}
}
fetchDepartments();
}, []);

// If loading, wait to render
if (isLoading) {
return <div>Loading...</div>;
}

// when button is submitted to finalize department and position,
// register employee with current position and department
// and route to home page
const clickResponse = () => {
// complete registration
completeRegistration(
userData,
email,
password,
currentDepartment,
currentPosition,
);
// redirect to main page
router.push('/');
};

return (
<>
<Heading as="h2" textColor="#363940" fontSize="30px" padding="50px">
Hi there, it looks like you are not registered yet.
</Heading>
<Heading as="h3" textColor="#363940" pl="50px">
Please specify your department and position below
</Heading>
<Flex
padding="50px"
width="80vw"
justifyContent="center"
alignItems="center"
>
<Heading as="h2" textColor="#363940" fontSize="24px" width="200px">
Select Department
</Heading>
<Select
defaultValue={departmentName ? departmentName : 'Select Department'}
disabled={departmentName}
onChange={(e) => setCurrentDepartment(e.target.value)}
>
{departments.map((department: DepartmentEntity, index: number) => {
return (
<option key={index} value={department.name}>
{department.name}
</option>
);
})}
</Select>
</Flex>
<Flex
padding="50px"
width="80vw"
justifyContent="center"
alignItems="center"
>
<Heading as="h2" textColor="#363940" fontSize="24px" width="200px">
Select Position
</Heading>
<Select
id="positionDropdown"
defaultValue={positionName ? positionName : 'Select Position'}
disabled={positionName}
onChange={(e) => setCurrentPosition(e.target.value)}
>
{positions.map((position: PositionEntity, index: number) => {
return (
<option key={index} value={position.name}>
{position.name}
</option>
);
})}
</Select>
</Flex>
<Button
alignSelf="center"
marginLeft="50px"
onClick={clickResponse}
disabled={!(currentDepartment && currentPosition)}
>
Submit
</Button>
</>
);
}
Loading