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

Entry post editing/publishing enhancements #135

Merged
merged 8 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/any.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: PR build
on:
push:
branches:
- 'bugfix/*'
- 'feature/*'
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: npm install, lint and/or test
run: |
yarn
- name: build
run: yarn build
env:
CI: true
9 changes: 9 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ const config = {
source: "/:author(@.+)/:permlink/edit",
destination: "/entry/created/:author/:permlink/edit"
},
// As We added preview once
{
source: "/:category/:author(@.+)/:permlink/preview",
destination: "/entry/:category/:author/:permlink"
},
{
source: "/:author(@.+)/:permlink/preview",
destination: "/entry/created/:author/:permlink"
},
{
source: "/:category/:author(@.+)/:permlink",
destination: "/entry/:category/:author/:permlink"
Expand Down
10 changes: 8 additions & 2 deletions src/api/mutations/update-reply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { CommentOptions, Entry, MetaData } from "@/entities";
import { comment, formatError } from "@/api/operations";
import { error } from "@/features/shared";
import { EcencyEntriesCacheManagement } from "@/core/caches";
import { useValidatePostUpdating } from "@/api/mutations/validate-post-updating";

export function useUpdateReply(entry?: Entry | null, onSuccess?: () => void) {
export function useUpdateReply(entry?: Entry | null, onSuccess?: () => Promise<void>) {
const activeUser = useGlobalStore((state) => state.activeUser);

const { mutateAsync: validatePostUpdating } = useValidatePostUpdating();
const { updateEntryQueryData } = EcencyEntriesCacheManagement.useUpdateEntry();

return useMutation({
Expand Down Expand Up @@ -39,6 +41,11 @@ export function useUpdateReply(entry?: Entry | null, onSuccess?: () => void) {
options ?? null,
point
);
try {
await validatePostUpdating({ entry, text });
await onSuccess?.();
} catch (e) {}

return {
...entry,
json_metadata: jsonMeta,
Expand All @@ -54,7 +61,6 @@ export function useUpdateReply(entry?: Entry | null, onSuccess?: () => void) {

// remove reply draft
ss.remove(`reply_draft_${entry.author}_${entry.permlink}`);
onSuccess?.();
},
onError: (e) => error(...formatError(e))
});
Expand Down
22 changes: 22 additions & 0 deletions src/api/mutations/validate-post-updating.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useMutation } from "@tanstack/react-query";
import { Entry } from "@/entities";
import { getPost } from "@/api/bridge";

export function useValidatePostUpdating() {
return useMutation({
mutationKey: ["validate-post-updating"],
mutationFn: async ({ entry, text, title }: { entry: Entry; text: string; title?: string }) => {
const response = await getPost(entry.author, entry.permlink);

if (title && response?.title !== title) {
throw new Error("[PostUpdateValidation] Post title isn`t matching yet");
}

if (response?.body !== text) {
throw new Error("[PostUpdateValidation] Post text isn`t matching yet");
}
},
retry: 3,
retryDelay: 3000
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,14 @@ export function EntryFooterControls({ entry }: Props) {
entry={entry}
alignBottom={true}
separatedSharing={true}
toggleEdit={() => router.push(`/${entry.url}/edit`)}
toggleEdit={() => {
if (typeof entry.parent_author === "string") {
// It will trigger in-place editor
router.push(`/${entry.category}/@${entry.author}/${entry.permlink}?edit=true`);
} else {
router.push(`/${entry.url}/edit`);
}
}}
/>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function EntryPageBodyViewer({ entry, rawParam, isEdit }: Props) {
</SelectionPopover>
</>
)}
{isEdit && <EntryPageEdit entry={entry} />}
<EntryPageEdit entry={entry} isEdit={isEdit} />
</EntryPageViewerManager>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,31 @@ import { Entry } from "@/entities";
import { useRouter } from "next/navigation";
import { EntryPageContext } from "@/app/(dynamicPages)/entry/[category]/[author]/[permlink]/_components/context";
import { useUpdateReply } from "@/api/mutations";
import { makeJsonMetaDataReply } from "@/utils";
import { delay, makeJsonMetaDataReply } from "@/utils";
import appPackage from "../../../../../../../../package.json";
import { useContext } from "react";
import { useContext, useEffect, useState } from "react";
import useLocalStorage from "react-use/lib/useLocalStorage";
import { PREFIX } from "@/utils/local-storage";
import { EcencyEntriesCacheManagement } from "@/core/caches";

export interface Props {
entry: Entry;
isEdit: boolean;
}

export function EntryPageEdit({ entry }: Props) {
export function EntryPageEdit({ entry: initialEntry, isEdit }: Props) {
const router = useRouter();
const { data: entry } = EcencyEntriesCacheManagement.getEntryQuery(initialEntry).useClientQuery();

const [_, __, clearText] = useLocalStorage(PREFIX + "_c_t", "");
const { commentsInputRef } = useContext(EntryPageContext);
const [isLoading, setIsLoading] = useState(false);

const { mutateAsync: updateReplyApi, isPending: isUpdateReplyLoading } = useUpdateReply(
entry,
() => {
router.push(entry.url);
// reload(); TODO
}
);
const { mutateAsync: updateReplyApi, isPending } = useUpdateReply(entry, async () => {
setIsLoading(true);
await delay(2000);
router.push(`/${entry?.category}/@${entry?.author}/${entry?.permlink}`);
});
const updateReply = async (text: string) => {
if (entry) {
return updateReplyApi({
Expand All @@ -37,17 +42,29 @@ export function EntryPageEdit({ entry }: Props) {
return;
};

useEffect(() => {
if (!isEdit) {
setIsLoading(false);
clearText();
}
}, [entry?.author, entry?.permlink, isEdit, clearText]);

return (
<Comment
defText={entry.body}
submitText={i18next.t("g.update")}
entry={entry}
onSubmit={updateReply}
cancellable={true}
onCancel={() => router.back()}
inProgress={isUpdateReplyLoading}
autoFocus={true}
inputRef={commentsInputRef}
/>
isEdit && (
<div className="relative">
<Comment
defText={entry!.body}
submitText={i18next.t("g.update")}
entry={entry!!}
onSubmit={updateReply}
clearOnSubmit={false}
cancellable={true}
onCancel={() => router.back()}
inProgress={isLoading || isPending}
autoFocus={true}
inputRef={commentsInputRef}
/>
</div>
)
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default async function EntryPage({
<span itemScope={true} itemType="http://schema.org/Article">
<EntryPageContent
category={category}
isEdit={false}
isEdit={searchParams["edit"] === "true" ?? false}
entry={entry}
rawParam={searchParams["raw"] ?? ""}
/>
Expand Down

This file was deleted.

3 changes: 1 addition & 2 deletions src/app/client-providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import React, { PropsWithChildren } from "react";
import { QueryClientProvider } from "@tanstack/react-query";
import { UIManager } from "@ui/core";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
import { ChatProvider } from "@/app/chat-provider";
import { ClientInit } from "@/app/client-init";
import { makeQueryClient } from "@/core/react-query";
Expand Down Expand Up @@ -37,7 +36,7 @@ export function ClientProviders(props: PropsWithChildren) {
</PushNotificationsProvider>
<Announcements />
</UIManager>
<ReactQueryDevtools initialIsOpen={false} />
{/*<ReactQueryDevtools initialIsOpen={false} />*/}
</QueryClientProvider>
);
}
27 changes: 26 additions & 1 deletion src/app/submit/_api/publish.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import * as bridgeApi from "../../../api/bridge";
import { getPost } from "../../../api/bridge";
import { markAsPublished, updateSpeakVideoInfo } from "@/api/threespeak";
import { comment, formatError, reblog } from "@/api/operations";
import { useThreeSpeakManager } from "../_hooks";
Expand All @@ -12,6 +13,7 @@ import { useGlobalStore } from "@/core/global-store";
import { BeneficiaryRoute, Entry, FullAccount, RewardType } from "@/entities";
import {
createPermlink,
delay,
isCommunity,
makeApp,
makeCommentOptions,
Expand All @@ -25,6 +27,27 @@ import { useRouter } from "next/navigation";
import { QueryIdentifiers } from "@/core/react-query";
import { EcencyEntriesCacheManagement } from "@/core/caches";

/**
* Helps to validate if post was really created on Blockchain
*/
async function validatePostCreating(author: string, permlink: string, attempts = 0) {
if (attempts === 3) {
return;
}

let response: Entry | undefined;
try {
response = await getPost(author, permlink);
} catch (e) {
response = undefined;
}
if (!response) {
await delay(3000);
attempts += 1;
return validatePostCreating(author, permlink, attempts);
}
}

export function usePublishApi(onClear: () => void) {
const queryClient = useQueryClient();
const router = useRouter();
Expand Down Expand Up @@ -155,11 +178,13 @@ export function usePublishApi(onClear: () => void) {
};
updateEntryQueryData([entry]);

await validatePostCreating(entry.author, entry.permlink);

success(i18next.t("submit.published"));
onClear();
clearActivePoll();
const newLoc = makeEntryPath(parentPermlink, author, permlink);
router.push(newLoc + "/preview");
router.push(newLoc);

//Mark speak video as published
if (!!unpublished3SpeakVideo && activeUser.username === unpublished3SpeakVideo.owner) {
Expand Down
Loading
Loading