Skip to content

Commit

Permalink
added ability to create user account
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiebones committed Mar 27, 2021
1 parent 35e679c commit 5d85673
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 20 deletions.
34 changes: 15 additions & 19 deletions client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ import state from "./applicationState";
import EditHostelDetails from "./components/editHostelDetails";
import SendMessageToStudent from "./components/sendMessageToStudents";
import CustomNavbar from "./components/common/customNavbar";
import AdminViewTransaction from "./components/adminViewTransaction"
import AdminViewTransaction from "./components/adminViewTransaction";
import GlobalStyle from "./globalStyles";
import CreateStaffUserAccountByAdmin from "./components/adminCreateUserAccount";
/**
imports of page components ends here
*/
Expand All @@ -80,7 +81,6 @@ const App = (props) => {
const [authenticated, setAuthenticated] = useRecoilState(state.authState);
const [currentUser, setCurrentUser] = useRecoilState(state.currentUserState);


useEffect(() => {
if (!currentUser) {
//load the stuffs from the store if it exists
Expand Down Expand Up @@ -128,7 +128,6 @@ const App = (props) => {
/>
)}
/>

<AuthorizedComponent
component={ConfirmTransaction}
authenticated={authenticated}
Expand All @@ -137,7 +136,6 @@ const App = (props) => {
exact
path="/confirm_transaction"
/>

{/* student route starts here*/}
<AuthorizedComponent
component={ConfirmTransaction}
Expand All @@ -147,7 +145,6 @@ const App = (props) => {
exact
path="/confirm_transaction"
/>

<AuthorizedComponent
path="/make_payment"
exact
Expand All @@ -157,7 +154,6 @@ const App = (props) => {
{...props}
authorizedRole={["student"]}
/>

<AuthorizedComponent
path="/hostel_payment"
component={MakeRemitaPaymentUsingRRR}
Expand All @@ -167,7 +163,6 @@ const App = (props) => {
authorizedRole={["student"]}
exact
/>

<AuthorizedComponent
path="/print_allocation_receipt"
exact
Expand Down Expand Up @@ -213,7 +208,6 @@ const App = (props) => {
authorizedRole={["student"]}
{...props}
/>

<AuthorizedComponent
path="/print_receipt/:rrr"
exact
Expand All @@ -223,9 +217,7 @@ const App = (props) => {
authorizedRole={["student"]}
{...props}
/>

{/* students route ends here*/}

{/* <Route
exact
path="/confirm_transaction"
Expand Down Expand Up @@ -261,20 +253,28 @@ const App = (props) => {
component={AdminViewTransaction}
authenticated={authenticated}
currentUser={currentUser}
authorizedRole={["super-admin"]}
authorizedRole={["super-admin", "admin"]}
{...props}
/>

<AuthorizedComponent
path="/admin/dashboard"
path="/admin/view_transactions"
exact
component={AdminViewTransaction}
authenticated={authenticated}
currentUser={currentUser}
authorizedRole={["super-admin", "admin"]}
{...props}
/>

<AuthorizedComponent
path="/admin/create_staff_account"
exact
component={AdminDashboard}
component={CreateStaffUserAccountByAdmin}
authenticated={authenticated}
currentUser={currentUser}
authorizedRole={["super-admin"]}
{...props}
/>

<AuthorizedComponent
path="/admin/edit_hostel"
exact
Expand All @@ -292,7 +292,6 @@ const App = (props) => {
currentUser={currentUser}
{...props}
/> */}

<AuthorizedComponent
path="/admin/confirm_allocation"
exact
Expand All @@ -302,7 +301,6 @@ const App = (props) => {
authorizedRole={["super-admin"]}
{...props}
/>

<AuthorizedComponent
path="/admin/send_message"
exact
Expand All @@ -312,7 +310,6 @@ const App = (props) => {
authorizedRole={["super-admin"]}
{...props}
/>

<AuthorizedComponent
path="/create_new_session"
exact
Expand Down Expand Up @@ -421,7 +418,6 @@ const App = (props) => {
authorizedRole={["super-admin"]}
{...props}
/>

{/* Admin routes ends here */}
</React.Fragment>
</div>
Expand Down
175 changes: 175 additions & 0 deletions client/src/components/adminCreateUserAccount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import React, { useState, useEffect } from "react";
import { useMutation } from "@apollo/client";
import { CreateStaffUserAccountByAdmin } from "../graphql/mutation";
import styled from "styled-components";

const AdminCreateUserAccountStyles = styled.div`
.title {
}
`;

const AdminCreateUserAccount = () => {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [role, setRole] = useState("0");
const [name, setName] = useState("");
const [submitted, setSubmitted] = useState(false);
const [errors, setErrors] = useState(null);
const [createAccount, createAccountResult] = useMutation(
CreateStaffUserAccountByAdmin
);

useEffect(() => {
if (createAccountResult.error) {
setErrors(createAccountResult.error);
setSubmitted(!submitted);
}
if (createAccountResult.data) {
window.alert("Account created successfully");
setRole("0");
setName("");
setSubmitted(!submitted);
setPassword("");
setEmail("");
setErrors(null);
}
}, [createAccountResult.data, createAccountResult.error]);

const onInputChange = (e) => {
const value = e.target.value;
const name = e.target.name;
switch (name) {
case "name":
setName(value);
break;
case "email":
setEmail(value);
break;
case "password":
setPassword(value);
break;
case "role":
if (value !== "0") {
setRole(value);
}
break;
}
};

const createUserAccountFunction = async (e) => {
e.preventDefault();
//gather the variables here and
if (!name) {
window.alert("name of the account owner is required");
return;
}
if (!email) {
window.alert("email of the account owner is required");
return;
}
if (!password) {
window.alert("password of the account owner is required");
return;
}
if (!role) {
window.alert("the account owner role is a required field");
return;
}
const confirmThings = window.confirm(
`You are about creating an account with the following details \n Name: ${name} \n Email: ${email} \n Password: ${password} \n Staff Role: ${role}`
);
if (!confirmThings) return;
try {
setSubmitted(!submitted);
await createAccount({
variables: {
email,
password,
name,
role,
},
});
} catch (error) {}
};
return (
<AdminCreateUserAccountStyles>
<div className="row">
<div className="col-md-6 offset-md-3">
<div className="title">
<h3 className="text-info text-center">Create Staff User Account</h3>
</div>
<div className="text-center">
{errors && <p className="lead text-danger">{errors.message}</p>}
</div>
<form onSubmit={createUserAccountFunction}>
<div className="form-group">
<label htmlFor="name">Email</label>
<input
type="email"
className="form-control"
name="email"
aria-describedby="email"
value={email}
onChange={onInputChange}
/>
</div>

<div className="form-group">
<label htmlFor="name">Name</label>
<input
type="text"
className="form-control"
name="name"
aria-describedby="name"
value={name}
onChange={onInputChange}
/>
</div>

<div className="form-group">
<label htmlFor="password">password</label>
<input
type="text"
className="form-control"
id="password"
onChange={onInputChange}
name="password"
value={password}
/>
</div>

<div className="form-group">
<label htmlFor="password">account role</label>
<select
className="form-control"
name="role"
onChange={onInputChange}
value={role}
>
<option value="0">select user role</option>
<option value="admin">admin</option>
<option value="super-admin">
super administrator (access to every thing)
</option>
<option value="supervisor">supervisor</option>
</select>
</div>

<div className="text-center">
<button
type="submit"
className="btn btn-primary btn-lg"
disabled={submitted}
>
{submitted ? "creating account....." : "create user account"}
</button>
</div>
</form>
</div>
</div>
</AdminCreateUserAccountStyles>
);
};

export default AdminCreateUserAccount;
23 changes: 23 additions & 0 deletions client/src/components/navbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,29 @@ const Navbar = ({ authenticated, currentUser }) => {
Transactions
</StyledLink>
</li>

<li className="nav-item dropdown">
<a
className="nav-link dropdown-toggle"
data-toggle="dropdown"
href="#"
role="button"
aria-haspopup="true"
aria-expanded="false"
>
User Accounts
</a>

<div className="dropdown-menu">
<StyledLink
exact
to="/admin/create_staff_account"
className="nav-link"
>
Create Staff Account
</StyledLink>
</div>
</li>
</React.Fragment>
)}

Expand Down
19 changes: 18 additions & 1 deletion client/src/graphql/mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,23 @@ const ConfirmStudentTransaction = gql`
}
`;

//
//createStaffUserAccountByAdmin

const CreateStaffUserAccountByAdmin = gql`
mutation createStaffUserAccountByAdmin(
$email: String!
$name: String!
$password: String!
$role: String!
) {
createStaffUserAccountByAdmin(
email: $email
name: $name
password: $password
role: $role
)
}
`;

export {
SendSMSToStudents,
Expand Down Expand Up @@ -386,4 +402,5 @@ export {
PlaceStudentInHoldBedSpace,
DashStudentFreeRoom,
SimulateRemitaTransaction,
CreateStaffUserAccountByAdmin,
};
Loading

0 comments on commit 5d85673

Please sign in to comment.