Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fear: added button to show esp jwt token #90

Merged
merged 2 commits into from
Jul 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 62 additions & 24 deletions nextjs-interface/src/app/ui/dashboard/RenameElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import {
export default function RenameElement({ id }: { id: string }) {
const [newName, setNewName] = useState("");
const [confirm, setConfirm] = React.useState(false);
const [showToken, setShowToken] = useState(false);
const token = getToken();

const updateEspName = async (newName: string) => {
const url = `/postgrest/esp?id=eq.${id}`;
const response = await fetch(url, {
Expand All @@ -30,40 +33,75 @@ export default function RenameElement({ id }: { id: string }) {
} else {
}
};

const copyToClipboard = (text: string) => {
navigator.clipboard
.writeText(text)
.then(() => {
console.log("Token copied to clipboard");
})
.catch((err) => {
console.error("Failed to copy token: ", err);
});
};

const toggleAndCopyToken = () => {
setShowToken(!showToken);
if (!showToken && token) {
copyToClipboard(token);
} else {
return null;
}
};

return (
<div className="flex cursor-pointer gap-2">
<Popover>
<PopoverTrigger asChild>
<EllipsisVertical />
</PopoverTrigger>
<PopoverContent className="flex w-60 gap-2 dark:bg-zinc-800">
<Input
type="text"
placeholder="modifier l'esp"
value={newName}
className="dark:bg-zinc-800"
onChange={(e) => {
setNewName(e.target.value);
setConfirm(true);
}}
/>
{confirm ? (
<Button
onClick={async () => {
try {
await updateEspName(newName);
setConfirm(false);
} catch (e) {
console.error(e);
}
<PopoverContent className="mr-5 mt-2 flex w-fit flex-col gap-2 dark:bg-zinc-800">
<div className="flex flex-row gap-x-5">
<Input
type="text"
placeholder="modifier l'esp"
value={newName}
className="dark:bg-zinc-800"
onChange={(e) => {
setNewName(e.target.value);
setConfirm(true);
}}
/>
{confirm && (
<Button
onClick={async () => {
try {
await updateEspName(newName);
setConfirm(false);
} catch (e) {
console.error(e);
}
}}
className="p-2 text-white dark:bg-zinc-700 dark:text-white dark:hover:bg-black"
>
Confirm
</Button>
)}
</div>
<div>
<Button
onClick={toggleAndCopyToken}
className="p-2 text-white dark:bg-zinc-700 dark:text-white dark:hover:bg-black"
>
Confirm
{showToken ? "Cacher le Token" : "Afficher et Copier le Token"}
</Button>
) : (
""
)}
{showToken && (
<div className="mt-2 bg-zinc-200 p-2 text-sm dark:bg-zinc-800">
Token copier !: <br />
{token}
</div>
)}
</div>
</PopoverContent>
</Popover>
</div>
Expand Down