Skip to content

Commit

Permalink
implement SUDO page with all components: Delete User, Register Admin,…
Browse files Browse the repository at this point in the history
… Create Group | add logics too b00tc4mp#382
  • Loading branch information
abelpriem committed Mar 21, 2024
1 parent b454338 commit 7332f70
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 28 deletions.
23 changes: 19 additions & 4 deletions staff/abel-prieto/PROYECT/HiInit/src/components/DeleteUser.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useNavigate } from 'react-router-dom'
import Context from '../Context'
import logic from '../logic'
import { CommandBar, Pointer } from '../utils'
import ListUsers from './ListUsers'
import Users from './Users'

function DeleteUsers() {
const [users, setUsers] = useState([])
Expand Down Expand Up @@ -32,6 +32,10 @@ function DeleteUsers() {
setList(true)
} else if ((commandText === 'EXIT' || commandText === 'exit') && event.key === 'Enter') {
setList(false)
handleLogout()
} else if ((commandText === 'SUDO' || commandText === 'sudo') && event.key === 'Enter') {
setList(false)
navigate('/administrator')
} else if (event.key === 'Enter') {
setUknownCommand(!uknownCommand)
}
Expand All @@ -58,7 +62,7 @@ function DeleteUsers() {
if (fetchingUsers || list) {
const fetchDeleted = async () => {
try {
const result = await logic.retrieveUser()
const result = await logic.retrieveAllUsers()
setUsers(result)
setFetchingUsers(false)

Expand All @@ -68,15 +72,26 @@ function DeleteUsers() {
color: 'tomato'
})

handleError(error, navigate)
setFetchingUsers(false)
handleError(error, navigate)
}
}

fetchDeleted()
}
}, [fetchingUsers, list, handleError, navigate])

// LOGOUT VIEW
function handleLogout() {
logic.logoutUser(error => {
if (error) {
handleError(error, navigate)
}

navigate('/')
})
}

return (
<div className="container">
<p>~$</p>
Expand All @@ -103,7 +118,7 @@ function DeleteUsers() {
</>
)}

{list && users.map(user => <ListUsers key={user.id} file={user} clientError={'#client-error-download'} />)}
{list && users.map(user => <Users key={user.id} user={user} clientError={'#client-error-download'} />)}
</div>
)
}
Expand Down
6 changes: 0 additions & 6 deletions staff/abel-prieto/PROYECT/HiInit/src/components/ListUsers.jsx

This file was deleted.

94 changes: 91 additions & 3 deletions staff/abel-prieto/PROYECT/HiInit/src/components/RegisterAdmin.jsx
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
8 changes: 4 additions & 4 deletions staff/abel-prieto/PROYECT/HiInit/src/components/Users.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function Users(props) {

Swal.fire({
title: "Are you want to delete it?",
text: "This action will delete this user from BBD...",
text: "This action will delete this user, files...",
icon: "warning",
showCancelButton: true,
confirmButtonColor: "#3085d6",
Expand All @@ -25,7 +25,7 @@ function Users(props) {
}).then((result) => {
if (result.isConfirmed) {

logic.deleteFile(file.id)
logic.deleteUser(user.id)
.then(() => {
Swal.fire({
title: "Deleted!",
Expand All @@ -48,10 +48,10 @@ function Users(props) {
}

return <>
<article>
<article key={user._id}>
<ul>
<p>{user.username}</p>
<button id="delete-file" className='button-form' onClick={handleDeleteUser}>Delete</button>
<button id="delete-file" className='button-delete' onClick={handleDeleteUser}>Delete</button>
</ul>
</article>
</>
Expand Down
4 changes: 2 additions & 2 deletions staff/abel-prieto/PROYECT/HiInit/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Password from './Password'
import RegisterAdmin from './RegisterAdmin'
import DeleteUser from './DeleteUser'
import CreateGroup from './CreateGroup'
import ListUsers from './ListUsers'
import Users from './Users'

export {
Login,
Expand All @@ -21,5 +21,5 @@ export {
RegisterAdmin,
DeleteUser,
CreateGroup,
ListUsers
Users
}
26 changes: 26 additions & 0 deletions staff/abel-prieto/PROYECT/HiInit/src/logic/deleteUser.js
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
13 changes: 11 additions & 2 deletions staff/abel-prieto/PROYECT/HiInit/src/logic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import uploadFile from './uploadFile.js'
import deleteFile from './deleteFile.js'
import downloadFile from './downloadFile.js'

import deleteUser from './deleteUser.js'
import retrieveAllUsers from './retrieveAllUsers.js'

export {
loginUser,
registerUser,
Expand All @@ -25,7 +28,10 @@ export {
retrieveFiles,
uploadFile,
deleteFile,
downloadFile
downloadFile,

retrieveAllUsers,
deleteUser
}

const logic = {
Expand All @@ -41,7 +47,10 @@ const logic = {
retrieveFiles,
uploadFile,
deleteFile,
downloadFile
downloadFile,

retrieveAllUsers,
deleteUser
}

export default logic
28 changes: 28 additions & 0 deletions staff/abel-prieto/PROYECT/HiInit/src/logic/retrieveAllUsers.js
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
2 changes: 1 addition & 1 deletion staff/abel-prieto/PROYECT/HiInit/src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ReactDOM.createRoot(document.getElementById('root')).render(
<Route path="/upload" element={<Upload />} />
<Route path="/download" element={<Download />} />
<Route path="/profile/*" element={<Profile />} />
<Route path="/administrator" element={<Sudo />} />
<Route path="/administrator/*" element={<Sudo />} />
<Route path="/create-admin" element={<RegisterAdmin />} />
<Route path="/delete-user" element={<DeleteUser />} />
<Route path="/create-group" element={<CreateGroup />} />
Expand Down
2 changes: 1 addition & 1 deletion staff/abel-prieto/PROYECT/HiInit/src/views/Desktop.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ function Desktop() {
setRole(user.role)
setFetchingUser(!fetchingUser)
} catch (error) {
handleError(error, navigate)
setFetchingUser(!fetchingUser)
handleError(error, navigate)
}
}

Expand Down
13 changes: 8 additions & 5 deletions staff/abel-prieto/PROYECT/HiInit/src/views/Sudo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,19 @@ export default function Sudo() {
} else if ((commandText === '1') && event.key === 'Enter') {
setUknownCommand(false)
setMenu(false)
navigate('/create-admin')
navigate('/administrator/create-admin')
} else if ((commandText === '2') && event.key === 'Enter') {
setUknownCommand(false)
setMenu(false)
navigate('/delete-user')
} else if ((commandText === '3') && event.key === 'Enter') {
setUknownCommand(false)
setMenu(false)
navigate('/create-group')
navigate('/administrator/create-group')
} else if ((commandText === 'SUDO' || commandText === 'sudo') && event.key === 'Enter') {
setUknownCommand(false)
setMenu(true)
navigate('/administrator')
} else if (event.key === 'Enter') {
setUknownCommand(!uknownCommand)
}
Expand Down Expand Up @@ -72,7 +76,7 @@ export default function Sudo() {
return <>
<div className="container">
<p>~$</p>
<p>ADMINISTRATOR: Select one of all different options. Entry DESKTOP or EXIT to return </p>
<p>ADMINISTRATOR. Please, choose an option. Entry DESKTOP, SUDO or EXIT to return </p>

<br />

Expand All @@ -88,7 +92,6 @@ export default function Sudo() {

<Routes>
<Route path="/create-admin" element={<RegisterAdmin />} />
<Route path="/delete-user" element={<DeleteUser />} />
<Route path="/create-group" element={<CreateGroup />} />
</Routes>

Expand All @@ -107,7 +110,7 @@ export default function Sudo() {

{uknownCommand && (
<span>
<p>shell: command not found: '{commandText}'. Entry DESKTOP or EXIT</p>
<p>shell: command not found: '{commandText}'. Entry SUDO, DESKTOP or EXIT</p>
</span>
)}
</div>
Expand Down

0 comments on commit 7332f70

Please sign in to comment.