2023-04-26 02:37:29 +00:00
|
|
|
import type { JSX } from 'solid-js'
|
2023-05-07 15:15:30 +00:00
|
|
|
import { Accessor, createContext, createSignal, useContext } from 'solid-js'
|
2023-05-04 19:57:02 +00:00
|
|
|
import { createStore, SetStoreFunction } from 'solid-js/store'
|
2023-05-07 15:15:30 +00:00
|
|
|
import { Topic } from '../graphql/types.gen'
|
2023-05-05 20:05:50 +00:00
|
|
|
import { apiClient } from '../utils/apiClient'
|
|
|
|
import { useLocalize } from './localize'
|
|
|
|
import { useSnackbar } from './snackbar'
|
2023-05-08 17:21:06 +00:00
|
|
|
import { openPage } from '@nanostores/router'
|
|
|
|
import { router, useRouter } from '../stores/router'
|
2023-05-10 20:20:53 +00:00
|
|
|
import { slugify } from '../utils/slugify'
|
2023-04-26 02:37:29 +00:00
|
|
|
|
|
|
|
type WordCounter = {
|
|
|
|
characters: number
|
|
|
|
words: number
|
|
|
|
}
|
|
|
|
|
2023-05-04 19:57:02 +00:00
|
|
|
type ShoutForm = {
|
2023-05-07 15:15:30 +00:00
|
|
|
shoutId: number
|
2023-05-04 19:57:02 +00:00
|
|
|
slug: string
|
|
|
|
title: string
|
|
|
|
subtitle: string
|
|
|
|
selectedTopics: Topic[]
|
2023-05-10 20:20:53 +00:00
|
|
|
mainTopic?: Topic
|
2023-05-04 19:57:02 +00:00
|
|
|
body: string
|
|
|
|
coverImageUrl: string
|
|
|
|
}
|
|
|
|
|
2023-04-26 02:37:29 +00:00
|
|
|
type EditorContextType = {
|
|
|
|
isEditorPanelVisible: Accessor<boolean>
|
|
|
|
wordCounter: Accessor<WordCounter>
|
2023-05-04 19:57:02 +00:00
|
|
|
form: ShoutForm
|
2023-05-10 20:20:53 +00:00
|
|
|
formErrors: Record<keyof ShoutForm, string>
|
2023-04-26 02:37:29 +00:00
|
|
|
actions: {
|
2023-05-08 17:21:06 +00:00
|
|
|
saveShout: () => Promise<void>
|
|
|
|
publishShout: () => Promise<void>
|
|
|
|
publishShoutById: (shoutId: number) => Promise<void>
|
|
|
|
deleteShout: (shoutId: number) => Promise<boolean>
|
2023-04-26 02:37:29 +00:00
|
|
|
toggleEditorPanel: () => void
|
|
|
|
countWords: (value: WordCounter) => void
|
2023-05-04 19:57:02 +00:00
|
|
|
setForm: SetStoreFunction<ShoutForm>
|
2023-05-10 20:20:53 +00:00
|
|
|
setFormErrors: SetStoreFunction<Record<keyof ShoutForm, string>>
|
2023-04-26 02:37:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const EditorContext = createContext<EditorContextType>()
|
|
|
|
|
|
|
|
export function useEditorContext() {
|
|
|
|
return useContext(EditorContext)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const EditorProvider = (props: { children: JSX.Element }) => {
|
2023-05-05 20:05:50 +00:00
|
|
|
const { t } = useLocalize()
|
2023-05-08 17:21:06 +00:00
|
|
|
|
|
|
|
const { page } = useRouter()
|
|
|
|
|
2023-05-05 20:05:50 +00:00
|
|
|
const {
|
|
|
|
actions: { showSnackbar }
|
|
|
|
} = useSnackbar()
|
|
|
|
|
2023-05-03 16:13:48 +00:00
|
|
|
const [isEditorPanelVisible, setIsEditorPanelVisible] = createSignal<boolean>(false)
|
2023-05-04 19:57:02 +00:00
|
|
|
|
|
|
|
const [form, setForm] = createStore<ShoutForm>(null)
|
2023-05-10 20:20:53 +00:00
|
|
|
const [formErrors, setFormErrors] = createStore<Record<keyof ShoutForm, string>>(null)
|
2023-05-04 19:57:02 +00:00
|
|
|
|
2023-04-26 02:37:29 +00:00
|
|
|
const [wordCounter, setWordCounter] = createSignal<WordCounter>({
|
|
|
|
characters: 0,
|
|
|
|
words: 0
|
|
|
|
})
|
2023-05-05 20:05:50 +00:00
|
|
|
|
2023-05-03 16:13:48 +00:00
|
|
|
const toggleEditorPanel = () => setIsEditorPanelVisible((value) => !value)
|
2023-04-26 02:37:29 +00:00
|
|
|
const countWords = (value) => setWordCounter(value)
|
2023-05-05 20:05:50 +00:00
|
|
|
|
2023-05-08 17:21:06 +00:00
|
|
|
const validate = () => {
|
2023-05-05 20:05:50 +00:00
|
|
|
if (!form.title) {
|
|
|
|
setFormErrors('title', t('Required'))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-05-08 17:21:06 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-05-10 20:20:53 +00:00
|
|
|
const validateSettings = () => {
|
|
|
|
if (form.selectedTopics.length === 0) {
|
|
|
|
setFormErrors('selectedTopics', t('Required'))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
const updateShout = async ({ publish }: { publish: boolean }) => {
|
|
|
|
return apiClient.updateArticle({
|
|
|
|
shoutId: form.shoutId,
|
|
|
|
shoutInput: {
|
|
|
|
body: form.body,
|
|
|
|
topics: form.selectedTopics,
|
|
|
|
// authors?: InputMaybe<Array<InputMaybe<Scalars['String']>>>
|
|
|
|
// community?: InputMaybe<Scalars['Int']>
|
|
|
|
mainTopic: form.mainTopic,
|
|
|
|
slug: form.slug,
|
|
|
|
subtitle: form.subtitle,
|
|
|
|
title: form.title,
|
|
|
|
cover: form.coverImageUrl
|
|
|
|
},
|
|
|
|
publish
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-08 17:21:06 +00:00
|
|
|
const saveShout = async () => {
|
2023-05-10 01:46:39 +00:00
|
|
|
if (isEditorPanelVisible()) {
|
|
|
|
toggleEditorPanel()
|
|
|
|
}
|
|
|
|
|
2023-05-10 20:20:53 +00:00
|
|
|
if (page().route === 'edit' && !validate()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (page().route === 'editSettings' && !validateSettings()) {
|
2023-05-08 17:21:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-05 20:05:50 +00:00
|
|
|
try {
|
2023-05-10 20:20:53 +00:00
|
|
|
const shout = await updateShout({ publish: false })
|
2023-05-08 17:21:06 +00:00
|
|
|
|
|
|
|
if (shout.visibility === 'owner') {
|
|
|
|
openPage(router, 'drafts')
|
|
|
|
} else {
|
|
|
|
openPage(router, 'article', { slug: shout.slug })
|
|
|
|
}
|
2023-05-05 20:05:50 +00:00
|
|
|
} catch (error) {
|
2023-05-08 18:01:11 +00:00
|
|
|
console.error('[saveShout]', error)
|
2023-05-05 20:05:50 +00:00
|
|
|
showSnackbar({ type: 'error', body: t('Error') })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const publishShout = async () => {
|
2023-05-10 01:46:39 +00:00
|
|
|
if (isEditorPanelVisible()) {
|
|
|
|
toggleEditorPanel()
|
|
|
|
}
|
2023-05-10 20:20:53 +00:00
|
|
|
|
2023-05-08 17:21:06 +00:00
|
|
|
if (!validate()) {
|
|
|
|
return
|
|
|
|
}
|
2023-05-10 20:20:53 +00:00
|
|
|
|
2023-05-08 17:21:06 +00:00
|
|
|
if (page().route === 'edit') {
|
2023-05-10 20:20:53 +00:00
|
|
|
const slug = slugify(form.title)
|
2023-05-08 17:21:06 +00:00
|
|
|
setForm('slug', slug)
|
|
|
|
openPage(router, 'editSettings', { shoutId: form.shoutId.toString() })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-05 20:05:50 +00:00
|
|
|
try {
|
2023-05-10 20:20:53 +00:00
|
|
|
await updateShout({ publish: true })
|
2023-05-08 18:26:42 +00:00
|
|
|
openPage(router, 'feed')
|
2023-05-08 17:21:06 +00:00
|
|
|
} catch (error) {
|
2023-05-08 18:01:11 +00:00
|
|
|
console.error('[publishShout]', error)
|
2023-05-08 17:21:06 +00:00
|
|
|
showSnackbar({ type: 'error', body: t('Error') })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const publishShoutById = async (shoutId: number) => {
|
|
|
|
try {
|
|
|
|
await apiClient.updateArticle({
|
|
|
|
shoutId,
|
|
|
|
publish: true
|
|
|
|
})
|
|
|
|
|
|
|
|
openPage(router, 'feed')
|
|
|
|
} catch (error) {
|
2023-05-08 18:01:11 +00:00
|
|
|
console.error('[publishShoutById]', error)
|
2023-05-08 17:21:06 +00:00
|
|
|
showSnackbar({ type: 'error', body: t('Error') })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const deleteShout = async (shoutId: number) => {
|
|
|
|
try {
|
|
|
|
await apiClient.deleteShout({
|
|
|
|
shoutId
|
2023-05-05 20:05:50 +00:00
|
|
|
})
|
|
|
|
return true
|
|
|
|
} catch {
|
|
|
|
showSnackbar({ type: 'error', body: t('Error') })
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-26 02:37:29 +00:00
|
|
|
const actions = {
|
2023-05-05 20:05:50 +00:00
|
|
|
saveShout,
|
|
|
|
publishShout,
|
2023-05-08 17:21:06 +00:00
|
|
|
publishShoutById,
|
|
|
|
deleteShout,
|
2023-04-26 02:37:29 +00:00
|
|
|
toggleEditorPanel,
|
2023-05-04 19:57:02 +00:00
|
|
|
countWords,
|
2023-05-05 20:05:50 +00:00
|
|
|
setForm,
|
|
|
|
setFormErrors
|
2023-04-26 02:37:29 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 20:05:50 +00:00
|
|
|
const value: EditorContextType = { actions, form, formErrors, isEditorPanelVisible, wordCounter }
|
2023-04-26 02:37:29 +00:00
|
|
|
|
|
|
|
return <EditorContext.Provider value={value}>{props.children}</EditorContext.Provider>
|
|
|
|
}
|