Skip to content

Commit

Permalink
update tree, item
Browse files Browse the repository at this point in the history
  • Loading branch information
steeeee0223 committed Jul 17, 2024
1 parent 96fe5fd commit 5d44cba
Show file tree
Hide file tree
Showing 18 changed files with 139 additions and 205 deletions.
7 changes: 4 additions & 3 deletions apps/worxpace/src/actions/archive-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { revalidatePath } from "next/cache";
import type { MutationFetcher } from "swr/mutation";

import type { Document, ENTITY_TYPE } from "@acme/prisma";
import type { ENTITY_TYPE } from "@acme/prisma";
import { type Modified } from "@acme/ui/lib";
import { DeleteDocument, type DeleteDocumentInput } from "@acme/validators";

Expand All @@ -12,9 +12,10 @@ import {
auditLogs,
createMutationFetcher,
documents,
DocumentsKey,
fetchClient,
UnauthorizedError,
type DetailedDocument,
type DocumentsKey,
} from "~/lib";

const handler = createMutationFetcher(
Expand Down Expand Up @@ -42,7 +43,7 @@ const handler = createMutationFetcher(
);

export const archiveDocument: MutationFetcher<
Modified<Document>,
Modified<DetailedDocument>,
DocumentsKey,
DeleteDocumentInput
> = ({ workspaceId }, data) => handler(workspaceId, data);
5 changes: 3 additions & 2 deletions apps/worxpace/src/actions/create-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { revalidatePath } from "next/cache";
import type { MutationFetcher } from "swr/mutation";

import { ENTITY_TYPE, type Document } from "@acme/prisma";
import type { ENTITY_TYPE } from "@acme/prisma";
import { CreateDocument, type CreateDocumentInput } from "@acme/validators";

import {
Expand All @@ -15,6 +15,7 @@ import {
generateDefaultIcon,
toIcon,
UnauthorizedError,
type DetailedDocument,
type DocumentsKey,
} from "~/lib";

Expand Down Expand Up @@ -44,7 +45,7 @@ const handler = createMutationFetcher(
);

export const createDocument: MutationFetcher<
Document,
DetailedDocument,
DocumentsKey,
CreateDocumentInput
> = ({ workspaceId }, data) => handler(workspaceId, data);
5 changes: 3 additions & 2 deletions apps/worxpace/src/actions/restore-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { revalidatePath } from "next/cache";
import type { MutationFetcher } from "swr/mutation";

import type { Document, ENTITY_TYPE } from "@acme/prisma";
import type { ENTITY_TYPE } from "@acme/prisma";
import { type Modified } from "@acme/ui/lib";
import { DeleteDocument, type DeleteDocumentInput } from "@acme/validators";

Expand All @@ -14,6 +14,7 @@ import {
documents,
fetchClient,
UnauthorizedError,
type DetailedDocument,
type DocumentsKey,
} from "~/lib";

Expand Down Expand Up @@ -42,7 +43,7 @@ const handler = createMutationFetcher(
);

export const restoreDocument: MutationFetcher<
Modified<Document>,
Modified<DetailedDocument>,
DocumentsKey,
DeleteDocumentInput
> = ({ workspaceId }, data) => handler(workspaceId, data);
7 changes: 4 additions & 3 deletions apps/worxpace/src/actions/update-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { revalidatePath } from "next/cache";
import type { MutationFetcher } from "swr/mutation";

import type { Document, ENTITY_TYPE } from "@acme/prisma";
import type { ENTITY_TYPE } from "@acme/prisma";
import { UpdateDocument, type UpdateDocumentInput } from "@acme/validators";

import {
Expand All @@ -13,6 +13,7 @@ import {
documents,
fetchClient,
UnauthorizedError,
type DetailedDocument,
type DocumentKey,
type DocumentsKey,
} from "~/lib";
Expand Down Expand Up @@ -45,12 +46,12 @@ const handler = createMutationFetcher(UpdateDocument, async (_key, { arg }) => {
});

export const updateDocument: MutationFetcher<
Document,
DetailedDocument,
DocumentsKey,
UpdateDocumentInput
> = ({ workspaceId }, data) => handler(workspaceId, data);
export const updateInternalDocument: MutationFetcher<
Document,
DetailedDocument,
DocumentKey,
UpdateDocumentInput
> = ({ documentId }, data) => handler(documentId, data);

This file was deleted.

2 changes: 1 addition & 1 deletion apps/worxpace/src/app/api/documents/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function GET(req: NextRequest) {
if (!result) return new NextResponse("Unauthorized", { status: 401 });
const documents = await db.getByWorkspace({ workspaceId, isArchived });
return NextResponse.json(documents);
} catch {
} catch (e) {
return new NextResponse("Internal Service Error", { status: 500 });
}
}
17 changes: 10 additions & 7 deletions apps/worxpace/src/components/modal/settings-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import { SettingsPanel, useSettingsStore, useWorkspace } from "@acme/ui/notion";
import type { SettingsPanelProps, SettingsStore } from "@acme/ui/notion";
import { Dialog, DialogContent } from "@acme/ui/shadcn";

import { useEdgeStore, usePlatform, useSettings } from "~/hooks";
import { useEdgeStore, usePlatform, useSettings, useSetup } from "~/hooks";
import { toIcon } from "~/lib";

export const SettingsModal = () => {
/** Route */
const router = useRouter();
const { signOut } = useAuth();
const platform = usePlatform();
const { reset } = useSettingsStore();
const store = useSettingsStore();
const { activeWorkspace, select } = useWorkspace();
const { isOpen, setClose } = useModal<SettingsStore>();
const { edgestore } = useEdgeStore();
Expand All @@ -32,16 +32,19 @@ export const SettingsModal = () => {
? { clerkId: platform.clerkId, workspaceId: activeWorkspace.id }
: null,
);
const { fetchData } = useSetup({ clerkId: platform.clerkId });
/** Settings handlers */
const settingsProps: SettingsPanelProps = {
settings,
onUpdate: async ({ account, workspace }) => {
account && (await updateAccount(account));
workspace &&
(await updateWorkspace({
if (workspace) {
await updateWorkspace({
...workspace,
icon: workspace.icon ? toIcon(workspace.icon) : undefined,
}));
});
await fetchData();
}
},
onUploadFile: async (file, options) => {
const { url } = await edgestore.publicFiles.upload({ file, options });
Expand All @@ -53,7 +56,7 @@ export const SettingsModal = () => {
select();
await signOut(() => {
platform.reset();
reset();
store.reset();
router.push("/sign-in");
});
},
Expand All @@ -62,7 +65,7 @@ export const SettingsModal = () => {
setClose();
select();
platform.update((prev) => ({ ...prev, workspaceId: "" }));
reset();
store.reset();
router.push("/select-role");
},
};
Expand Down
10 changes: 7 additions & 3 deletions apps/worxpace/src/hooks/use-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import { toast } from "sonner";
import useSWR from "swr";
import useSWRMutation from "swr/mutation";

import type { Document } from "@acme/prisma";
import { useTree } from "@acme/ui/custom";

import { updateInternalDocument } from "~/actions";
import { documentFetcher, toIconInfo, type DocumentKey } from "~/lib";
import {
DetailedDocument,
documentFetcher,
toIconInfo,
type DocumentKey,
} from "~/lib";

export const useDocument = (info: Omit<DocumentKey, "type"> | null) => {
const { dispatch } = useTree();
Expand All @@ -18,7 +22,7 @@ export const useDocument = (info: Omit<DocumentKey, "type"> | null) => {
data: page,
isLoading,
error,
} = useSWR<Document, Error>(key, documentFetcher, {
} = useSWR<DetailedDocument, Error>(key, documentFetcher, {
onSuccess: ({ title, icon }) => console.log(`[swr:page] ${title} - `, icon),
onError: (e) => console.log(`[swr:page]: ${e.message}`),
revalidateIfStale: true,
Expand Down
37 changes: 17 additions & 20 deletions apps/worxpace/src/hooks/use-documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { toast } from "sonner";
import useSWR from "swr";
import useSWRMutation from "swr/mutation";

import type { Document } from "@acme/prisma";
import { useTree } from "@acme/ui/custom";

import {
Expand All @@ -15,11 +14,16 @@ import {
restoreDocument,
updateDocument,
} from "~/actions";
import { fetchUrl, toIconInfo, type DocumentsKey } from "~/lib";
import {
fetchUrl,
toTreeItem,
type DetailedDocument,
type DocumentsKey,
} from "~/lib";

const fetcher = async ({ workspaceId }: DocumentsKey) => {
try {
return await fetchUrl<Document[]>(
return await fetchUrl<DetailedDocument[]>(
`/api/documents?workspaceId=${workspaceId}`,
);
} catch (error) {
Expand All @@ -44,33 +48,26 @@ export const useDocuments = ({
isLoading,
error,
mutate,
} = useSWR<Document[], Error>(key, fetcher, {
} = useSWR<DetailedDocument[], Error>(key, fetcher, {
onSuccess: (data) =>
dispatch({
type: "set",
payload: data.map((doc) => ({
...doc,
icon: toIconInfo(doc.icon),
group: doc.isArchived ? `trash:${doc.type}` : doc.type,
})),
}),
dispatch({ type: "set", payload: data.map(toTreeItem) }),
onError: (e) => console.log(`[swr:workspace]: ${e.message}`),
});
/** Mutations */
const onError = (e: Error) => toast.error(e.message);
const { trigger: create } = useSWRMutation(key, createDocument, {
onSuccess: (data) => {
const { id, title, parentId, icon, type: group } = data;
dispatch({
type: "add",
payload: [{ id, title, parentId, icon: toIconInfo(icon), group }],
});
toast.success(`Page Created: ${title}`);
onClickItem?.(id, group);
dispatch({ type: "add", payload: [toTreeItem(data)] });
toast.success(`Page Created: ${data.title}`);
onClickItem?.(data.id, data.type);
},
onError,
});
const { trigger: update } = useSWRMutation(key, updateDocument, { onError });
const { trigger: update } = useSWRMutation(key, updateDocument, {
onSuccess: (data) =>
dispatch({ type: "update:item", payload: toTreeItem(data) }),
onError,
});
const { trigger: archive } = useSWRMutation(key, archiveDocument, {
onSuccess: ({ item, ids }) => {
dispatch({
Expand Down
Loading

0 comments on commit 5d44cba

Please sign in to comment.