Skip to content

Commit

Permalink
Resolve "Change call api and check permalink to be onSubmit in create…
Browse files Browse the repository at this point in the history
…Post"
  • Loading branch information
func0x committed Mar 28, 2024
1 parent 606cdc6 commit 4c9b98b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 30 deletions.
18 changes: 6 additions & 12 deletions apps/blog/components/post-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ export default function PostForm({ username }: { username: string }) {
const router = useRouter();
const [preview, setPreview] = useState(true);
const [sideBySide, setSideBySide] = useState(true);
const [postPermlink, setPostPermlink] = useState('');
const { manabarsData } = useManabars(username);
const [storedPost, storePost] = useLocalStorage<AccountFormValues>('postData', defaultValues);
const { t } = useTranslation('common_blog');
Expand Down Expand Up @@ -165,22 +164,12 @@ export default function PostForm({ username }: { username: string }) {
const tagsCheck = validateTagInput(watchedValues.tags, watchedValues.category === 'blog', t);
const summaryCheck = validateSummoryInput(watchedValues.postSummary, t);
const altUsernameCheck = validateAltUsernameInput(watchedValues.author, t);
useEffect(() => {
storePost(watchedValues);
}, [JSON.stringify(watchedValues)]);

useEffect(() => {
const createPostPermlink = async () => {
const plink = await createPermlink(storedPost?.title ?? '', username, storedPost?.title ?? '');
setPostPermlink(plink);
};
createPostPermlink();
}, [username, storedPost?.title]);

async function onSubmit(data: AccountFormValues) {
const chain = await hiveChainService.getHiveChain();
const tags = storedPost?.tags.replace(/#/g, '').split(' ') ?? [];
const maxAcceptedPayout = await chain.hbd(Number(storedPost.maxAcceptedPayout));
const postPermlink = await createPermlink(storedPost?.title ?? '', username);
try {
await transactionService.post(
postPermlink,
Expand All @@ -198,6 +187,11 @@ export default function PostForm({ username }: { username: string }) {
console.error(error);
}
}

useEffect(() => {
storePost(watchedValues);
}, [JSON.stringify(watchedValues)]);

return (
<div className={clsx({ container: !sideBySide || !preview })}>
<div
Expand Down
13 changes: 0 additions & 13 deletions apps/blog/components/reply-textbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { Input } from '@ui/components/input';
import { useTranslation } from 'next-i18next';
import { useUser } from '@smart-signer/lib/auth/use-user';
import { transactionService } from '@transaction/index';
import { createPermlink } from '@transaction/lib/utils';
import { HiveRendererContext } from './hive-renderer-context';
import DialogLogin from './dialog-login';
import { useLocalStorage } from '@smart-signer/lib/use-local-storage';
Expand All @@ -28,7 +27,6 @@ export function ReplyTextbox({
const { t } = useTranslation('common_blog');
const [text, setText] = useState(storedPost ? storedPost : '');
const [cleanedText, setCleanedText] = useState('');
const [replyPermlink, setReplyPermlink] = useState('');
const { hiveRenderer } = useContext(HiveRendererContext);

useEffect(() => {
Expand All @@ -41,17 +39,6 @@ export function ReplyTextbox({
}
}, [hiveRenderer, text]);

useEffect(() => {
const createReplyPermlink = async () => {
if (user && user.isLoggedIn) {
const plink = await createPermlink('', user.username, permlink);
setReplyPermlink(plink);
}
};

createReplyPermlink();
}, [user, permlink]);

const handleCancel = () => {
localStorage.removeItem(storageId);
if (text === '') return onSetReply(false);
Expand Down
7 changes: 2 additions & 5 deletions packages/transaction/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import base58 from 'bs58';
import secureRandom from 'secure-random';
import { getPostHeader } from './bridge';

export async function createPermlink(title: string, author: string, postPermlink: string) {
export async function createPermlink(title: string, author: string) {
let permlink;
if (title && title.trim() !== '') {
let s = getSlug(title.replace(/[<>]/g, ''), { truncate: 128 });
Expand All @@ -14,10 +14,7 @@ export async function createPermlink(title: string, author: string, postPermlink
s = s.toLowerCase().replace(/[^a-z0-9-]+/g, '');

// ensure the permlink is unique
let head = undefined;
try {
head = await getPostHeader(author, postPermlink);
} catch (e) {}
let head = await getPostHeader(author, s);
if (head && !!head.category) {
const noise = base58.encode(secureRandom.randomBuffer(4)).toLowerCase();
permlink = noise + '-' + s;
Expand Down

0 comments on commit 4c9b98b

Please sign in to comment.