webapp/src/context/profile.tsx

103 lines
2.9 KiB
TypeScript
Raw Normal View History

2024-06-24 17:50:27 +00:00
import type { Author, ProfileInput } from '../graphql/schema/core.gen'
2024-06-24 17:50:27 +00:00
import { AuthToken } from '@authorizerdev/authorizer-js'
import { Accessor, JSX, createContext, createEffect, createSignal, on, useContext } from 'solid-js'
2022-12-01 17:16:14 +00:00
import { createStore } from 'solid-js/store'
2024-06-24 17:50:27 +00:00
import updateAuthorMuatation from '~/graphql/mutation/core/author-update'
import { useAuthors } from './authors'
import { useGraphQL } from './graphql'
import { useSession } from './session'
2022-12-01 17:16:14 +00:00
2024-06-24 17:50:27 +00:00
type ProfileContextType = {
author: Accessor<Author>
setAuthor: (a: Author) => void
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
}
2024-06-24 17:50:27 +00:00
const ProfileContext = createContext<ProfileContextType>({} as ProfileContextType)
2024-06-24 17:50:27 +00:00
export function useProfile() {
return useContext(ProfileContext)
}
const userpicUrl = (userpic: string) => {
2024-02-04 09:03:15 +00:00
if (userpic?.includes('assets.discours.io')) {
return userpic.replace('100x', '500x500')
}
return userpic
}
2024-06-24 17:50:27 +00:00
export const ProfileProvider = (props: { children: JSX.Element }) => {
const { session } = useSession()
const { mutation } = useGraphQL()
const { addAuthor } = useAuthors()
const [form, setForm] = createStore<ProfileInput>({} as ProfileInput)
const [author, setAuthor] = createSignal<Author>({} as Author)
// when session is loaded
createEffect(
on(
() => session(),
(s: AuthToken | undefined) => {
if (s) {
const profile = s?.user?.app_data?.profile
if (profile?.id) {
setAuthor(profile)
addAuthor(profile)
}
}
},
2024-06-26 08:22:05 +00:00
{ defer: true }
)
2024-06-24 17:50:27 +00:00
)
2022-12-07 08:37:40 +00:00
const submit = async (profile: ProfileInput) => {
2024-06-24 17:50:27 +00:00
const response = await mutation(updateAuthorMuatation, profile).toPromise()
2023-02-09 22:39:52 +00:00
if (response.error) {
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()
setForm({
name: currentAuthor.name,
slug: currentAuthor.slug,
bio: currentAuthor.bio,
about: currentAuthor.about,
2024-06-24 17:50:27 +00:00
pic: userpicUrl(currentAuthor.pic || ''),
2024-06-26 08:22:05 +00:00
links: currentAuthor.links
})
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-06-24 17:50:27 +00:00
const updatedLinks = remove
? (prev.links || []).filter((item) => item !== value)
: [...(prev.links || []), value]
2024-03-18 11:57:55 +00:00
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
}
}
2024-06-24 17:50:27 +00:00
const value: ProfileContextType = {
author,
setAuthor,
form,
2024-02-04 17:40:15 +00:00
submit,
updateFormField,
2024-06-26 08:22:05 +00:00
setForm
}
2022-12-01 17:16:14 +00:00
2024-06-24 17:50:27 +00:00
return <ProfileContext.Provider value={value}>{props.children}</ProfileContext.Provider>
}