import { PageWrap } from '../../_shared/PageWrap' import { t } from '../../../utils/intl' import { Icon } from '../../_shared/Icon' import ProfileSettingsNavigation from '../../Discours/ProfileSettingsNavigation' import { For, createSignal, Show, onMount, createEffect } from 'solid-js' import { clsx } from 'clsx' import styles from './Settings.module.scss' import { useProfileForm } from '../../../context/profile' import validateUrl from '../../../utils/validateUrl' import { createFileUploader, UploadFile } from '@solid-primitives/upload' import { Loading } from '../../Loading' import { useSession } from '../../../context/session' import Button from '../../_shared/Button' import { useSnackbar } from '../../../context/snackbar' export const ProfileSettingsPage = () => { const [addLinkForm, setAddLinkForm] = createSignal(false) const [incorrectUrl, setIncorrectUrl] = createSignal(false) const [isSubmitting, setIsSubmitting] = createSignal(false) const [isUserpicUpdating, setIsUserpicUpdating] = createSignal(false) const { actions: { showSnackbar } } = useSnackbar() const { actions: { loadSession } } = useSession() const { form, updateFormField, submit, slugError } = useProfileForm() const handleChangeSocial = (value: string) => { if (validateUrl(value)) { updateFormField('links', value) setAddLinkForm(false) } else { setIncorrectUrl(true) } } const handleSubmit = async (event: Event) => { event.preventDefault() setIsSubmitting(true) try { await submit(form) showSnackbar({ body: t('Profile successfully saved') }) } catch { showSnackbar({ type: 'error', body: t('Error') }) } loadSession() setIsSubmitting(false) } const { selectFiles } = createFileUploader({ multiple: false, accept: 'image/*' }) const handleFileUpload = async (uploadFile: UploadFile) => { const formData = new FormData() formData.append('file', uploadFile.file, uploadFile.name) const response = await fetch('/api/upload', { method: 'POST', body: formData }) return response.json() } const handleAvatarClick = async () => { await selectFiles(async ([uploadFile]) => { try { setIsUserpicUpdating(true) const fileUrl = await handleFileUpload(uploadFile) updateFormField('userpic', fileUrl) setIsUserpicUpdating(false) } catch (error) { console.error('[upload avatar] error', error) } }) } const [hostname, setHostname] = createSignal('new.discours.io') onMount(() => setHostname(window?.location.host)) return (

{t('Profile settings')}

{t('Here you can customize your profile the way you want.')}

{t('Userpic')}

}> {form.name}

{t('Name')}

{t( 'Your name will appear on your profile page and as your signature in publications, comments and responses.' )}

updateFormField('name', event.currentTarget.value)} value={form.name} />

{t('Address on Discourse')}

updateFormField('slug', event.currentTarget.value)} value={form.slug} class="nolabel" />

{t(`${slugError()}`)}

{t('Introduce')}

{t('About myself')}