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.
create Download page || add delete function on Download page b00tc4mp…
- Loading branch information
Abel Prieto
committed
Mar 10, 2024
1 parent
6f577be
commit 25a7d00
Showing
9 changed files
with
190 additions
and
10 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
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 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 |
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/retrieveFiles.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' | ||
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 |
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