1
0
Fork 0
freesewing/sites/shared/components/account/img.mjs

118 lines
4 KiB
JavaScript
Raw Normal View History

2023-03-24 16:33:14 +01:00
// Dependencies
2023-08-20 18:35:19 +02:00
import { useState, useCallback } from 'react'
2023-01-26 19:31:23 +01:00
import { useTranslation } from 'next-i18next'
2023-01-27 20:39:09 +01:00
import { useDropzone } from 'react-dropzone'
2023-08-19 18:01:22 +02:00
import { cloudflareImageUrl } from 'shared/utils.mjs'
2023-03-24 16:33:14 +01:00
// Hooks
import { useAccount } from 'shared/hooks/use-account.mjs'
import { useBackend } from 'shared/hooks/use-backend.mjs'
2023-08-19 18:01:22 +02:00
import { useLoadingStatus } from 'shared/hooks/use-loading-status.mjs'
// Components
import { Icons, welcomeSteps, BackToAccountButton } from './shared.mjs'
2023-05-17 17:04:15 +02:00
import { ContinueButton } from 'shared/components/buttons/continue-button.mjs'
import { SaveSettingsButton } from 'shared/components/buttons/save-settings-button.mjs'
2023-01-26 19:31:23 +01:00
2023-08-19 18:01:22 +02:00
export const ns = ['account', 'status']
2023-01-26 19:31:23 +01:00
export const ImgSettings = ({ title = false, welcome = false }) => {
2023-08-20 18:48:40 +02:00
const { account, setAccount } = useAccount()
const backend = useBackend()
2023-08-19 18:01:22 +02:00
const { setLoadingStatus, LoadingStatus } = useLoadingStatus()
const { t } = useTranslation(ns)
2023-01-27 20:39:09 +01:00
const [img, setImg] = useState(false)
2023-08-19 18:01:22 +02:00
const [url, setUrl] = useState('')
2023-01-27 20:39:09 +01:00
const onDrop = useCallback((acceptedFiles) => {
const reader = new FileReader()
reader.onload = () => {
setImg(reader.result)
}
acceptedFiles.forEach((file) => reader.readAsDataURL(file))
}, [])
const { getRootProps, getInputProps } = useDropzone({ onDrop })
2023-01-26 19:31:23 +01:00
const save = async () => {
2023-08-19 18:01:22 +02:00
setLoadingStatus([true, 'processingUpdate'])
const result = await backend.updateAccount({ img: url ? url : img })
2023-03-24 16:33:14 +01:00
if (result.success) {
setAccount(result.data.account)
2023-08-19 18:01:22 +02:00
setLoadingStatus([true, 'settingsSaved', true, true])
} else setLoadingStatus([true, 'backendError', true, false])
2023-01-26 19:31:23 +01:00
}
const nextHref = '/docs/guide'
return (
2023-03-27 19:21:27 +02:00
<div className="max-w-xl">
2023-08-19 18:01:22 +02:00
<LoadingStatus />
2023-02-18 16:11:02 +01:00
{title ? <h2 className="text-4xl">{t('imgTitle')}</h2> : null}
2023-01-27 20:39:09 +01:00
<div>
{!welcome || img !== false ? (
2023-08-19 18:01:22 +02:00
<img
alt="img"
src={img || cloudflareImageUrl({ id: `user-${account.ihash}`, variant: 'public' })}
className="shadow mb-4"
/>
2023-01-27 20:39:09 +01:00
) : null}
<div
{...getRootProps()}
className={`
flex rounded-lg w-full flex-col items-center justify-center
lg:h-64 lg:border-4 lg:border-secondary lg:border-dashed
`}
>
<input {...getInputProps()} />
<p className="hidden lg:block p-0 m-0">{t('imgDragAndDropImageHere')}</p>
2023-01-27 20:39:09 +01:00
<p className="hidden lg:block p-0 my-2">{t('or')}</p>
<button className={`btn btn-secondary btn-outline mt-4 px-8`}>
{t('imgSelectImage')}
</button>
2023-01-27 20:39:09 +01:00
</div>
2023-08-19 18:01:22 +02:00
<p className="hidden lg:block p-0 my-2 text-center">{t('or')}</p>
<div className="flex flex-row items-center">
<input
type="url"
className="input input-secondary w-full input-bordered"
placeholder="Paste an image URL here"
value={url}
onChange={(evt) => setUrl(evt.target.value)}
/>
</div>
2023-01-27 20:39:09 +01:00
</div>
2023-01-26 19:31:23 +01:00
{welcome ? (
<>
<button className={`btn btn-secondary mt-4 px-8`} onClick={save} disabled={!img}>
{t('save')}
</button>
<ContinueButton btnProps={{ href: nextHref }} link />
2023-03-24 16:33:14 +01:00
{welcomeSteps[account.control].length > 0 ? (
2023-01-26 19:31:23 +01:00
<>
<progress
className="progress progress-primary w-full mt-12"
2023-03-24 16:33:14 +01:00
value={700 / welcomeSteps[account.control].length}
2023-01-26 19:31:23 +01:00
max="100"
></progress>
<span className="pt-4 text-sm font-bold opacity-50">
2023-03-24 16:33:14 +01:00
7 / {welcomeSteps[account.control].length}
2023-01-26 19:31:23 +01:00
</span>
<Icons
2023-03-24 16:33:14 +01:00
done={welcomeSteps[account.control].slice(0, 6)}
todo={welcomeSteps[account.control].slice(7)}
2023-01-26 19:31:23 +01:00
current="img"
/>
</>
) : null}
</>
) : (
<>
<SaveSettingsButton btnProps={{ onClick: save }} />
2023-08-19 18:01:22 +02:00
<BackToAccountButton />
</>
)}
2023-03-27 19:21:27 +02:00
</div>
2023-01-26 19:31:23 +01:00
)
}