Skip to content

Commit

Permalink
Fixed Preview #67
Browse files Browse the repository at this point in the history
  • Loading branch information
chikobara committed Aug 29, 2024
1 parent 669c496 commit c991f2e
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 51 deletions.
99 changes: 85 additions & 14 deletions convex/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,14 @@ export const getFavSidebar = query({
if (!identity) {
throw new Error("Not authenticated");
}
const userId = identity.subject;

const documents = await ctx.db
.query("documents")
.withIndex("by_user_parent", (q) =>
q.eq("userId", userId).eq("parentDocument", args.parentDocument),
)
.filter((q) => q
.eq(q.field("isArchived"), false) )
.filter((q) => q
.eq(q.field("isFav"), true) )
.order("desc")
.collect();

return documents;
},
});
Expand Down Expand Up @@ -326,15 +320,8 @@ export const getById = query({
export const getByBlockId = query({
args: { bid: v.string() },
handler: async (ctx, args) => {
const identity = await ctx.auth.getUserIdentity();

if (!identity) {
throw new Error("Not authenticated");
}
const userId = identity.subject;
const document = await ctx.db
.query("documents")
.withIndex("by_user", (q) => q.eq("userId", userId))
.filter((q) => q.eq(q.field("blockId"), args.bid))
.first();
return document;
Expand All @@ -359,7 +346,8 @@ export const update = mutation({
handler: async (ctx, args) => {
const identity = await ctx.auth.getUserIdentity();
if (!identity) {
throw new Error("Not authenticated");
// throw new Error("Not authenticated");
return null;
}

const userId = identity.subject;
Expand Down Expand Up @@ -426,4 +414,87 @@ export const removeCoverImage = mutation({
})
return document;
}
});

export const publish = mutation({
args: { id: v.id("documents") },
handler: async (ctx, args) => {
const identity = await ctx.auth.getUserIdentity();
if (!identity) {
throw new Error("Not authenticated");
}
const userId = identity.subject;

const existingDocument = await ctx.db.get(args.id);
if (!existingDocument) {
throw new Error("Not found");
}

if (existingDocument.userId !== userId) {
throw new Error("Unauthorized");
}
const recursivePublish = async (documentId: Id<"documents">) => {
const children = await ctx.db
.query("documents")
.withIndex("by_user_parent", (q) =>
q.eq("userId", userId).eq("parentDocument", documentId),
)
.collect();

for (const child of children) {
await ctx.db.patch(child._id, {
isPublished: true,
});
await recursivePublish(child._id);
}
};

const document = await ctx.db.patch(args.id, {
isPublished: true,
});
recursivePublish(args.id);
return document;
},
});


export const unPublish = mutation({
args: { id: v.id("documents") },
handler: async (ctx, args) => {
const identity = await ctx.auth.getUserIdentity();
if (!identity) {
throw new Error("Not authenticated");
}
const userId = identity.subject;

const existingDocument = await ctx.db.get(args.id);
if (!existingDocument) {
throw new Error("Not found");
}

if (existingDocument.userId !== userId) {
throw new Error("Unauthorized");
}
const recursiveUnPublish = async (documentId: Id<"documents">) => {
const children = await ctx.db
.query("documents")
.withIndex("by_user_parent", (q) =>
q.eq("userId", userId).eq("parentDocument", documentId),
)
.collect();

for (const child of children) {
await ctx.db.patch(child._id, {
isPublished: false,
});
await recursiveUnPublish(child._id);
}
};

const document = await ctx.db.patch(args.id, {
isPublished: false,
});
recursiveUnPublish(args.id);
return document;
},
});
23 changes: 4 additions & 19 deletions src/app/(main)/_components/publish.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ interface PublishProps {

export const Publish = ({ initialData }: PublishProps) => {
const origin = useOrigin();
const update = useMutation(api.documents.update);
const publish = useMutation(api.documents.publish);
const unPublish = useMutation(api.documents.unPublish);
const { dict } = useLocalization();

const [copied, setCopied] = useState(false);
Expand All @@ -32,31 +33,15 @@ export const Publish = ({ initialData }: PublishProps) => {
const onPublish = () => {
setIsSubmitting(true);

const promise = update({
id: initialData._id,
isPublished: true,
}).finally(() => setIsSubmitting(false));

toast.promise(promise, {
loading: dict.main.components.Publish.toastPublishLoading,
success: dict.main.components.Publish.toastPublishSuccess,
error: dict.main.components.Publish.toastPublishError,
});
publish({ id: initialData._id }).finally(() => setIsSubmitting(false));
};

const onUnpublish = () => {
setIsSubmitting(true);

const promise = update({
unPublish({
id: initialData._id,
isPublished: false,
}).finally(() => setIsSubmitting(false));

toast.promise(promise, {
loading: dict.main.components.Publish.toastUnpublishLoading,
success: dict.main.components.Publish.toastUnpublishSuccess,
error: dict.main.components.Publish.toastUnpublishError,
});
};

const onCopy = () => {
Expand Down
17 changes: 1 addition & 16 deletions src/app/(public)/(routes)/preview/[documentId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,6 @@ export default function DocumentIdPage({ params }: DocumentIdPageProps) {
documentId: params.documentId as Id<"documents">,
});

// Mutation function to update the document
const update = useMutation(api.documents.update);

// Handler function for updating the document content
const onChange = (content: string) => {
update({
id: params.documentId,
content,
});
};

// Show a spinner while documents are loading
if (document === undefined) {
return (
Expand Down Expand Up @@ -73,11 +62,7 @@ export default function DocumentIdPage({ params }: DocumentIdPageProps) {
>
<div className="mt-2">
<Toolbar preview initialData={document} />
<Editor
editable={false}
onChange={() => {}}
initialData={document}
/>
<Editor editable={false} initialData={document} />
</div>
</div>
</div>
Expand Down
8 changes: 6 additions & 2 deletions src/components/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import { Divider } from "@nextui-org/divider";
import { useLocalization } from "@/app/(main)/contexts/LocalizationContext";

interface EditorProps {
onChange: (value: string) => void;
onChange?: (value: string) => void;
initialData: Doc<"documents">;
editable?: boolean;
}
Expand Down Expand Up @@ -187,7 +187,11 @@ const Editor = ({ onChange, initialData, editable }: EditorProps) => {
<div>
<div
onClick={() => {
router.push(`/documents/${document._id}`);
if (editable) {
router.push(`/documents/${document._id}`);
} else {
router.push(`/preview/${document._id}`);
}
}} // redirect to the page
role="button"
className="select-none bg-default/40 hover:bg-default/65 rounded-md flex py-1.5 break-words px-20 w-fit font text-medium transition-all text-muted-foreground inline-content"
Expand Down

1 comment on commit c991f2e

@vercel
Copy link

@vercel vercel bot commented on c991f2e Aug 29, 2024

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

memoir – ./

open-memoir.vercel.app
memoir-mapp.vercel.app
memoir-git-main-mapp.vercel.app

Please sign in to comment.