-
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.
Merge pull request #36 from evmiguel/edit-fields
Edit fields
- Loading branch information
Showing
13 changed files
with
1,295 additions
and
54 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 was deleted.
Oops, something went wrong.
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,80 @@ | ||
import { Button } from "@/components/ui/button" | ||
import { | ||
Dialog, | ||
DialogContent, | ||
DialogDescription, | ||
DialogFooter, | ||
DialogHeader, | ||
DialogTitle, | ||
DialogTrigger, | ||
} from "@/components/ui/dialog" | ||
import { Input } from "@/components/ui/input" | ||
import { Label } from "@/components/ui/label" | ||
import Link from "next/link" | ||
import { Purchase } from "../Purchases/columns" | ||
import { useRouter } from "next/navigation" | ||
|
||
type EditPurchaseProps = { | ||
purchase: Purchase, | ||
open: boolean, | ||
openOnChange: (value: boolean) => void | ||
} | ||
|
||
export default function EditPurchase({ purchase, open, openOnChange }: EditPurchaseProps) { | ||
const router = useRouter(); | ||
|
||
async function updatePurchase(formData: FormData) { | ||
|
||
const data = { | ||
id: purchase.id, | ||
name: formData.get('name'), | ||
category: formData.get('category') | ||
} | ||
|
||
try { | ||
await fetch('/api/purchase', { | ||
method: 'PUT', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify(data) | ||
}); | ||
openOnChange(false); | ||
router.refresh(); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
|
||
return ( | ||
<Dialog open={open} onOpenChange={openOnChange}> | ||
<DialogContent className="sm:max-w-[425px]"> | ||
<DialogHeader> | ||
<DialogTitle>Edit Purchase</DialogTitle> | ||
<DialogDescription> | ||
Change the name or category of your purchase. Hit save when you're done | ||
</DialogDescription> | ||
</DialogHeader> | ||
<form action={updatePurchase}> | ||
<div className="grid gap-4 py-4"> | ||
<div className="grid grid-cols-4 items-center gap-4"> | ||
<Label htmlFor="name" className="text-right"> | ||
Name | ||
</Label> | ||
<Input id="name" name="name" defaultValue={purchase.name} className="col-span-3" /> | ||
</div> | ||
<div className="grid grid-cols-4 items-center gap-4"> | ||
<Label htmlFor="category" className="text-right"> | ||
Category | ||
</Label> | ||
<Input id="category" name="category" defaultValue={purchase.category as string} className="col-span-3" /> | ||
</div> | ||
</div> | ||
<DialogFooter> | ||
<Button type="submit">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
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
Oops, something went wrong.