2023-11-28 13:18:25 +00:00
|
|
|
import type { ProfileInput } from '../graphql/schema/core.gen'
|
2023-11-14 15:10:00 +00:00
|
|
|
|
2024-02-04 11:25:21 +00:00
|
|
|
import { JSX, createContext, createEffect, useContext } from 'solid-js'
|
2022-12-01 17:16:14 +00:00
|
|
|
import { createStore } from 'solid-js/store'
|
2023-11-14 15:10:00 +00:00
|
|
|
|
2023-12-19 09:34:24 +00:00
|
|
|
import { apiClient } from '../graphql/client/core'
|
2023-11-14 15:10:00 +00:00
|
|
|
|
|
|
|
import { useSession } from './session'
|
2022-12-01 17:16:14 +00:00
|
|
|
|
2023-11-23 18:15:06 +00:00
|
|
|
type ProfileFormContextType = {
|
|
|
|
form: ProfileInput
|
2024-02-04 17:40:15 +00:00
|
|
|
setForm: (profile: ProfileInput) => void
|
|
|
|
submit: (profile: ProfileInput) => Promise<void>
|
|
|
|
updateFormField: (fieldName: string, value: string, remove?: boolean) => void
|
2023-11-23 18:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const ProfileFormContext = createContext<ProfileFormContextType>()
|
|
|
|
|
|
|
|
export function useProfileForm() {
|
|
|
|
return useContext(ProfileFormContext)
|
|
|
|
}
|
|
|
|
|
2023-09-18 16:33:22 +00:00
|
|
|
const userpicUrl = (userpic: string) => {
|
2024-02-04 09:03:15 +00:00
|
|
|
if (userpic?.includes('assets.discours.io')) {
|
2023-09-18 16:33:22 +00:00
|
|
|
return userpic.replace('100x', '500x500')
|
|
|
|
}
|
|
|
|
return userpic
|
|
|
|
}
|
2023-11-23 18:15:06 +00:00
|
|
|
export const ProfileFormProvider = (props: { children: JSX.Element }) => {
|
2023-12-20 16:54:20 +00:00
|
|
|
const { author } = useSession()
|
2023-11-23 18:15:06 +00:00
|
|
|
const [form, setForm] = createStore<ProfileInput>({})
|
|
|
|
|
2022-12-07 08:37:40 +00:00
|
|
|
const submit = async (profile: ProfileInput) => {
|
2024-02-03 14:58:41 +00:00
|
|
|
const response = await apiClient.updateAuthor(profile)
|
2023-02-09 22:39:52 +00:00
|
|
|
if (response.error) {
|
2023-12-08 07:37:10 +00:00
|
|
|
console.error(response.error)
|
|
|
|
throw response.error
|
2022-12-07 08:37:40 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-01 17:16:14 +00:00
|
|
|
|
2024-02-05 15:04:23 +00:00
|
|
|
createEffect(() => {
|
2023-12-25 06:52:04 +00:00
|
|
|
if (author()) {
|
|
|
|
const currentAuthor = author()
|
2023-08-14 13:42:12 +00:00
|
|
|
setForm({
|
2023-11-23 18:15:06 +00:00
|
|
|
name: currentAuthor.name,
|
|
|
|
slug: currentAuthor.slug,
|
|
|
|
bio: currentAuthor.bio,
|
|
|
|
about: currentAuthor.about,
|
2023-12-08 07:37:10 +00:00
|
|
|
pic: userpicUrl(currentAuthor.pic),
|
2023-11-23 18:15:06 +00:00
|
|
|
links: currentAuthor.links,
|
2023-08-14 13:42:12 +00:00
|
|
|
})
|
2022-12-01 17:16:14 +00:00
|
|
|
}
|
|
|
|
})
|
2023-12-25 06:52:04 +00:00
|
|
|
|
2022-12-01 17:16:14 +00:00
|
|
|
const updateFormField = (fieldName: string, value: string, remove?: boolean) => {
|
|
|
|
if (fieldName === 'links') {
|
2024-03-18 11:55:07 +00:00
|
|
|
setForm((prev) => {
|
2024-03-18 11:57:55 +00:00
|
|
|
const updatedLinks = remove ? prev.links.filter((item) => item !== value) : [...prev.links, value]
|
|
|
|
return { ...prev, links: updatedLinks }
|
|
|
|
})
|
2022-12-01 17:16:14 +00:00
|
|
|
} else {
|
2024-03-18 11:57:55 +00:00
|
|
|
setForm((prev) => ({ ...prev, [fieldName]: value }))
|
2022-12-01 17:16:14 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-09 14:29:48 +00:00
|
|
|
|
2023-11-23 18:15:06 +00:00
|
|
|
const value: ProfileFormContextType = {
|
|
|
|
form,
|
2024-02-04 17:40:15 +00:00
|
|
|
submit,
|
|
|
|
updateFormField,
|
|
|
|
setForm,
|
2023-11-23 18:15:06 +00:00
|
|
|
}
|
2022-12-01 17:16:14 +00:00
|
|
|
|
2023-11-23 18:15:06 +00:00
|
|
|
return <ProfileFormContext.Provider value={value}>{props.children}</ProfileFormContext.Provider>
|
|
|
|
}
|