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.
add new route with EditResource component; fetch individual resource …
…by id and start populating input b00tc4mp#484
- Loading branch information
Showing
11 changed files
with
423 additions
and
45 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
32 changes: 32 additions & 0 deletions
32
staff/judy-grayland/project/app/src/components/EditResourceButton.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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import Button from './Button' | ||
import { NotFoundError } from '../../../shared/errors' | ||
import logic from '../logic' | ||
import { errors } from '../../../shared' | ||
import { useNavigate } from 'react-router-dom' | ||
|
||
function EditResourceButton({ resourceId }) { | ||
const navigate = useNavigate() | ||
|
||
function handleEditResourceClick() { | ||
navigate(`/resources/${resourceId}`) | ||
} | ||
// const handleEditResourceClick = () => { | ||
// try { | ||
// logic | ||
// .editResource(resourceId, newData) | ||
// .then(() => { | ||
// console.log('resource edited sucessfully') | ||
// // by using navigate(0) we reload the page we're on, so the resource that's been deleted no longer shows | ||
// navigate(0) | ||
// }) | ||
// .catch((error) => { | ||
// throw new Error(error) | ||
// }) | ||
// } catch (error) { | ||
// throw new NotFoundError(error) | ||
// } | ||
// } | ||
return <Button onClick={handleEditResourceClick}>Editar</Button> | ||
} | ||
|
||
export default EditResourceButton |
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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import Field from './Field' | ||
import Button from './Button' | ||
import DeleteResourceButton from './DeleteResourceButton' | ||
import EditResourceButton from './EditResourceButton' | ||
import Form from './Form' | ||
import Topic from './Topic' | ||
|
||
export { Field, Button, DeleteResourceButton, Form, Topic } | ||
export { Field, Button, DeleteResourceButton, EditResourceButton, Form, Topic } |
36 changes: 0 additions & 36 deletions
36
staff/judy-grayland/project/app/src/logic/createActivity.js
This file was deleted.
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,43 @@ | ||
import { validate, errors } from '../../../shared' | ||
const { SystemError } = errors | ||
|
||
function editResource(resourceId, newData) { | ||
if (newData.title) validate.text(newData.title, 'title') | ||
if (newData.description) validate.text(newData.description, 'description') | ||
if (newData.resourceType) | ||
validate.resourceType(newData.resourceType, 'resourceType') | ||
if (newData.topic) validate.tagArray(newData.topic, 'topic array') | ||
if (newData.image) validate.text(newData.image, 'image') | ||
if (newData.link) validate.text(newData.link, 'link') | ||
if (newData.author) validate.text(newData.author, 'author') | ||
|
||
// prepare the request (body) | ||
const req = { | ||
method: 'PATCH', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ id: resourceId, newData }), | ||
} | ||
|
||
// send the request to the backend | ||
return fetch(`${import.meta.env.VITE_API_URL}/resources/${resourceId}`, 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) => { | ||
console.log('error from server', body.error) | ||
throw new errors[body.error](body.message) | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
export default editResource |
Oops, something went wrong.