Skip to content

Commit

Permalink
title is updated
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnmclean committed Jan 7, 2025
1 parent 0f08111 commit 8c91186
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use server";

import { revalidatePath } from "next/cache";
import { unauthorized } from "next/navigation";
import { withZod } from "@rvf/zod";
import { db, eq, schema } from "@sovoli/db";
Expand Down Expand Up @@ -41,7 +42,7 @@ export async function updateTitleAction(
})
.where(eq(schema.Knowledge.id, result.data.id));

return {
status: "success",
};
revalidatePath("[username]/[slug]", "page");

return { status: "success" };
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { TitleUpdateForm } from "./TitleUpdateForm";

export function KnowledgeTitle() {
const knowledge = useKnowledge();
const [title, setTitle] = useState(knowledge.title ?? "");
const { data: session } = useSession();

const [isEditing, setIsEditing] = useState(false);
Expand All @@ -20,10 +21,14 @@ export function KnowledgeTitle() {
id={knowledge.id}
title={knowledge.title ?? ""}
onCancel={() => setIsEditing(false)}
onSubmitted={(newTitle) => {
setTitle(newTitle);
setIsEditing(false);
}}
/>
) : (
<div className="flex w-full flex-row justify-between">
<h1 className="text-2xl font-bold">{knowledge.title}</h1>
<h1 className="text-2xl font-bold">{title}</h1>

{session?.userId === knowledge.User?.id && (
<Button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,33 @@ export interface TitleUpdateFormProps {
id: string;
title: string;
onCancel: () => void;
onSubmitted: (newTitle: string) => void;
}

export function TitleUpdateForm({ id, title, onCancel }: TitleUpdateFormProps) {
export function TitleUpdateForm({
id,
title,
onCancel,
onSubmitted,
}: TitleUpdateFormProps) {
const [state, formAction, pending] = useActionState<State, FormData>(
updateTitleAction,
async (_, formData) => {
try {
// Call the update action
const result = await updateTitleAction(null, formData);

// If the action succeeds, trigger the onUpdate callback
if (result?.status === "success") {
const updatedTitle = formData.get("title") as string;
onSubmitted(updatedTitle);
}

return result; // Return the result for further state handling
} catch (error) {
console.error("Error during form action:", error);
return { status: "error", message: "Failed to update title." };
}
},
null,
);

Expand Down

0 comments on commit 8c91186

Please sign in to comment.