-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
Binary file added
BIN
+47.6 KB
playwright-tests/snapshots/update-description.spec.ts-update-description.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,18 @@ | ||
import { test, expect } from "@playwright/test" | ||
|
||
test("test", async ({ page }) => { | ||
await page.goto("http://localhost:5177/editor?snippet_id=snippet_3") | ||
await page.getByRole("button", { name: "Fake testuser Login" }).click() | ||
await page.locator('[id="radix-\\:rm\\:"]').click() | ||
await page.waitForTimeout(2000) | ||
await page.getByRole("menuitem", { name: "Edit Description" }).click() | ||
await page.getByPlaceholder("Enter new description").press("End") | ||
await page | ||
.getByPlaceholder("Enter new description") | ||
.fill("Check out this new description") | ||
await page.getByRole("button", { name: "Update" }).click() | ||
await page.locator('[id="radix-\\:rm\\:"]').click() | ||
await page.waitForTimeout(2000) | ||
await page.getByRole("menuitem", { name: "Edit Description" }).click() | ||
await expect(page).toHaveScreenshot("update-description.png") | ||
}) |
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 { Dialog, DialogContent, DialogHeader, DialogTitle } from "../ui/dialog" | ||
import { Button } from "../ui/button" | ||
import { useState } from "react" | ||
import { useMutation, useQueryClient } from "react-query" | ||
import { createUseDialog } from "./create-use-dialog" | ||
import { useAxios } from "@/hooks/use-axios" | ||
import { useToast } from "@/hooks/use-toast" | ||
import { Textarea } from "../ui/textarea" | ||
|
||
export const UpdateDescriptionDialog = ({ | ||
open, | ||
onOpenChange, | ||
snippetId, | ||
currentDescription, | ||
onUpdate, | ||
}: { | ||
open: boolean | ||
onOpenChange: (open: boolean) => void | ||
snippetId: string | ||
currentDescription: string | ||
onUpdate?: (newDescription: string) => void | ||
}) => { | ||
const [newDescription, setNewDescription] = useState(currentDescription) | ||
const axios = useAxios() | ||
const { toast } = useToast() | ||
const qc = useQueryClient() | ||
|
||
const updateDescriptionMutation = useMutation({ | ||
mutationFn: async () => { | ||
const response = await axios.post("/snippets/update", { | ||
snippet_id: snippetId, | ||
description: newDescription, | ||
}) | ||
if (response.status !== 200) { | ||
throw new Error("Failed to update description") | ||
} | ||
return response.data | ||
}, | ||
onMutate: async () => { | ||
await qc.cancelQueries({ queryKey: ["snippets", snippetId] }) | ||
const previousSnippet = qc.getQueryData(["snippets", snippetId]) | ||
qc.setQueryData(["snippets", snippetId], (old: any) => ({ | ||
...old, | ||
description: newDescription, | ||
})) | ||
return { previousSnippet } | ||
}, | ||
onSuccess: () => { | ||
onUpdate?.(newDescription) | ||
onOpenChange(false) | ||
toast({ | ||
title: "Description updated", | ||
description: "Successfully updated snippet description", | ||
}) | ||
}, | ||
onError: (error, variables, context) => { | ||
qc.setQueryData(["snippets", snippetId], context?.previousSnippet) | ||
console.error("Error updating description:", error) | ||
toast({ | ||
title: "Error", | ||
description: "Failed to update the description. Please try again.", | ||
variant: "destructive", | ||
}) | ||
}, | ||
onSettled: () => { | ||
qc.invalidateQueries({ queryKey: ["snippets", snippetId] }) | ||
}, | ||
}) | ||
|
||
return ( | ||
<Dialog open={open} onOpenChange={onOpenChange}> | ||
<DialogContent> | ||
<DialogHeader> | ||
<DialogTitle>Update Description</DialogTitle> | ||
</DialogHeader> | ||
<Textarea | ||
value={newDescription} | ||
onChange={(e) => setNewDescription(e.target.value)} | ||
placeholder="Enter new description" | ||
disabled={updateDescriptionMutation.isLoading} | ||
className="resize-none min-h-[80px]" | ||
/> | ||
<Button | ||
disabled={updateDescriptionMutation.isLoading} | ||
onClick={() => updateDescriptionMutation.mutate()} | ||
> | ||
{updateDescriptionMutation.isLoading ? "Updating..." : "Update"} | ||
</Button> | ||
</DialogContent> | ||
</Dialog> | ||
) | ||
} | ||
|
||
export const useUpdateDescriptionDialog = createUseDialog( | ||
UpdateDescriptionDialog, | ||
) |