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

inv-126 : 타이틀, 내용 업데이트 쿼리 #64

Merged
merged 11 commits into from
Aug 18, 2024
2 changes: 1 addition & 1 deletion src/app/(main)/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default async function Page() {
if (!auth.user) {
return redirect("/sign-in");
}
const invitations = await getInvitationsByUserId(auth.user.id);
const invitations = await getInvitationsByUserId();

return (
<div className="flex h-dvh flex-col overflow-hidden">
Expand Down
12 changes: 9 additions & 3 deletions src/components/editor/navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Undo2,
} from "lucide-react";
import Link from "next/link";
import { useParams } from "next/navigation";
import { toast } from "sonner";
import { useEditor } from "~/components/editor/provider";
import TitleInput from "~/components/editor/title-input";
Expand All @@ -27,11 +28,14 @@ import {
} from "~/components/ui/dropdown-menu";
import { Tabs, TabsList, TabsTrigger } from "~/components/ui/tabs";
import TooltipSimple from "~/components/ui/tooltip-simple";
import { updateInvitation } from "~/lib/db/schema/invitations.query";
import { cn } from "~/lib/utils";

export default function EditorNavigation() {
const { editor, dispatch } = useEditor();
const { openDialog } = useAlertDialogStore();
const params = useParams();
const subDomain = params.subdomain;

const handlePreviewClick = () => {
dispatch({ type: "TOGGLE_PREVIEW_MODE" });
Expand All @@ -48,9 +52,11 @@ export default function EditorNavigation() {
const handleOnSave = async () => {
try {
const content = JSON.stringify(editor.data);
console.log(":content", content);
// TODO: API insert page
// TODO: API log notification
await updateInvitation({
id: subDomain,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

subDomain은 수정이 가능하기에, editor.config.invitationId로 적용해주세요1

title: editorConfig.invitationTitle,
customFields: content,
});
toast.success("Saved Editor");
} catch (error) {
console.error(error);
Expand Down
18 changes: 11 additions & 7 deletions src/components/editor/title-input.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
"use client";

import { useMutation } from "@tanstack/react-query";
import { debounce, delay, random } from "es-toolkit";
import { debounce, delay } from "es-toolkit";
import { CheckIcon, LoaderIcon, XIcon } from "lucide-react";
import { useParams } from "next/navigation";
import { useRef } from "react";
import { toast } from "sonner";
import { useEditor } from "~/components/editor/provider";
import { updateInvitation } from "~/lib/db/schema/invitations.query";
import { cn } from "~/lib/utils";

export default function TitleInput() {
const { editor } = useEditor();
const { editor, editorConfig } = useEditor();
const params = useParams();
const subDomain = params.subdomain as string;

const abortControllerRef = useRef<AbortController | null>(null);

Expand Down Expand Up @@ -45,11 +49,11 @@ export default function TitleInput() {
if (signal.aborted) {
return;
}

if (random(0, 1) > 0.5) {
throw new Error("Failed to update title");
}

editorConfig.invitationTitle = value;
await updateInvitation({
id: subDomain,
title: value,
});
delayMutation.mutate();
},
onError: () => {
Expand Down
15 changes: 10 additions & 5 deletions src/lib/db/schema/invitations.query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { count, eq } from "drizzle-orm";
import { nanoid } from "nanoid";
import { getAuth } from "~/lib/auth/utils";
import { db } from "~/lib/db";
import {
invitations,
Expand All @@ -11,7 +12,7 @@ import {

type CreateInvitationParams = Omit<
InvitationInsert,
"id" | "eventUrl" | "createdAt" | "updatedAt"
"id" | "userId" | "eventUrl" | "createdAt" | "updatedAt"
>;

type UpdateInvitationParams = {
Expand Down Expand Up @@ -41,27 +42,31 @@ export async function getInvitationByEventUrl(eventUrl: string) {
return responses[0];
}

export async function getInvitationsByUserId(
userId: Invitation["userId"],
): Promise<Invitation[]> {
export async function getInvitationsByUserId(): Promise<Invitation[]> {
const sessionId = await getAuth();
const sessionUserId = sessionId.session.userId;

return await db
.select()
.from(invitations)
.where(eq(invitations.userId, userId));
.where(eq(invitations.userId, sessionUserId));
}

export async function createInvitation(
params: CreateInvitationParams,
): Promise<InvitationInsert> {
const id = nanoid();
const currentTimestamp = new Date();
const sessionId = await getAuth();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const auth = await getAuth() 컨벤션 맞춰주시면 감사하겠습니다 🙇🏻‍♂️

const sessionUserId = sessionId.session.userId;

try {
const res = await db
.insert(invitations)
.values({
...params,
id,
userId: sessionUserId,
eventUrl: id,
createdAt: currentTimestamp,
updatedAt: currentTimestamp,
Expand Down
Loading