webapp/src/components/Pages/profile/ProfileSettingsPage.tsx

221 lines
8.6 KiB
TypeScript
Raw Normal View History

2022-11-24 21:37:43 +00:00
import { PageWrap } from '../../_shared/PageWrap'
2022-12-01 08:37:04 +00:00
import { t } from '../../../utils/intl'
2022-11-24 21:37:43 +00:00
import type { PageProps } from '../../types'
import { Icon } from '../../_shared/Icon'
import ProfileSettingsNavigation from '../../Discours/ProfileSettingsNavigation'
2022-12-04 09:46:13 +00:00
import { For, createSignal, Show, onMount } from 'solid-js'
2022-12-01 08:37:04 +00:00
import { clsx } from 'clsx'
import styles from './Settings.module.scss'
2022-12-01 17:16:14 +00:00
import { useProfileForm } from '../../../context/profile'
2022-12-07 08:37:40 +00:00
import validateUrl from '../../../utils/validateUrl'
2022-11-24 21:37:43 +00:00
export const ProfileSettingsPage = (props: PageProps) => {
2022-12-01 17:16:14 +00:00
const [addLinkForm, setAddLinkForm] = createSignal<boolean>(false)
2022-12-07 08:37:40 +00:00
const [incorrectUrl, setIncorrectUrl] = createSignal<boolean>(false)
2022-12-07 08:53:41 +00:00
const { form, updateFormField, submit, slugError } = useProfileForm()
2023-01-26 05:30:39 +00:00
let updateForm: HTMLFormElement
const handleChangeSocial = (value: string) => {
2022-12-07 08:37:40 +00:00
if (validateUrl(value)) {
updateFormField('links', value)
setAddLinkForm(false)
} else {
setIncorrectUrl(true)
}
2022-12-01 17:16:14 +00:00
}
const handleSubmit = (event: Event): void => {
event.preventDefault()
submit(form)
}
2023-01-26 05:30:39 +00:00
let userpicFile: HTMLInputElement
2023-01-28 00:31:46 +00:00
const handleFileUpload = async (file: File) => {
2023-01-28 11:00:06 +00:00
const formData = new FormData()
formData.append('file', file)
console.log(formData)
const response = await fetch('/api/upload', {
method: 'POST',
body: formData,
headers: {
'Content-Type': 'multipart/form-data'
}
})
const json = await response.json()
console.debug(json)
2023-01-28 00:31:46 +00:00
}
2023-01-26 05:30:39 +00:00
const handleUserpicUpload = async (ev) => {
// TODO: show progress
2023-01-28 11:00:06 +00:00
console.debug('handleUserpicUpload')
2023-01-26 05:30:39 +00:00
try {
const f = ev.target.files[0]
2023-01-28 11:00:06 +00:00
if (f) await handleFileUpload(f)
2023-01-26 05:30:39 +00:00
} catch (error) {
console.error('[upload] error', error)
}
2022-12-02 06:12:50 +00:00
}
2023-01-28 00:31:46 +00:00
2022-12-04 09:46:13 +00:00
const [hostname, setHostname] = createSignal('new.discours.io')
onMount(() => setHostname(window?.location.host))
2022-12-02 06:12:50 +00:00
2022-11-24 21:37:43 +00:00
return (
<PageWrap>
2022-12-01 17:16:14 +00:00
<Show when={form}>
2022-12-01 08:37:04 +00:00
<div class="wide-container">
<div class="shift-content">
<div class="left-col">
<div class={clsx('left-navigation', styles.leftNavigation)}>
<ProfileSettingsNavigation />
</div>
</div>
2022-12-01 08:37:04 +00:00
<div class="row">
<div class="col-md-10 col-lg-9 col-xl-8">
<h1>{t('Profile settings')}</h1>
<p class="description">{t('Here you can customize your profile the way you want.')}</p>
2023-01-26 05:30:39 +00:00
<form ref={updateForm} onSubmit={handleSubmit} enctype="multipart/form-data">
2022-12-01 08:37:04 +00:00
<h4>{t('Userpic')}</h4>
<div class="pretty-form__item">
<div class={styles.avatarContainer}>
2023-01-26 05:30:39 +00:00
<img
class={styles.avatar}
src={form.userpic}
alt={form.name}
onClick={() => userpicFile.click()}
/>
<input
ref={userpicFile}
type="file"
2023-01-27 23:07:25 +00:00
name="file"
2023-01-28 11:00:06 +00:00
value="file"
2023-01-26 05:30:39 +00:00
hidden
onChange={handleUserpicUpload}
/>
2022-12-01 08:37:04 +00:00
</div>
</div>
<h4>{t('Name')}</h4>
<p class="description">
{t(
'Your name will appear on your profile page and as your signature in publications, comments and responses.'
)}
</p>
<div class="pretty-form__item">
2022-11-24 21:37:43 +00:00
<input
2022-12-01 08:37:04 +00:00
type="text"
name="username"
id="username"
2022-12-01 17:16:14 +00:00
placeholder={t('Name')}
onChange={(event) => updateFormField('name', event.currentTarget.value)}
value={form.name}
2022-11-24 21:37:43 +00:00
/>
2022-12-02 06:40:26 +00:00
<label for="username">{t('Name')}</label>
2022-11-24 21:37:43 +00:00
</div>
2022-12-02 06:40:26 +00:00
<h4>{t('Address on Discourse')}</h4>
<div class="pretty-form__item">
<div class={styles.discoursName}>
2022-12-04 09:46:13 +00:00
<label for="user-address">https://{hostname()}/author/</label>
2022-12-02 06:40:26 +00:00
<div class={styles.discoursNameField}>
<input
type="text"
name="user-address"
id="user-address"
onChange={(event) => updateFormField('slug', event.currentTarget.value)}
value={form.slug}
class="nolabel"
/>
2022-12-07 08:53:41 +00:00
<p class="form-message form-message--error">{t(`${slugError()}`)}</p>
2022-12-02 06:40:26 +00:00
</div>
</div>
</div>
<h4>{t('Introduce')}</h4>
<div class="pretty-form__item">
<textarea name="presentation" id="presentation" placeholder={t('Introduce')}>
{form.bio}
</textarea>
<label for="presentation">{t('Introduce')}</label>
</div>
2022-11-24 21:37:43 +00:00
2022-12-09 06:09:21 +00:00
<h4>{t('About myself')}</h4>
<div class="pretty-form__item">
<textarea
name="about"
id="about"
placeholder={t('About myself')}
value={form.about}
onChange={(event) => updateFormField('about', event.currentTarget.value)}
/>
<label for="about">{t('About myself')}</label>
</div>
2022-12-06 03:41:49 +00:00
2022-12-09 06:09:21 +00:00
{/*Нет реализации полей на бэке*/}
2022-12-01 17:16:14 +00:00
{/*<h4>{t('How can I help/skills')}</h4>*/}
{/*<div class="pretty-form__item">*/}
{/* <input type="text" name="skills" id="skills" />*/}
{/*</div>*/}
{/*<h4>{t('Where')}</h4>*/}
{/*<div class="pretty-form__item">*/}
{/* <input type="text" name="location" id="location" placeholder="Откуда" />*/}
{/* <label for="location">{t('Where')}</label>*/}
{/*</div>*/}
2022-12-01 08:37:04 +00:00
2022-12-01 17:16:14 +00:00
{/*<h4>{t('Date of Birth')}</h4>*/}
{/*<div class="pretty-form__item">*/}
{/* <input*/}
{/* type="date"*/}
{/* name="birthdate"*/}
{/* id="birthdate"*/}
{/* placeholder="Дата рождения"*/}
{/* class="nolabel"*/}
{/* />*/}
{/*</div>*/}
2022-12-01 08:37:04 +00:00
<div class={clsx(styles.multipleControls, 'pretty-form__item')}>
<div class={styles.multipleControlsHeader}>
<h4>{t('Social networks')}</h4>
2022-12-01 17:16:14 +00:00
<button type="button" class="button" onClick={() => setAddLinkForm(!addLinkForm())}>
+
</button>
2022-12-01 08:37:04 +00:00
</div>
2022-12-01 17:16:14 +00:00
<Show when={addLinkForm()}>
<div class={styles.multipleControlsItem}>
<input
autofocus={true}
type="text"
name="link"
class="nolabel"
onChange={(event) => handleChangeSocial(event.currentTarget.value)}
/>
</div>
2022-12-07 08:37:40 +00:00
<Show when={incorrectUrl()}>
<p class="form-message form-message--error">{t('It does not look like url')}</p>
</Show>
2022-12-01 17:16:14 +00:00
</Show>
<For each={form.links}>
2022-12-01 08:37:04 +00:00
{(link) => (
<div class={styles.multipleControlsItem}>
2022-12-01 17:16:14 +00:00
<input type="text" value={link} readonly={true} name="link" class="nolabel" />
<button type="button" onClick={() => updateFormField('links', link, true)}>
2022-12-01 08:37:04 +00:00
<Icon name="remove" class={styles.icon} />
</button>
</div>
)}
</For>
2022-11-24 21:37:43 +00:00
</div>
2022-12-01 08:37:04 +00:00
<br />
<p>
2022-12-01 17:16:14 +00:00
<button type="submit" class="button button--submit">
{t('Save settings')}
</button>
2022-12-01 08:37:04 +00:00
</p>
</form>
</div>
2022-11-24 21:37:43 +00:00
</div>
</div>
</div>
2022-12-01 08:37:04 +00:00
</Show>
2022-11-24 21:37:43 +00:00
</PageWrap>
)
}
// for lazy loading
export default ProfileSettingsPage