-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented ability to deactivate student account
- Loading branch information
1 parent
5d85673
commit 2f5f6e6
Showing
10 changed files
with
283 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import React from "react"; | ||
import styled from "styled-components"; | ||
import { CapFirstLetterOfEachWord } from "../../modules/utils"; | ||
|
||
const UsersTableStyles = styled.div` | ||
.even { | ||
background-color: #bdba4b; | ||
} | ||
`; | ||
|
||
const UserTable = ({ users, activateUser, submitted }) => { | ||
const activateDeactivateUser = (userId) => { | ||
activateUser(userId); | ||
}; | ||
return ( | ||
<UsersTableStyles> | ||
<table className="table table-striped table-bordered"> | ||
<thead> | ||
<tr> | ||
<th scope="col" className="text-center"> | ||
Name | ||
</th> | ||
<th scope="col" className="text-center"> | ||
Reg Number | ||
</th> | ||
<th scope="col" className="text-center"> | ||
</th> | ||
|
||
<th scope="col" className="text-center"> | ||
Account Status | ||
</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{users.map(({ email, name, regNumber, active, id }) => { | ||
return ( | ||
<tr key={regNumber} className="even"> | ||
<td> | ||
<p>{CapFirstLetterOfEachWord(name)}</p> | ||
</td> | ||
<td> | ||
<p>{regNumber.toUpperCase()}</p> | ||
</td> | ||
<td> | ||
<p> | ||
<span>{email}</span> | ||
</p> | ||
</td> | ||
<td> | ||
{active ? ( | ||
<button | ||
disabled={submitted} | ||
className="btn btn-sm btn-danger" | ||
onClick={() => activateDeactivateUser(id)} | ||
> | ||
{submitted ? "please wait....." : "deactivate"} | ||
</button> | ||
) : ( | ||
<button | ||
disabled={submitted} | ||
className="btn btn-sm btn-success" | ||
onClick={() => activateDeactivateUser(id)} | ||
> | ||
{submitted ? "please wait....." : "activate"} | ||
</button> | ||
)} | ||
</td> | ||
</tr> | ||
); | ||
})} | ||
</tbody> | ||
</table> | ||
</UsersTableStyles> | ||
); | ||
}; | ||
|
||
export default UserTable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import { useLazyQuery, useMutation } from "@apollo/client"; | ||
import { ActivateDeactivateUser } from "../graphql/mutation"; | ||
import { SearchUserAccount } from "../graphql/queries"; | ||
import styled from "styled-components"; | ||
import Loading from "./common/loading"; | ||
import UsersTable from "./reuseableComponents/usersTable"; | ||
|
||
const SearchRecordsStyles = styled.div``; | ||
|
||
const SearchStudentAccount = () => { | ||
const [regNumber, setRegNumber] = useState(""); | ||
const [users, setUsers] = useState([]); | ||
const [errors, setErrors] = useState(null); | ||
const [noData, setNoData] = useState(false); | ||
const [submitted, setSubmitted] = useState(false); | ||
|
||
const [searchFunc, searchResult] = useLazyQuery(SearchUserAccount); | ||
const [activateFunction, activateResult] = useMutation( | ||
ActivateDeactivateUser | ||
); | ||
|
||
useEffect(() => { | ||
if (searchResult.data) { | ||
const users = searchResult.data.searchStudentAccount; | ||
|
||
if (users.length > 0) { | ||
setUsers(users); | ||
setNoData(false); | ||
} else { | ||
setNoData(true); | ||
setUsers([]); | ||
} | ||
} | ||
if (searchResult.error) { | ||
setErrors(searchResult.error); | ||
} | ||
}, [searchResult.data, searchResult.error]); | ||
|
||
useEffect(() => { | ||
if (activateResult.data) { | ||
setSubmitted(!submitted); | ||
window.alert("successful"); | ||
} | ||
if (activateResult.error) { | ||
setSubmitted(!submitted); | ||
setErrors(activateResult.error); | ||
} | ||
}, [activateResult.data, activateResult.error]); | ||
|
||
const performSearchInput = (e) => { | ||
const value = e.target.value; | ||
setRegNumber(value); | ||
if (value.length > 1) { | ||
//perform query search here | ||
setTimeout( | ||
searchFunc({ | ||
variables: { | ||
regNumber: value, | ||
}, | ||
}), | ||
500 | ||
); | ||
} | ||
}; | ||
const activateUser = async (userId) => { | ||
const confirmOperation = window.confirm("please confirm your action"); | ||
if (!confirmOperation) return; | ||
try { | ||
setSubmitted(!submitted); | ||
await activateFunction({ | ||
variables: { | ||
userId: userId, | ||
}, | ||
refetchQueries: [ | ||
{ | ||
query: SearchUserAccount, | ||
variables: { | ||
regNumber: regNumber, | ||
}, | ||
}, | ||
], | ||
}); | ||
} catch (error) {} | ||
}; | ||
return ( | ||
<SearchRecordsStyles> | ||
<div className="row"> | ||
<div className="col-md-12"> | ||
<div className="text-center"> | ||
{searchResult.loading && <Loading />} | ||
{errors && <p className="lead text-danger">{errors.message}</p>} | ||
</div> | ||
</div> | ||
</div> | ||
<div className="row"> | ||
<div className="col-md-6 offset-md-3"> | ||
<form> | ||
<div className="form-group"> | ||
<label htmlFor="regNumber">Reg Number</label> | ||
<input | ||
type="text" | ||
className="form-control" | ||
name="regNumber" | ||
aria-describedby="regNumber" | ||
value={regNumber} | ||
onChange={performSearchInput} | ||
/> | ||
</div> | ||
</form> | ||
</div> | ||
</div> | ||
<div className="row"> | ||
<div className="col-md-12"> | ||
<div className="text-center"> | ||
{noData && <p className="lead">Your query returned no result!</p>} | ||
|
||
{users.length > 0 && ( | ||
<UsersTable | ||
users={users} | ||
activateUser={activateUser} | ||
submitted={submitted} | ||
/> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
</SearchRecordsStyles> | ||
); | ||
}; | ||
|
||
export default SearchStudentAccount; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.