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

Update registration page and unregistered user handling #160

Merged
merged 3 commits into from Nov 5, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/components/data/DataProvider.tsx
Expand Up @@ -3,6 +3,7 @@ import { DataFile } from "../../model/file";
import { AuthContext } from "../identity/AuthProvider";
import { getFiles } from "../../api/api";
import Loader from "../generic/Loader";
import { UserContext } from "../identity/UserProvider";

export interface IDataContext {
files: DataFile[];
Expand All @@ -16,14 +17,16 @@ export const DataContext = React.createContext<IDataContext | undefined>(

export const DataProvider: React.FunctionComponent = props => {
const authContext = React.useContext(AuthContext);
const userContext = React.useContext(UserContext);

const [files, setFiles] = React.useState<DataFile[] | undefined>(undefined);
const [dataStatus, setDataStatus] = React.useState<
IDataContext["dataStatus"]
>("fetching");

const refreshData = () => {
if (authContext) {
// Only try to fetch data if user has been approved
if (authContext && userContext && userContext.role) {
setDataStatus("fetching");
getFiles(authContext.idToken)
.then(fs => {
Expand All @@ -34,7 +37,9 @@ export const DataProvider: React.FunctionComponent = props => {
}
};

React.useEffect(refreshData, []);
React.useEffect(refreshData, [
authContext && userContext && userContext.role
]);

const value = files && { files, dataStatus, refreshData };

Expand Down
118 changes: 62 additions & 56 deletions src/components/identity/Register.tsx
Expand Up @@ -30,13 +30,18 @@ export const useRegisterStyles = makeStyles({
progress: {
textAlign: "center",
paddingTop: 30
}
},
body: { width: 700, margin: "auto", paddingTop: 25 },
text: { fontSize: 18 }
});

export default function Register() {
const classes = useRegisterStyles();
const authData = React.useContext(AuthContext);

const [hasPrepopulated, setHasPrepopulated] = React.useState<boolean>(
false
);
const [state, setEntireState] = React.useState({
first_n: "",
last_n: "",
Expand All @@ -49,29 +54,32 @@ export default function Register() {
});

const setState = React.useCallback(
(partialState: any) => setEntireState({ ...state, ...partialState }),
[state]
(partialState: any) => {
setEntireState({ ...state, ...partialState });
},
// React's linter complains about spread elements in dependency arrays,
// but we bypass in this case to avoid having a very long dependency array.
// eslint-disable-next-line
[...Object.values(state), state]
);

React.useEffect(() => {
if (authData) {
setState({ token: authData.idToken, ...authData.user });
if (!hasPrepopulated) {
setState({ ...authData.user, token: authData.idToken });
setHasPrepopulated(true);
} else {
setState({ token: authData.idToken });
}
}
}, [authData, setState]);
}, [authData, setState, hasPrepopulated]);

function handleFirstNameChange(event: React.ChangeEvent<HTMLInputElement>) {
setState({ first_n: event.target.value });
}

function handleLastNameChange(event: React.ChangeEvent<HTMLInputElement>) {
setState({ last_n: event.target.value });
}

function handleOrganizationChange(
event: React.ChangeEvent<HTMLSelectElement>
) {
setState({ organization: event.target.value });
}
const handleChange = (
field: string,
event: React.ChangeEvent<{ value: any }>
) => {
setState({ [field]: event.target.value });
};

function handleClick() {
const firstNameError: boolean = !state.first_n;
Expand Down Expand Up @@ -102,7 +110,7 @@ export default function Register() {
if (!authData) {
return (
<>
<div className={classes.header}>Registration</div>
<div className={classes.header}>CIDC Registration Request</div>
<div className={classes.progress}>
<CircularProgress />
</div>
Expand All @@ -112,55 +120,51 @@ export default function Register() {

return (
<div>
<div className={classes.header}>Registration</div>
<div style={{ width: "25%", margin: "auto", paddingTop: 25 }}>
<Typography
style={{ fontSize: 18, width: "100%", margin: "auto" }}
>
Please complete your CIMAC-CIDC Data Portal registration
request below.
<div className={classes.header}>CIDC Registration Request</div>
<div className={classes.body}>
<Typography className={classes.text}>
If you are interested in accessing the CIMAC-CIDC Data
Portal, please complete your registration request below.
</Typography>
<Grid container={true} spacing={3}>
<Grid item={true} xs={12}>
<Grid container={true} spacing={3} direction="column">
<Grid item={true}>
<TextField
label="Login Email"
style={{ minWidth: 420 }}
label="Contact Email"
value={state.email}
disabled={true}
fullWidth={true}
margin="normal"
variant="outlined"
/>
</Grid>
<Grid item={true} xs={12}>
<Grid item={true}>
<TextField
label="First Name"
style={{ minWidth: 420 }}
fullWidth={true}
value={state.first_n}
onChange={handleFirstNameChange}
onChange={e => handleChange("first_n", e)}
margin="normal"
variant="outlined"
required={true}
error={state.firstNameError}
/>
</Grid>
<Grid item={true} xs={12}>
<Grid item={true}>
<TextField
label="Last Name"
style={{ minWidth: 420 }}
fullWidth={true}
value={state.last_n}
onChange={handleLastNameChange}
onChange={e => handleChange("last_n", e)}
margin="normal"
variant="outlined"
required={true}
error={state.lastNameError}
/>
</Grid>
<Grid item={true} xs={12}>
<Grid item={true}>
<FormControl
variant="outlined"
fullWidth
required={true}
margin="normal"
error={state.organizationError}
Expand All @@ -169,9 +173,7 @@ export default function Register() {
<InputLabel>Organization</InputLabel>
<Select
value={state.organization}
onChange={(e: any) =>
handleOrganizationChange(e)
}
onChange={e => handleChange("organization", e)}
input={<OutlinedInput labelWidth={100} />}
>
<MenuItem value="EMPTY">Please select</MenuItem>
Expand All @@ -193,22 +195,26 @@ export default function Register() {
</Select>
</FormControl>
</Grid>
<Grid item={true} xs={12}>
<div
style={{
display: "flex",
justifyContent: "center"
}}
>
<Button
variant="contained"
color="primary"
// tslint:disable-next-line:jsx-no-lambda
onClick={() => handleClick()}
>
Register
</Button>
</div>
<Grid item={true}>
<Grid container justify="space-between">
<Grid item>
<Button
variant="contained"
onClick={() => history.push("/logout")}
>
Cancel
</Button>
</Grid>
<Grid item>
<Button
variant="contained"
color="primary"
onClick={() => handleClick()}
>
Register
</Button>
</Grid>
</Grid>
</Grid>
</Grid>
</div>
Expand Down
30 changes: 17 additions & 13 deletions src/components/identity/Unactivated.tsx
Expand Up @@ -3,6 +3,7 @@ import { Typography } from "@material-ui/core";
import history from "./History";
import { useUserContext } from "./UserProvider";
import { useRegisterStyles } from "./Register";
import ContactAnAdmin from "../generic/ContactAnAdmin";

export default function Unactivated() {
const classes = useRegisterStyles();
Expand All @@ -15,19 +16,22 @@ export default function Unactivated() {

return (
<div data-testid="unactivated-message">
<div className={classes.header}>Registration</div>
<Typography
style={{
fontSize: 20,
width: "70%",
margin: "auto",
paddingTop: 25
}}
>
Thank for you registering for the CIMAC-CIDC Data Portal. We
will email you when your authorization request has been
completed.
</Typography>
<div className={classes.header}>CIDC Registration Request</div>
<div className={classes.body}>
<Typography paragraph className={classes.text}>
Thank for you for submitting a registration request for the
CIMAC-CIDC Data Portal.
</Typography>
<Typography paragraph className={classes.text}>
If you are a member of the CIMAC Network, we will issue an
jacoblurye marked this conversation as resolved.
Show resolved Hide resolved
account to you as data for your trial(s) become available on
the site.
</Typography>
<Typography paragraph className={classes.text}>
In the meantime, feel free to <ContactAnAdmin lower /> with
any questions you may have.
</Typography>
</div>
</div>
);
}
2 changes: 1 addition & 1 deletion src/components/identity/UserProvider.tsx
Expand Up @@ -60,7 +60,7 @@ const UserProvider: React.FunctionComponent<RouteComponentProps> = props => {
history.replace("/register");
}
});
} else if (!permissions) {
} else if (!permissions && user.role) {
getPermissionsForUser(idToken, user.id).then(perms =>
setPermissions(perms)
);
Expand Down