diff --git a/src/pages/write/action.tsx b/src/pages/write/action.tsx index ae16b2b..447a605 100644 --- a/src/pages/write/action.tsx +++ b/src/pages/write/action.tsx @@ -1,7 +1,7 @@ import { NDKContextType } from 'contexts/NDKContext' import { ActionFunctionArgs, redirect } from 'react-router-dom' import { getBlogPageRoute } from 'routes' -import { BlogFormErrors, BlogEventSubmitForm } from 'types' +import { BlogFormErrors, BlogEventSubmitForm, BlogEventEditForm } from 'types' import { isReachable, isValidImageUrl, @@ -19,7 +19,7 @@ import { store } from 'store' export const writeRouteAction = (ndkContext: NDKContextType) => - async ({ request }: ActionFunctionArgs) => { + async ({ params, request }: ActionFunctionArgs) => { // Get the current state const userState = store.getState().user let hexPubkey: string @@ -47,7 +47,9 @@ export const writeRouteAction = const formData = await request.formData() // Parse the the data - const formSubmit = parseFormData(formData) + const formSubmit = parseFormData( + formData + ) // Check for errors const formErrors = await validateFormData(formSubmit) @@ -59,9 +61,23 @@ export const writeRouteAction = const turndownService = new TurndownService() const content = turndownService.turndown(formSubmit.content!) - const uuid = uuidv4() + // Check if we are editing or this is a new blog + const { naddr } = params + const isEditing = + naddr && request.method === 'PUT' && isEditForm(formSubmit) + const formEdit = isEditing ? formSubmit : undefined + const currentTimeStamp = now() + // Get the existing edited fields or new ones + const uuid = isEditing && formEdit?.dTag ? formSubmit.dTag : uuidv4() + const rTag = + isEditing && formEdit?.rTag ? formEdit.rTag : window.location.host + const published_at = + isEditing && formEdit?.published_at + ? formEdit.published_at + : currentTimeStamp + const aTag = `${kinds.LongFormArticle}:${hexPubkey}:${uuid}` const tTags = formSubmit .tags!.toLowerCase() @@ -76,8 +92,8 @@ export const writeRouteAction = tags: [ ['d', uuid], ['a', aTag], - ['r', window.location.host], - ['published_at', currentTimeStamp.toString()], + ['r', rTag], + ['published_at', published_at.toString()], ['title', formSubmit.title!], ['image', formSubmit.image!], ['summary', formSubmit.summary!], @@ -160,3 +176,9 @@ const validateFormData = async ( return errors } + +function isEditForm( + form: Partial +): form is BlogEventEditForm { + return (form as BlogEventEditForm).dTag !== undefined +} diff --git a/src/pages/write/index.tsx b/src/pages/write/index.tsx index 0cb1ebd..870bb2b 100644 --- a/src/pages/write/index.tsx +++ b/src/pages/write/index.tsx @@ -68,7 +68,7 @@ export const WritePage = () => { {navigation.state === 'submitting' && ( )} -
+ { name='nsfw' defaultChecked={blog?.nsfw} /> + {typeof blog?.dTag !== 'undefined' && ( + + )} + {typeof blog?.rTag !== 'undefined' && ( + + )} + {typeof blog?.published_at !== 'undefined' && ( + + )}