catch-response-on-update

This commit is contained in:
Untone 2024-02-17 18:03:01 +03:00
parent 3a6faa65a8
commit 0dd2736dd5
3 changed files with 30 additions and 13 deletions

View File

@ -70,10 +70,10 @@ export const PublishSettings = (props: Props) => {
return { return {
coverImageUrl: props.form?.coverImageUrl, coverImageUrl: props.form?.coverImageUrl,
mainTopic: props.form?.mainTopic || EMPTY_TOPIC, mainTopic: props.form?.mainTopic || EMPTY_TOPIC,
slug: props.form?.slug, slug: props.form?.slug || '',
title: props.form?.title, title: props.form?.title || '',
subtitle: props.form?.subtitle, subtitle: props.form?.subtitle || '',
description: composeDescription(), description: composeDescription() || '',
selectedTopics: [], selectedTopics: [],
} }
}) })

View File

@ -161,7 +161,11 @@ export const EditorProvider = (props: { children: JSX.Element }) => {
} }
try { try {
const shout = await updateShout(formToSave, { publish: false }) const { shout, error } = await updateShout(formToSave, { publish: false })
if (error) {
snackbar?.showSnackbar({ type: 'error', body: localize?.t(error) || '' })
return
}
removeDraftFromLocalStorage(formToSave.shoutId) removeDraftFromLocalStorage(formToSave.shoutId)
if (shout?.published_at) { if (shout?.published_at) {
@ -176,24 +180,33 @@ export const EditorProvider = (props: { children: JSX.Element }) => {
} }
const saveDraft = async (draftForm: ShoutForm) => { const saveDraft = async (draftForm: ShoutForm) => {
await updateShout(draftForm, { publish: false }) const { error } = await updateShout(draftForm, { publish: false })
if (error) {
snackbar?.showSnackbar({ type: 'error', body: localize?.t(error) || '' })
return
}
} }
const publishShout = async (formToPublish: ShoutForm) => { const publishShout = async (formToPublish: ShoutForm) => {
if (isEditorPanelVisible()) { const editorPanelVisible = isEditorPanelVisible()
const pageRoute = page()?.route
if (editorPanelVisible) {
toggleEditorPanel() toggleEditorPanel()
} }
if (page()?.route === 'edit') { if (pageRoute === 'edit') {
if (!validate()) { if (!validate()) {
return return
} }
await updateShout(formToPublish, { publish: false })
const slug = slugify(form.title) const slug = slugify(form.title)
setForm('slug', slug) setForm('slug', slug)
openPage(router, 'editSettings', { shoutId: form.shoutId.toString() }) openPage(router, 'editSettings', { shoutId: form.shoutId.toString() })
const { error } = await updateShout(formToPublish, { publish: false })
if (error) {
snackbar?.showSnackbar({ type: 'error', body: localize?.t(error) || '' })
}
return return
} }
@ -202,7 +215,11 @@ export const EditorProvider = (props: { children: JSX.Element }) => {
} }
try { try {
await updateShout(formToPublish, { publish: true }) const { error } = await updateShout(formToPublish, { publish: true })
if (error) {
snackbar?.showSnackbar({ type: 'error', body: localize?.t(error) || '' })
return
}
openPage(router, 'feed') openPage(router, 'feed')
} catch (error) { } catch (error) {
console.error('[publishShout]', error) console.error('[publishShout]', error)

View File

@ -157,12 +157,12 @@ export const apiClient = {
shout_id: number shout_id: number
shout_input?: ShoutInput shout_input?: ShoutInput
publish: boolean publish: boolean
}): Promise<Shout> => { }): Promise<CommonResult> => {
const response = await apiClient.private const response = await apiClient.private
.mutation(updateArticle, { shout_id, shout_input, publish }) .mutation(updateArticle, { shout_id, shout_input, publish })
.toPromise() .toPromise()
console.debug('[graphql.client.core] updateArticle:', response.data) console.debug('[graphql.client.core] updateArticle:', response.data)
return response.data.update_shout.shout return response.data.update_shout
}, },
deleteShout: async (params: MutationDelete_ShoutArgs): Promise<void> => { deleteShout: async (params: MutationDelete_ShoutArgs): Promise<void> => {
const response = await apiClient.private.mutation(deleteShout, params).toPromise() const response = await apiClient.private.mutation(deleteShout, params).toPromise()