Skip to content

Commit

Permalink
Fix/delete esp (#217)
Browse files Browse the repository at this point in the history
* feat: possibility to delete an ESP directly from the dashboard

* feat: create the migration files

Closes: #184
  • Loading branch information
ColinRgm authored Jan 7, 2025
1 parent e8adb1d commit efa7a36
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 1 deletion.
8 changes: 8 additions & 0 deletions database/migration/deploy/delete_esp_permission.sql
Original file line number Diff line number Diff line change
@@ -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;
8 changes: 8 additions & 0 deletions database/migration/revert/delete_esp_permission.sql
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions database/migration/sqitch.plan
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ esp 2024-06-26T08:20:01Z dylan <dylanbossoku@dylan> # create a esp table to stov
2024-07-05 2024-07-05T08:58:10Z Nils <nils@fedora> # Grant user the ability to remove users
2024-07-10 2024-07-10T08:08:27Z Nils <nils@fedora> # Give the esp permission to know it's id
add_new_permission 2024-12-17T14:46:26Z Colin <colin@JT-24-11-07-Colin> # Add a permission for the user to edit the user's datas
delete_esp_permission 2025-01-07T08:18:34Z Colin <colin@JT-24-11-07-Colin> # Add a permission for the user to delete ESP
8 changes: 8 additions & 0 deletions database/migration/verify/delete_esp_permission.sql
Original file line number Diff line number Diff line change
@@ -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;
8 changes: 7 additions & 1 deletion nextjs-interface/src/app/dashboard/esp/[espId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 }) {
Expand Down Expand Up @@ -111,7 +114,10 @@ export default function Page({ params }: { params: any }) {
<div className="flex h-full w-full flex-col gap-y-5 pt-2">
<div className="flex justify-between text-xl font-bold uppercase">
<div>{esp.name}</div>
<RenameElement id={params.espId} />
<div className="flex gap-4">
<RenameElement id={params.espId} />
<DeleteEsp id={params.espId} />
</div>
</div>
<div className="mt-0 text-zinc-500">{esp.ip}</div>
<div className="flex flex-col gap-x-5 gap-y-5 sm:flex-row">
Expand Down
45 changes: 45 additions & 0 deletions nextjs-interface/src/app/ui/dashboard/DeleteEsp.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<div className="flex cursor-pointer gap-2">
<Trash2 onClick={() => deleteEsp(id)} />
</div>
);
}

0 comments on commit efa7a36

Please sign in to comment.