Skip to content

Commit

Permalink
fix case for new posts and drafts
Browse files Browse the repository at this point in the history
  • Loading branch information
juandjara committed Nov 14, 2023
1 parent fec2d3d commit e0b492e
Showing 1 changed file with 36 additions and 38 deletions.
74 changes: 36 additions & 38 deletions app/routes/p/$project/$cid/$pid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,46 +108,44 @@ export async function action({ request, params }: ActionArgs) {

const content = matter ? ['---', matter, '---', '', body].join('\n') : body

if (!isNew) {
const isDraft = formData.get('draft') === 'true'
if (isDraft) {
await saveDraft({
project,
file: {
id: sha,
path: fullPath,
body,
title: title || getBasename(fullPath),
attributes: Object.fromEntries(
(matter || '').split('\n').filter(Boolean).map(line => {
const [key, ...value] = line.split(':')
return [key.trim(), value.join(':').trim()]
})
)
}
})
const cookie = await setFlashMessage(request, `Saved draft for "${getBasename(fullPath)}" successfully`)
return json({ ok: true }, {
headers: {
'Set-Cookie': cookie
}
})
}

const isDeleteDraft = formData.get('delete_draft') === 'true'
if (isDeleteDraft) {
await deleteDraft(project, fullPath)
const cookie = await setFlashMessage(request, `Draft for "${getBasename(fullPath)}" deleted successfully`)
return json({ ok: true }, {
headers: {
'Set-Cookie': cookie
}
})
}
const isDraft = formData.get('draft') === 'true'
if (isDraft) {
await saveDraft({
project,
file: {
id: sha || '',
path: fullPath,
body,
title: title || getBasename(fullPath),
attributes: Object.fromEntries(
(matter || '').split('\n').filter(Boolean).map(line => {
const [key, ...value] = line.split(':')
return [key.trim(), value.join(':').trim()]
})
)
}
})
const cookie = await setFlashMessage(request, `Saved draft for "${getBasename(fullPath)}" successfully`)
return json({ ok: true }, {
headers: {
'Set-Cookie': cookie
}
})
}

const isDeleteDraft = formData.get('delete_draft') === 'true'
if (isDeleteDraft) {
await deleteDraft(project, fullPath)
const cookie = await setFlashMessage(request, `Draft for "${getBasename(fullPath)}" deleted successfully`)
return json({ ok: true }, {
headers: {
'Set-Cookie': cookie
}
})
}

await deleteDraft(project, fullPath)

try {
await saveFile(token, {
branch,
Expand Down Expand Up @@ -194,7 +192,7 @@ export default function PostDetails() {
const debouncedSubmit = useMemo(
() => debounce(
() => {
if (formRef.current) {
if (formRef.current && !isNew) {
const fd = new FormData(formRef.current)
fd.set('draft', 'true')
fetcher.submit(fd, {
Expand All @@ -207,7 +205,7 @@ export default function PostDetails() {
},
AUTOSAVE_INTERVAL
),
[fetcher]
[fetcher, isNew]
)

function onTouched() {
Expand Down

0 comments on commit e0b492e

Please sign in to comment.