Skip to content
Snippets Groups Projects
Verified Commit 5b0efe8c authored by Defendi Alberto's avatar Defendi Alberto
Browse files

Fetch roles from api and forward onClick.

parent a59bd356
No related branches found
No related tags found
2 merge requests!56Refined auth flow and new website pages.,!45Polished auth flow including ReservedRoute and role selection.
import React, { FC, useContext, useEffect, useState } from 'react'; import React, { FC, useEffect, useState } from 'react';
import axios from 'axios';
import Button from '@material-ui/core/Button'; import Button from '@material-ui/core/Button';
import { AuthContext } from '../AuthContext'; import { getRoles } from 'api/getRoles';
import { setRole } from 'api/setRole';
const choseAndForward = (e: unknown): void => { import { useHistory } from 'react-router-dom';
// Do nothing. import { AuthRoutes } from 'api/routes';
};
/** /**
* Screen that let's users decide role between available roles. * Page that let's users decide role between available roles.
* This is intended to use when a user has more than one role. * This is intended to use when a user has more than one role.
* @returns ChoseRole component. * @returns ChoseRole component.
*/ */
export const ChoseRole: FC = () => { export const ChoseRole: FC = () => {
const [userRoles, setUserRoles] = useState<Array<string>>(['']); const history = useHistory();
const [userRoles, setUserRoles] = useState<string[]>(['hello', 'world']);
const choseAndForward = (role: string): void => {
// Set role in the server.
setRole(role);
// Push to homepage.
history.push(`${AuthRoutes.dashboard}${AuthRoutes.home}`);
};
useEffect(() => { useEffect(() => {
const getUserRoles = async (): Promise<unknown> => { getRoles().then((fetchedRoles) => setUserRoles(fetchedRoles));
const response = await axios('/api/web/get_role'); }, []);
setUserRoles(response.data.token);
return null;
};
getUserRoles();
}, [userRoles]);
const { role } = useContext(AuthContext);
return ( return (
<div data-testid="ChoseRole"> <div data-testid="ChoseRole">
<Button {userRoles ? (
variant="outlined" userRoles.map((role) => (
color="default" <Button
onClick={(e) => choseAndForward(e)} variant="outlined"
> color="default"
{role} key={role}
</Button> onClick={() => choseAndForward(role)}
>
{role}
</Button>
))
) : (
<h1>No roles were returned.</h1>
)}
</div> </div>
); );
}; };
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment