Skip to content

Commit 3e81c53

Browse files
Merge branch 'develop' into feat/e2e-testing-for-article-sorting
2 parents a17ad5b + 62b1847 commit 3e81c53

File tree

13 files changed

+328
-103
lines changed

13 files changed

+328
-103
lines changed

app/(app)/create/[[...paramsArr]]/page.tsx

Lines changed: 0 additions & 16 deletions
This file was deleted.

app/(app)/layout.tsx

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,22 +79,13 @@ export default async function RootLayout({
7979

8080
return (
8181
<>
82-
<ProgressBar />
83-
<AuthProvider>
84-
<ThemeProvider>
85-
<TRPCReactProvider headers={headers()}>
86-
<PromptProvider>
87-
<Nav
88-
session={session}
89-
algoliaSearchConfig={algoliaSearchConfig}
90-
username={userData?.username || null}
91-
/>
92-
{children}
93-
<Footer />
94-
</PromptProvider>
95-
</TRPCReactProvider>
96-
</ThemeProvider>
97-
</AuthProvider>
82+
<Nav
83+
session={session}
84+
algoliaSearchConfig={algoliaSearchConfig}
85+
username={userData?.username || null}
86+
/>
87+
{children}
88+
<Footer />
9889
</>
9990
);
10091
}

app/(app)/my-posts/_client.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { useSearchParams } from "next/navigation";
1818
import { api } from "@/server/trpc/react";
1919
import { Tabs } from "@/components/Tabs";
2020
import { PromptDialog } from "@/components/PromptService";
21-
import { PostStatus, getPostStatus } from "@/utils/post";
21+
import { status, getPostStatus } from "@/utils/post";
2222

2323
function classNames(...classes: string[]) {
2424
return classes.filter(Boolean).join(" ");
@@ -135,7 +135,7 @@ const MyPosts = () => {
135135
}) => {
136136
const postStatus = published
137137
? getPostStatus(new Date(published))
138-
: PostStatus.DRAFT;
138+
: status.DRAFT;
139139
return (
140140
<article
141141
className="mb-4 border border-neutral-300 bg-white p-4 dark:bg-neutral-900"
@@ -158,11 +158,11 @@ const MyPosts = () => {
158158
</p>
159159
<div className="flex items-center">
160160
<div className="flex-grow">
161-
{published && postStatus === PostStatus.SCHEDULED ? (
161+
{published && postStatus === status.SCHEDULED ? (
162162
<>
163163
{renderDate("Scheduled to publish on ", published)}
164164
</>
165-
) : published && postStatus === PostStatus.PUBLISHED ? (
165+
) : published && postStatus === status.PUBLISHED ? (
166166
<>
167167
{/*If updatedAt is greater than published by more than on minutes show updated at else show published
168168
as on updating published updatedAt is automatically updated and is greater than published*/}
@@ -174,7 +174,7 @@ const MyPosts = () => {
174174
<>{renderDate("Published on ", published)}</>
175175
)}
176176
</>
177-
) : postStatus === PostStatus.DRAFT ? (
177+
) : postStatus === status.DRAFT ? (
178178
<>{renderDate("Last updated on ", updatedAt)}</>
179179
) : null}
180180
</div>

app/(app)/create/[[...paramsArr]]/_client.tsx renamed to app/(editor)/create/[[...paramsArr]]/_client.tsx

Lines changed: 53 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,23 @@ import { useMarkdownHotkeys } from "@/markdoc/editor/hotkeys/hotkeys.markdoc";
2222
import { useMarkdownShortcuts } from "@/markdoc/editor/shortcuts/shortcuts.markdoc";
2323
import { markdocComponents } from "@/markdoc/components";
2424
import { config } from "@/markdoc/config";
25-
import { useParams, useRouter } from "next/navigation";
25+
import { notFound, useParams, useRouter } from "next/navigation";
2626
import { usePrompt } from "@/components/PromptService";
2727
import { Switch } from "@/components/Switch/Switch";
2828
import copy from "copy-to-clipboard";
29-
import { PostStatus, getPostStatus, isValidScheduleTime } from "@/utils/post";
29+
import {
30+
type PostStatus,
31+
getPostStatus,
32+
isValidScheduleTime,
33+
status,
34+
} from "@/utils/post";
3035
import { ImageUp, LoaderCircle } from "lucide-react";
3136
import { uploadFile } from "@/utils/s3helpers";
3237
import { getUploadUrl } from "@/app/actions/getUploadUrl";
38+
import EditorNav from "./navigation";
39+
import { type Session } from "next-auth";
3340

34-
const Create = () => {
41+
const Create = ({ session }: { session: Session | null }) => {
3542
const params = useParams();
3643
const router = useRouter();
3744

@@ -52,6 +59,7 @@ const Create = () => {
5259
const [uploadStatus, setUploadStatus] = useState<
5360
"loading" | "error" | "success" | "default"
5461
>("default");
62+
const [postStatus, setPostStatus] = useState<PostStatus | null>(null);
5563

5664
const { unsavedChanges: _unsaved, setUnsavedChanges: _setUnsaved } =
5765
usePrompt();
@@ -186,16 +194,10 @@ const Create = () => {
186194
toast.error("Error saving");
187195
}
188196

189-
if (isSuccess) {
190-
toast.success("Saved");
191-
}
192-
193197
if (draftFetchError) {
194-
toast.error(
195-
"Something went wrong fetching your draft, refresh your page or you may lose data",
196-
);
198+
notFound();
197199
}
198-
}, [draftFetchError, isError, isSuccess]);
200+
}, [draftFetchError, isError]);
199201

200202
useEffect(() => {
201203
if (shouldRefetch) {
@@ -227,7 +229,6 @@ const Create = () => {
227229
await create({ ...formData });
228230
} else {
229231
await save({ ...formData, id: postId });
230-
toast.success("Saved");
231232
setSavedTime(
232233
new Date().toLocaleString(undefined, {
233234
dateStyle: "medium",
@@ -243,9 +244,9 @@ const Create = () => {
243244
saveStatus === "loading" ||
244245
dataStatus === "loading";
245246

246-
const postStatus = data?.published
247+
const currentPostStatus = data?.published
247248
? getPostStatus(new Date(data.published))
248-
: PostStatus.DRAFT;
249+
: status.DRAFT;
249250

250251
const onSubmit = async (inputData: SavePostInput) => {
251252
// validate markdoc syntax
@@ -264,7 +265,7 @@ const Create = () => {
264265

265266
await savePost();
266267

267-
if (postStatus === PostStatus.PUBLISHED) {
268+
if (currentPostStatus === status.PUBLISHED) {
268269
if (data) {
269270
router.push(`/articles/${data.slug}`);
270271
}
@@ -341,10 +342,21 @@ const Create = () => {
341342
published: published ? published : undefined,
342343
});
343344
setIsPostScheduled(published ? new Date(published) > new Date() : false);
345+
setPostStatus(
346+
published ? getPostStatus(new Date(published)) : status.DRAFT,
347+
);
344348
}, [data]);
345349

346350
useEffect(() => {
347-
if (postStatus !== PostStatus.DRAFT) return;
351+
if ((title + body).length < 5) {
352+
setPostStatus(null);
353+
} else if (postStatus === null) {
354+
setPostStatus(status.DRAFT);
355+
}
356+
}, [title, body]);
357+
358+
useEffect(() => {
359+
if (currentPostStatus !== status.DRAFT) return;
348360
if ((title + body).length < 5) return;
349361
if (debouncedValue === (data?.title || "") + data?.body) return;
350362
if (allowUpdate) savePost();
@@ -374,8 +386,21 @@ const Create = () => {
374386
}
375387
}, [publishStatus, publishData, isPostScheduled, router]);
376388

389+
const handlePublish = () => {
390+
if (isDisabled) return;
391+
setOpen(true);
392+
};
393+
377394
return (
378395
<>
396+
<EditorNav
397+
session={session}
398+
username={session?.user?.name || null}
399+
postStatus={postStatus}
400+
unsavedChanges={unsavedChanges}
401+
onPublish={handlePublish}
402+
isDisabled={isDisabled}
403+
/>
379404
<form onSubmit={handleSubmit(onSubmit)}>
380405
<Transition show={open} as={Fragment}>
381406
<div className="old-input fixed bottom-0 left-0 top-0 z-50 h-screen w-full overflow-y-scroll bg-white dark:bg-black">
@@ -549,7 +574,7 @@ const Create = () => {
549574
</Disclosure>
550575
</div>
551576
<div className="mt-4 flex w-full justify-end sm:col-span-12 sm:mt-0">
552-
{postStatus === PostStatus.DRAFT && (
577+
{currentPostStatus === status.DRAFT && (
553578
<button
554579
type="button"
555580
disabled={isDisabled}
@@ -578,14 +603,17 @@ const Create = () => {
578603
</>
579604
) : (
580605
<>
581-
{postStatus === PostStatus.DRAFT &&
582-
(isPostScheduled ? "Schedule" : "Publish now")}
583-
{postStatus === PostStatus.SCHEDULED &&
584-
(isPostScheduled
585-
? "Save changes"
586-
: "Publish now")}
587-
{postStatus === PostStatus.PUBLISHED &&
588-
"Save changes"}
606+
{currentPostStatus === status.PUBLISHED
607+
? "Save changes"
608+
: currentPostStatus === status.DRAFT
609+
? isPostScheduled
610+
? "Schedule"
611+
: "Publish now"
612+
: currentPostStatus === status.SCHEDULED
613+
? isPostScheduled
614+
? "Update schedule"
615+
: "Publish now"
616+
: "Publish"}
589617
</>
590618
)}
591619
</button>
@@ -743,36 +771,6 @@ const Create = () => {
743771
inputRef={textareaRef}
744772
/>
745773
</div>
746-
747-
<div className="flex items-center justify-between">
748-
<>
749-
{saveStatus === "loading" && (
750-
<p>Auto-saving...</p>
751-
)}
752-
{saveStatus === "error" && savedTime && (
753-
<p className="text-xs text-red-600 lg:text-sm">
754-
{`Error saving, last saved: ${savedTime.toString()}`}
755-
</p>
756-
)}
757-
{saveStatus === "success" && savedTime && (
758-
<p className="text-xs text-neutral-400 lg:text-sm">
759-
{`Saved: ${savedTime.toString()}`}
760-
</p>
761-
)}
762-
</>
763-
<div />
764-
765-
<div className="flex">
766-
<button
767-
type="button"
768-
disabled={isDisabled}
769-
className="ml-5 inline-flex justify-center bg-gradient-to-r from-orange-400 to-pink-600 px-4 py-2 text-sm font-medium text-white shadow-sm hover:from-orange-300 hover:to-pink-500 focus:outline-none focus:ring-2 focus:ring-pink-300 focus:ring-offset-2 disabled:opacity-50"
770-
onClick={() => setOpen(true)}
771-
>
772-
Next
773-
</button>
774-
</div>
775-
</div>
776774
</div>
777775
)}
778776
</div>

0 commit comments

Comments
 (0)