-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
917c76e
commit dda3513
Showing
13 changed files
with
778 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { useState } from "react"; | ||
|
||
import { Button } from "@/components/ui/button"; | ||
import { Input } from "@/components/ui/input"; | ||
import { Label } from "@/components/ui/label"; | ||
import { | ||
Dialog, | ||
DialogContent, | ||
DialogDescription, | ||
DialogFooter, | ||
DialogHeader, | ||
DialogTitle, | ||
DialogTrigger, | ||
} from "@/components/ui/dialog"; | ||
|
||
import { useToast } from "@/hooks/use-toast"; | ||
import { authenticatedFetch } from "@/lib/api"; | ||
|
||
export function CreateUser({ mutate }) { | ||
const [loading, setLoading] = useState(false); | ||
const [open, setOpen] = useState(false); | ||
const { toast } = useToast(); | ||
|
||
const submit = async (event) => { | ||
event.preventDefault(); | ||
|
||
const username = event.target.querySelector("#username").value; | ||
const password = event.target.querySelector("#password").value; | ||
|
||
setLoading(true); | ||
try { | ||
await authenticatedFetch("/users", { | ||
method: "POST", | ||
body: JSON.stringify({ username, password }), | ||
}); | ||
|
||
await mutate(); | ||
|
||
toast({ description: "User created" }); | ||
setOpen(false); | ||
} catch (error: any) { | ||
toast({ | ||
title: "An error occurred", | ||
description: error.message, | ||
variant: "destructive", | ||
}); | ||
} | ||
setLoading(false); | ||
}; | ||
|
||
return ( | ||
<Dialog open={open} onOpenChange={setOpen}> | ||
<DialogTrigger asChild> | ||
<Button variant="outline">Add user</Button> | ||
</DialogTrigger> | ||
<DialogContent className="sm:max-w-[425px]"> | ||
<form onSubmit={submit}> | ||
<DialogHeader> | ||
<DialogTitle>Create user</DialogTitle> | ||
<DialogDescription>Add a new user to your team.</DialogDescription> | ||
</DialogHeader> | ||
<div className="grid gap-4 py-4"> | ||
<div className="grid grid-cols-4 items-center gap-4"> | ||
<Label htmlFor="username" className="text-right"> | ||
Username | ||
</Label> | ||
<Input | ||
id="username" | ||
required | ||
placeholder="pedro" | ||
className="col-span-3" | ||
/> | ||
</div> | ||
<div className="grid grid-cols-4 items-center gap-4"> | ||
<Label htmlFor="password" className="text-right"> | ||
Password | ||
</Label> | ||
<Input | ||
id="password" | ||
required | ||
minLength={5} | ||
type="password" | ||
className="col-span-3" | ||
/> | ||
</div> | ||
</div> | ||
<DialogFooter> | ||
<Button type="submit" disabled={loading}> | ||
Save changes | ||
</Button> | ||
</DialogFooter> | ||
</form> | ||
</DialogContent> | ||
</Dialog> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { useState } from "react"; | ||
import { TrashIcon } from "lucide-react"; | ||
|
||
import { Button } from "@/components/ui/button"; | ||
import { | ||
Dialog, | ||
DialogContent, | ||
DialogDescription, | ||
DialogFooter, | ||
DialogHeader, | ||
DialogTitle, | ||
DialogTrigger, | ||
} from "@/components/ui/dialog"; | ||
|
||
import { useToast } from "@/hooks/use-toast"; | ||
import { authenticatedFetch, useData } from "@/lib/api"; | ||
|
||
export function DeleteUser({ id }) { | ||
const [loading, setLoading] = useState(false); | ||
const [open, setOpen] = useState(false); | ||
|
||
const { mutate } = useData(`../users`); | ||
const { toast } = useToast(); | ||
|
||
const submit = async (event) => { | ||
event.preventDefault(); | ||
|
||
setLoading(true); | ||
try { | ||
await authenticatedFetch(`/users/${id}`, { | ||
method: "DELETE", | ||
}); | ||
|
||
await mutate(); | ||
setOpen(false); | ||
|
||
toast({ description: "User deleted" }); | ||
} catch (error: any) { | ||
toast({ | ||
title: "An error occurred", | ||
description: error.message, | ||
variant: "destructive", | ||
}); | ||
} | ||
setLoading(false); | ||
}; | ||
|
||
return ( | ||
<Dialog open={open} onOpenChange={setOpen}> | ||
<DialogTrigger asChild> | ||
<div className="flex justify-end"> | ||
<Button variant="outline" size="sm"> | ||
<TrashIcon className="size-4 mr-2" /> | ||
Remove | ||
</Button> | ||
</div> | ||
</DialogTrigger> | ||
<DialogContent className="sm:max-w-[425px]"> | ||
<form onSubmit={submit} className="flex flex-col gap-4"> | ||
<DialogHeader> | ||
<DialogTitle>Are you absolutely sure?</DialogTitle> | ||
<DialogDescription> | ||
Are you sure you want to remove this user? This action can't be | ||
undone. | ||
</DialogDescription> | ||
</DialogHeader> | ||
<DialogFooter> | ||
<Button type="submit" variant="destructive" disabled={loading}> | ||
Remove user | ||
</Button> | ||
</DialogFooter> | ||
</form> | ||
</DialogContent> | ||
</Dialog> | ||
); | ||
} |
Oops, something went wrong.