Skip to content

Commit

Permalink
create Download page || add delete function on Download page b00tc4mp…
Browse files Browse the repository at this point in the history
  • Loading branch information
Abel Prieto committed Mar 10, 2024
1 parent 6f577be commit 25a7d00
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 10 deletions.
31 changes: 28 additions & 3 deletions staff/abel-prieto/PROYECT/HiInit/src/components/Download.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { useState, useEffect, useContext } from "react"
import { useNavigate } from "react-router-dom"
import Context from "../Context"
import logic from "../logic"
import { CommandBar, Pointer } from "../utils"

import Files from "./Files"

function Download() {

// POINTER, UKNOWN COMMAND & POINTER STATE
const [files, setFiles] = useState([])
const [commandText, setCommandText] = useState('')
const [uknownCommand, setUknownCommand] = useState(false)
const [list, setList] = useState(false)
const { pointer } = Pointer()

// VIEWS
Expand All @@ -19,9 +24,11 @@ function Download() {
const handleKeyPress = (event) => {
let commandText = document.getElementById('command').value

if ((commandText === 'desktop' || commandText === 'DESKTOP') && event.key === 'Enter') {
if ((commandText === 'DESKTOP' || commandText === 'desktop') && event.key === 'Enter') {
setUknownCommand(false)
navigate('/desktop')
} else if ((commandText === 'LS' || commandText === 'ls') && event.key === 'Enter') {
setList(true)
} else if ((commandText === 'EXIT' || commandText === 'exit') && event.key === 'Enter') {
handleLogout()
} else if (event.key === 'Enter') {
Expand All @@ -31,6 +38,7 @@ function Download() {

const handleKeyDown = () => {
setUknownCommand(false)
// setList(false)
}

document.addEventListener('keypress', handleKeyPress)
Expand All @@ -40,13 +48,28 @@ function Download() {
document.removeEventListener('keypress', handleKeyPress)
document.removeEventListener('keydown', handleKeyDown)
}
}, [navigate, uknownCommand])
}, [navigate, uknownCommand, setList])

// FILES
useEffect(() => {
try {
logic.retrieveFiles()
.then(files => {
setFiles(files)
})
.catch(error => {
handleError(error)
})
} catch (error) {
handleError(error)
}
}, [files])

return <>
<div className="container">

<p>~$</p>
<p id="client-error-upload">Entry 'ls' command to list all your save files: </p>
<p id="client-error-download">Entry 'ls' command to list all your save files: </p>

<br />

Expand All @@ -68,6 +91,8 @@ function Download() {
</span>
</>
)}

{list && files.map(file => <Files key={file.id} file={file} clientError={'#client-error-download'} />)}
</div>
</>
}
Expand Down
67 changes: 67 additions & 0 deletions staff/abel-prieto/PROYECT/HiInit/src/components/Files.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { useContext } from 'react'
import { useNavigate } from 'react-router-dom'
import Context from '../Context'
import logic from '../logic'
import Swal from 'sweetalert2'

function Files(props) {
const { handleError } = useContext(Context)
const navigate = useNavigate()
const file = props.file

// DELETE FILES
function handleDeleteFile(event) {
event.preventDefault()

Swal.fire({
title: "Are you want to delete it?",
text: "You won't be able to revert this!",
icon: "warning",
showCancelButton: true,
confirmButtonColor: "#3085d6",
cancelButtonColor: "#d33",
confirmButtonText: "Yes, delete it"
}).then((result) => {
if (result.isConfirmed) {

logic.deleteFile(file.id)
.then(() => {
Swal.fire({
title: "Deleted!",
text: "Your file has been deleted.",
icon: "success"
})
})
.catch(error => {
const clientError = document.querySelector(props.clientError)

clientError.innerText = `${error.message} ❌`
clientError.style.color = 'tomato'

handleError(error, navigate)

return
})
}
})

document.body.addEventListener('keydown', function () {
const clientError = document.querySelector(props.clientError)

clientError.innerText = 'Entry ls command to list all your save files: '
clientError.style.color = '#EBDBB2'
})
}

return <>
<article>
<ul>
<p>{file.name}</p>
<button id="download-file" className='button-form'>Download</button>
<button id="delete-file" className='button-form' onClick={handleDeleteFile}>Delete</button>
</ul>
</article>
</>
}

export default Files
16 changes: 12 additions & 4 deletions staff/abel-prieto/PROYECT/HiInit/src/index.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions staff/abel-prieto/PROYECT/HiInit/src/logic/deleteFile.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 deleteFile(fileId) {
validate.id(fileId, 'ID File')

const req = {
method: 'DELETE',
headers: {
Authorization: `Bearer ${session.token}`
}
}

return fetch(`${import.meta.env.VITE_HIINIT_APP}/download/delete/${String(fileId)}`, 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 deleteFile
12 changes: 10 additions & 2 deletions staff/abel-prieto/PROYECT/HiInit/src/logic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import isUserLoggedIn from './isUserLoggedIn.js'
import changeUserEmail from './changeUserEmail.js'
import changeUserPassword from './changeUserPassword.js'

import retrieveFiles from './retrieveFiles.js'
import uploadFile from './uploadFile.js'
import deleteFile from './deleteFile.js'

export {
loginUser,
Expand All @@ -18,7 +20,10 @@ export {
isUserLoggedIn,
changeUserEmail,
changeUserPassword,
uploadFile

retrieveFiles,
uploadFile,
deleteFile
}

const logic = {
Expand All @@ -30,7 +35,10 @@ const logic = {
isUserLoggedIn,
changeUserEmail,
changeUserPassword,
uploadFile

retrieveFiles,
uploadFile,
deleteFile
}

export default logic
28 changes: 28 additions & 0 deletions staff/abel-prieto/PROYECT/HiInit/src/logic/retrieveFiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import session from './session'
import { errors } from 'com'
const { SystemError } = errors

function retrieveFiles() {
const req = {
method: 'GET',
headers: {
Authorization: `Bearer ${session.token}`
}
}

return fetch('http://localhost:9001/download', 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(files => { return files })
})
}

export default retrieveFiles
14 changes: 13 additions & 1 deletion staff/abel-prieto/PROYECT/HiInit/src/utils/CommandBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default function CommandBar() {
const [username, setUsername] = useState('')
const [group, setGroup] = useState('')
const [role, setRole] = useState('')
const [roleColor, setRoleColor] = useState('')

const navigate = useNavigate()
const { handleError } = useContext(Context)
Expand All @@ -20,6 +21,12 @@ export default function CommandBar() {
setUsername(user.username)
setGroup(user.group)
setRole(user.role)

if (user.role[0] === 'user') {
setRoleColor('#306ed3')
} else if (user.role[0] === 'admin') {
setRoleColor('red')
}
})
.catch(error => {
handleError(error, navigate)
Expand All @@ -34,6 +41,11 @@ export default function CommandBar() {
setUsername(guest.username)
setGroup(guest.group)
setRole(guest.role)

if (guest.role[0] === 'guest') {
setRoleColor('#ff8037')
}

})
.catch(error => {
handleError(error, navigate)
Expand All @@ -50,7 +62,7 @@ export default function CommandBar() {
<span className="command-style--rest">@</span>
<span className="command-style--group">{group}</span>
<span className="command-style--rest">-</span>
<span className="command-style--user">{role}</span>
<span style={{ color: roleColor }}>{role}</span>
<span className="command-style--rest">:$</span>
</div>
</>
Expand Down
3 changes: 3 additions & 0 deletions staff/abel-prieto/PROYECT/HiInit/src/views/Credentials.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,13 @@ function Credentials() {

{help && (
<ul>
<p>GUEST ~$ Command types</p>
<p>- - - - - - - - - - - - - - - - - - - </p>
<li><p>help: <em>list user commands</em></p></li>
<li><p>exit: <em>get back to initial page</em></p></li>
<li><p>login: <em>entry your credentials</em></p></li>
<li><p>register: <em>create an account</em></p></li>
<p>- - - - - - - - - - - - - - - - - - - </p>
</ul>
)}
</div>
Expand Down
3 changes: 3 additions & 0 deletions staff/abel-prieto/PROYECT/HiInit/src/views/Desktop.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,15 @@ function Desktop() {

{help && (
<ul>
<p>USER ~$ Command types </p>
<p>- - - - - - - - - - - - - - - - - - - </p>
<li><p>help: <em>list user commands</em></p></li>
<li><p>desktop: <em>get back to your main field</em></p></li>
<li><p>profile: <em>settings account</em></p></li>
<li><p>upload: <em>save files on storage</em></p></li>
<li><p>download: <em>recover files from drive</em></p></li>
<li><p>exit: <em>get back to initial page</em></p></li>
<p>- - - - - - - - - - - - - - - - - - - </p>
</ul>
)}
</div>
Expand Down

0 comments on commit 25a7d00

Please sign in to comment.