Skip to content

Commit

Permalink
added delete place button
Browse files Browse the repository at this point in the history
  • Loading branch information
timur authored and timur committed Dec 26, 2024
1 parent 9aac3ce commit 61a7f01
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/modules/place.module.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { TagComponent } from "@/components/tag";
import { Button } from "@/components/ui/button";
import { useDashboardStore } from "@/shared/stores/places.store";
import { uploadImageByUrl } from "@/shared/api/parse.api";
import { deletePlace } from "@/shared/api/places.api";

export const PlaceModule = ({
inputPlace,
Expand Down Expand Up @@ -91,6 +92,9 @@ export const PlaceModule = ({
onSave(place);
}, [place, onSave]);

const handleDelete = () => {
deletePlace(place);
};
const handleDragEnd = (result: DropResult) => {
if (!result.destination) return;

Expand Down Expand Up @@ -253,6 +257,13 @@ export const PlaceModule = ({
</div>
</div>
<div className="flex justify-end">
<Button
type="button"
onClick={handleDelete}
className="bg-red-800 mx-10"
>
Delete
</Button>
<Button type="button" onClick={handleSave}>
Save
</Button>
Expand Down
28 changes: 28 additions & 0 deletions src/shared/api/places.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,31 @@ export const savePlace = async (place: Place): Promise<Place> => {
throw err;
}
};

export const deletePlace = async (place: Place): Promise<Place> => {
const api_key = useSettingsStore.getState().api_key;

try {
const response = await axios.delete<Place>(
`${API_URL}/api/v1/places/${place.id}`,
{
headers: {
"X-API-Token": api_key,
},
}
);
console.log(response.data);
return response.data;
} catch (err) {
if (axios.isAxiosError(err)) {
console.error(`Error deleting place: ${err.message}`, {
status: err.response?.status,
data: err.response?.data,
});
} else {
console.error("Unexpected error deleting place:", err);
}

throw err;
}
};

0 comments on commit 61a7f01

Please sign in to comment.