From efa7a369adcafb17d1304e0f654d5beca00cdc0b Mon Sep 17 00:00:00 2001 From: ColinRgm <90181373+ColinRgm@users.noreply.github.com> Date: Tue, 7 Jan 2025 11:23:38 +0100 Subject: [PATCH] Fix/delete esp (#217) * feat: possibility to delete an ESP directly from the dashboard * feat: create the migration files Closes: #184 --- .../deploy/delete_esp_permission.sql | 8 ++++ .../revert/delete_esp_permission.sql | 8 ++++ database/migration/sqitch.plan | 1 + .../verify/delete_esp_permission.sql | 8 ++++ .../src/app/dashboard/esp/[espId]/page.tsx | 8 +++- .../src/app/ui/dashboard/DeleteEsp.tsx | 45 +++++++++++++++++++ 6 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 database/migration/deploy/delete_esp_permission.sql create mode 100644 database/migration/revert/delete_esp_permission.sql create mode 100644 database/migration/verify/delete_esp_permission.sql create mode 100644 nextjs-interface/src/app/ui/dashboard/DeleteEsp.tsx diff --git a/database/migration/deploy/delete_esp_permission.sql b/database/migration/deploy/delete_esp_permission.sql new file mode 100644 index 0000000..98abc1b --- /dev/null +++ b/database/migration/deploy/delete_esp_permission.sql @@ -0,0 +1,8 @@ +-- Deploy climat-guardian:delete_esp_permission to pg + +BEGIN; + + grant delete on api.esp to web_user; -- any user can delete ESP + grant delete on api.data to web_user; -- any user can delete ESP's data + +COMMIT; diff --git a/database/migration/revert/delete_esp_permission.sql b/database/migration/revert/delete_esp_permission.sql new file mode 100644 index 0000000..f9ebc2e --- /dev/null +++ b/database/migration/revert/delete_esp_permission.sql @@ -0,0 +1,8 @@ +-- Revert climat-guardian:delete_esp_permission from pg + +BEGIN; + + revoke delete on api.esp to web_user; -- any user can't anymore delete ESP + revoke delete on api.data to web_user; -- any user can't anymore delete ESP's data + +COMMIT; diff --git a/database/migration/sqitch.plan b/database/migration/sqitch.plan index 4654b35..2ca570d 100644 --- a/database/migration/sqitch.plan +++ b/database/migration/sqitch.plan @@ -10,3 +10,4 @@ esp 2024-06-26T08:20:01Z dylan # create a esp table to stov 2024-07-05 2024-07-05T08:58:10Z Nils # Grant user the ability to remove users 2024-07-10 2024-07-10T08:08:27Z Nils # Give the esp permission to know it's id add_new_permission 2024-12-17T14:46:26Z Colin # Add a permission for the user to edit the user's datas +delete_esp_permission 2025-01-07T08:18:34Z Colin # Add a permission for the user to delete ESP diff --git a/database/migration/verify/delete_esp_permission.sql b/database/migration/verify/delete_esp_permission.sql new file mode 100644 index 0000000..fc4f152 --- /dev/null +++ b/database/migration/verify/delete_esp_permission.sql @@ -0,0 +1,8 @@ +-- Verify climat-guardian:delete_esp_permission on pg + +BEGIN; + + \z api.esp -- check if user can delete or not an ESP + \z api.data -- check if user can delete or not an ESP's data + +ROLLBACK; diff --git a/nextjs-interface/src/app/dashboard/esp/[espId]/page.tsx b/nextjs-interface/src/app/dashboard/esp/[espId]/page.tsx index 1e05d87..16023fc 100644 --- a/nextjs-interface/src/app/dashboard/esp/[espId]/page.tsx +++ b/nextjs-interface/src/app/dashboard/esp/[espId]/page.tsx @@ -26,6 +26,7 @@ import React, { useState, useEffect } from "react"; import { useRouter, useSearchParams } from "next/navigation"; import RenameElement from "@/app/ui/dashboard/RenameElement"; +import { Trash2 } from "lucide-react"; import useFindIpById from "@/lib/data"; import { Select, @@ -34,6 +35,8 @@ import { SelectTrigger, SelectValue, } from "@/components/ui/select"; +import { getToken } from "@/lib/context"; +import DeleteEsp from "@/app/ui/dashboard/DeleteEsp"; // main component export default function Page({ params }: { params: any }) { @@ -111,7 +114,10 @@ export default function Page({ params }: { params: any }) {
{esp.name}
- +
+ + +
{esp.ip}
diff --git a/nextjs-interface/src/app/ui/dashboard/DeleteEsp.tsx b/nextjs-interface/src/app/ui/dashboard/DeleteEsp.tsx new file mode 100644 index 0000000..e494746 --- /dev/null +++ b/nextjs-interface/src/app/ui/dashboard/DeleteEsp.tsx @@ -0,0 +1,45 @@ +import { Trash2 } from "lucide-react"; +import { getToken } from "@/lib/context"; + +export default function DeleteEsp({ id }: { id: string }) { + const deleteEsp = async (id: string) => { + // Get the id in the URL of the page + const urlData = `/postgrest/data?esp_id=eq.${id}`; + + const url = `/postgrest/esp?id=eq.${id}`; + + try { + const responseData = await fetch(urlData, { + method: "DELETE", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${getToken()}`, + }, + }); + + const response = await fetch(url, { + method: "DELETE", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${getToken()}`, + }, + }); + + window.location.href = `/dashboard`; + + if (!responseData.ok && !response.ok) { + console.error(`une erreur lors de la suppression de l'ESP`); + console.error(await responseData.json()); + console.error(await response.json()); + } + } catch (error) { + console.error("Error: ", error); + } + }; + + return ( +
+ deleteEsp(id)} /> +
+ ); +}