Skip to content

Commit

Permalink
Add delete unused images button
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielOaks committed Jul 20, 2024
1 parent 2c78435 commit e853a98
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/app/apiSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@ export const api = createApi({
body,
}),
}),
deleteUnusedImages: build.mutation<OkResponse, RequestWithToken>({
query: ({ token }) => ({
url: `image/unused`,
method: "DELETE",
headers: {
Authorization: `Bearer ${token}`,
},
}),
}),
addImageToCategory: build.mutation<OkResponse, AddImageToCategoryRequest>({
query: ({ token, category, image, body }) => ({
url: `categories/${category}/images/${image}`,
Expand Down Expand Up @@ -265,6 +274,7 @@ export const {
useGetCategoriesQuery,
useGetCategoryQuery,
useAddImageMutation,
useDeleteUnusedImagesMutation,
useAddImageToCategoryMutation,
useDeleteImageFromCategoryMutation,
useGetCategoryImagesQuery,
Expand Down
23 changes: 22 additions & 1 deletion src/routes/AdminDashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import TheFooter from "../components/TheFooter";
import TheLoadingModal from "../components/TheLoadingModal";

import { useAppSelector } from "../app/hooks";
import { useGetCategoriesQuery, useDeleteCategoryMutation } from "../app/apiSlice";
import { useGetCategoriesQuery, useDeleteCategoryMutation, useDeleteUnusedImagesMutation } from "../app/apiSlice";

function AdminDashboard() {
const { data: categories, isLoading } = useGetCategoriesQuery();
const [deleteCategory] = useDeleteCategoryMutation();
const [deleteUnusedImages] = useDeleteUnusedImagesMutation();

const user = useAppSelector((state) => state.userProfile);

Expand Down Expand Up @@ -65,6 +66,26 @@ function AdminDashboard() {
</Link>
)}
</div>

<div className="mx-auto mt-6 flex w-[20em] max-w-full flex-col border-[5px] border-primary-700 bg-primary-900">
<button
className="block py-3 hover:bg-primary-800"
onClick={async (e) => {
if (window.confirm(`Delete unused images?`)) {
try {
await deleteUnusedImages({
token: user.token,
});
} catch (err) {
console.error(err);
return;
}
}
}}
>
Delete unused images
</button>
</div>
</div>
<TheFooter />
</div>
Expand Down

0 comments on commit e853a98

Please sign in to comment.