Skip to content

Commit

Permalink
fix suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehdz committed Jan 4, 2024
1 parent cbb9bae commit e27737b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/API/Suggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { API_URL } from '../config/API';

export const getSuggestions = async () => {
try {
const response = await axios.get(API_URL + '/api/suggestions', {
const response = await axios.get(API_URL + '/api/suggestion', {
headers: {
Authorization: `Bearer ${localStorage.getItem('token')}`,
},
Expand All @@ -18,7 +18,7 @@ export const getSuggestions = async () => {
export const addSuggestions = async (suggestion) => {
try {
const response = await axios.post(
API_URL + '/api/suggestions',
API_URL + '/api/suggestion',
{
...suggestion,
},
Expand All @@ -38,7 +38,7 @@ export const addSuggestions = async (suggestion) => {
export const editSuggestions = async (suggestion) => {
try {
const response = await axios.post(
API_URL + `/api/suggestions/${suggestion.id}`,
API_URL + `/api/suggestion/${suggestion.uuid}`,
{
...suggestion,
},
Expand All @@ -58,7 +58,7 @@ export const editSuggestions = async (suggestion) => {
export const deleteSuggestions = async (suggestionid) => {
try {
const response = await axios.delete(
API_URL + `/api/suggestions/${suggestionid}`,
API_URL + `/api/suggestion/${suggestionid}`,
{
headers: {
Authorization: `Bearer ${localStorage.getItem('token')}`,
Expand Down
51 changes: 44 additions & 7 deletions src/component/SuggestionsTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,30 @@ import {
} from '../API/Suggestions';
import '../styles/component/MarkerTable.css';

const SuggestionModal = ({ isModalOpen, handleClose, suggestion }) => {
const SuggestionModal = ({
isModalOpen,
handleClose,
suggestion,
suggestions,
setSuggestions,
}) => {
const [fields, setFields] = React.useState({
name: suggestion && suggestion.name,
tag: suggestion && suggestion.tag,
description: suggestion && suggestion.description,
imageUrl: suggestion && suggestion.imageUrl,
uuid: suggestion && suggestion.uuid,
});

const handleSubmit = async (e) => {
try {
e.preventDefault();

await editSuggestions(fields);
const index = suggestions.findIndex((row) => row.uuid === fields.uuid);

suggestions[index] = fields;
setSuggestions(suggestions);
handleClose();
} catch (e) {
console.log(e);
Expand All @@ -64,7 +75,7 @@ const SuggestionModal = ({ isModalOpen, handleClose, suggestion }) => {
fontWeight: 800,
}}
>
Edit suggestion N°{suggestion && suggestion.id}
Edit suggestion N°{suggestion && suggestion.uuid}
</Typography>
<IconButton onClick={() => handleClose()}>
<CloseIcon />
Expand Down Expand Up @@ -114,7 +125,12 @@ const SuggestionModal = ({ isModalOpen, handleClose, suggestion }) => {
);
};

const AddSuggestionModal = ({ isModalOpen, handleClose }) => {
const AddSuggestionModal = ({
isModalOpen,
handleClose,
suggestions,
setSuggestions,
}) => {
const [fields, setFields] = React.useState({
name: null,
tag: null,
Expand All @@ -127,6 +143,7 @@ const AddSuggestionModal = ({ isModalOpen, handleClose }) => {
e.preventDefault();

await addSuggestions(fields);
setSuggestions([...suggestions, fields]);
handleClose();
} catch (e) {
console.log(e);
Expand Down Expand Up @@ -203,12 +220,24 @@ const AddSuggestionModal = ({ isModalOpen, handleClose }) => {
);
};

const SuggestionDeleteModal = ({ isModalOpen, handleClose, suggestion }) => {
const SuggestionDeleteModal = ({
isModalOpen,
handleClose,
suggestion,
suggestions,
setSuggestions,
}) => {
const handleSubmit = async (e) => {
try {
e.preventDefault();

await deleteSuggestions(suggestion.id);
await deleteSuggestions(suggestion.uuid);

const index = suggestions.findIndex(
(row) => row.uuid === suggestion.uuid
);

suggestions.splice(index, 1);
handleClose();
} catch (e) {
console.log(e);
Expand All @@ -235,7 +264,7 @@ const SuggestionDeleteModal = ({ isModalOpen, handleClose, suggestion }) => {
fontWeight: 800,
}}
>
Remove suggestion N°{suggestion && suggestion.id} ?
Remove suggestion N°{suggestion && suggestion.uuid} ?
</Typography>
<IconButton onClick={() => handleClose()}>
<CloseIcon />
Expand Down Expand Up @@ -308,6 +337,8 @@ const SuggestionsTable = () => {
<AddSuggestionModal
isModalOpen={isModalAddOpen}
handleClose={handleAddClose}
suggestions={suggestions}
setSuggestions={setSuggestions}
/>
</Container>
);
Expand All @@ -333,6 +364,8 @@ const SuggestionsTable = () => {
<AddSuggestionModal
isModalOpen={isModalAddOpen}
handleClose={handleAddClose}
suggestions={suggestions}
setSuggestions={setSuggestions}
/>
</Container>
<TableContainer>
Expand All @@ -352,7 +385,7 @@ const SuggestionsTable = () => {
{suggestions.map((row, index) => (
<TableRow key={index}>
<TableCell component='th' scope='row'>
{row.id}
{row.uuid}
</TableCell>
<TableCell align='right'>{row.name}</TableCell>
<TableCell align='right'>{row.tag}</TableCell>
Expand Down Expand Up @@ -399,13 +432,17 @@ const SuggestionsTable = () => {
isModalOpen={isModalOpen}
handleClose={handleClose}
suggestion={selectedSuggestion}
suggestions={suggestions}
setSuggestions={setSuggestions}
/>
)}
{isModalDeleteOpen && (
<SuggestionDeleteModal
isModalOpen={isModalDeleteOpen}
handleClose={handleDeleteClose}
suggestion={selectedSuggestion}
suggestions={suggestions}
setSuggestions={setSuggestions}
/>
)}
</Container>
Expand Down

0 comments on commit e27737b

Please sign in to comment.