forked from b00tc4mp/isdi-parttime-202309
-
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.
implement SUDO page with all components: Delete User, Register Admin,…
… Create Group | add logics too b00tc4mp#382
- Loading branch information
Showing
11 changed files
with
191 additions
and
28 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
6 changes: 0 additions & 6 deletions
6
staff/abel-prieto/PROYECT/HiInit/src/components/ListUsers.jsx
This file was deleted.
Oops, something went wrong.
94 changes: 91 additions & 3 deletions
94
staff/abel-prieto/PROYECT/HiInit/src/components/RegisterAdmin.jsx
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 |
---|---|---|
@@ -1,6 +1,94 @@ | ||
import { useState, useContext } from 'react' | ||
import { useNavigate } from 'react-router-dom' | ||
import logic from '../logic' | ||
import Context from '../Context' | ||
|
||
function RegisterAdmin() { | ||
|
||
// FIELDS STATE | ||
const [showUsername, setShowUsername] = useState(true) | ||
const [showEmail, setShowEmail] = useState(false) | ||
const [showPassword, setShowPassword] = useState(false) | ||
|
||
const navigate = useNavigate() | ||
const { handleError } = useContext(Context) | ||
|
||
// SHOW EMAIL INPUT | ||
function showInputEmail() { | ||
setShowEmail(true) | ||
} | ||
|
||
// SHOW PASSWORD INPUT | ||
function showInputPassword() { | ||
setShowPassword(true) | ||
} | ||
|
||
// REGISTER FUNCTION | ||
function handleSubmit(event) { | ||
event.preventDefault() | ||
|
||
const clientError = document.querySelector('#client-error-register') | ||
|
||
const username = event.target.querySelector('#username').value | ||
const email = event.target.querySelector('#email').value | ||
const password = event.target.querySelector('#password').value | ||
|
||
try { | ||
return logic.registerAdmin(username, email, password) | ||
.then(() => { | ||
clientError.innerText = 'Admin succesfully created! ✅' | ||
clientError.style.color = 'green' | ||
}) | ||
} catch (error) { | ||
clientError.innerText = error.message | ||
clientError.style.color = 'tomato' | ||
|
||
handleError(error, navigate) | ||
} | ||
|
||
document.body.addEventListener('keydown', function () { | ||
clientError.innerText = 'Register - Create ADMIN account: ' | ||
clientError.style.color = '#EBDBB2' | ||
}) | ||
} | ||
|
||
|
||
export default function RegisterAdmin() { | ||
return <> | ||
<h1>Register Admin</h1> | ||
<div> | ||
<p>~$</p> | ||
<span> | ||
<form className="register-form" onSubmit={handleSubmit}> | ||
<p id="client-error-register">Register - Create ADMIN account: </p> | ||
|
||
<div className="space-between"> | ||
{showUsername && ( | ||
<div className="fields"> | ||
<label htmlFor="username"> <p style={{ color: '#18E3C8' }}>Username: </p></label> | ||
<input type="text" id="username" contentEditable="true" autoComplete="off" onChange={showInputEmail} /> | ||
</div> | ||
)} | ||
|
||
{showEmail && ( | ||
<div className="fields"> | ||
<label htmlFor="email"><p style={{ color: '#18E3C8' }}>Email: </p></label> | ||
<input type="text" id="email" contentEditable="true" autoComplete="off" onChange={showInputPassword} /> | ||
</div> | ||
)} | ||
|
||
{showPassword && ( | ||
<div className="fields"> | ||
<label htmlFor="password"><p style={{ color: '#18E3C8' }}>Password: </p></label> | ||
<input type="password" id="password" contentEditable="true" autoComplete="off" name="password" /> | ||
</div> | ||
)} | ||
</div> | ||
|
||
<button className="button-form" type="submit" >Send</button> | ||
</form> | ||
</span> | ||
|
||
</div > | ||
</> | ||
} | ||
} | ||
|
||
export default RegisterAdmin |
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,26 @@ | ||
import session from './session.js' | ||
import { errors, validate } from 'com' | ||
const { SystemError } = errors | ||
|
||
function deleteUser(userId) { | ||
validate.id(userId, 'ID User') | ||
|
||
const req = { | ||
method: 'DELETE', | ||
headers: { | ||
Authorization: `Bearer ${session.token}` | ||
} | ||
} | ||
|
||
return fetch(`${import.meta.env.VITE_HIINIT_APP}/users/delete/${String(userId)}`, req) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(res => { | ||
if (!res.ok) { | ||
return res.json() | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(body => { throw new errors[body.error](body.message) }) | ||
} | ||
}) | ||
} | ||
|
||
export default deleteUser |
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
28 changes: 28 additions & 0 deletions
28
staff/abel-prieto/PROYECT/HiInit/src/logic/retrieveAllUsers.js
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,28 @@ | ||
import session from './session.js' | ||
import { errors } from 'com' | ||
const { SystemError } = errors | ||
|
||
function retrieveAllUsers() { | ||
const req = { | ||
method: 'GET', | ||
headers: { | ||
Authorization: `Bearer ${session.token}` | ||
} | ||
} | ||
|
||
return fetch(`${import.meta.env.VITE_HIINIT_APP}/users/all`, req) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(res => { | ||
if (!res.ok) { | ||
return res.json() | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(body => { throw new errors[body.error](body.message) }) | ||
} | ||
|
||
return res.json() | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(users => { return users }) | ||
}) | ||
} | ||
|
||
export default retrieveAllUsers |
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