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

97 lines
3.1 KiB
JavaScript
Raw Normal View History

// Hooks
2023-01-27 20:39:09 +01:00
import { useState, useCallback } from 'react'
2023-01-26 19:31:23 +01:00
import { useTranslation } from 'next-i18next'
import { useBackend } from 'site/hooks/useBackend.mjs'
2023-01-27 20:39:09 +01:00
import { useDropzone } from 'react-dropzone'
import { useToast } from 'site/hooks/useToast.mjs'
// Components
import { Icons, welcomeSteps, BackToAccountButton } from './shared.mjs'
import { ContinueButton } from 'site/components/buttons/continue-button.mjs'
import { SaveSettingsButton } from 'site/components/buttons/save-settings-button.mjs'
2023-01-26 19:31:23 +01:00
export const ns = ['account', 'toast']
2023-01-26 19:31:23 +01:00
export const ImgSettings = ({ app, title = false, welcome = false }) => {
2023-01-26 19:31:23 +01:00
const backend = useBackend(app)
const toast = useToast()
const { t } = useTranslation(ns)
2023-01-27 20:39:09 +01:00
const [img, setImg] = useState(false)
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 () => {
app.startLoading()
const result = await backend.updateAccount({ img })
if (result === true) toast.for.settingsSaved()
else toast.for.backendError()
app.stopLoading()
2023-01-26 19:31:23 +01:00
}
const nextHref = '/docs/guide'
return (
<>
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 ? (
<img alt="img" src={img || app.account.img} className="shadow mb-4" />
) : 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 w-64`}>
{t('imgSelectImage')}
</button>
2023-01-27 20:39:09 +01:00
</div>
</div>
2023-01-26 19:31:23 +01:00
{welcome ? (
<>
<button className={`btn btn-secondary mt-4 w-64`} onClick={save} disabled={!img}>
{t('save')}
</button>
<ContinueButton app={app} btnProps={{ href: nextHref }} link />
2023-01-26 19:31:23 +01:00
{welcomeSteps[app.account.control].length > 0 ? (
<>
<progress
className="progress progress-primary w-full mt-12"
value={700 / welcomeSteps[app.account.control].length}
max="100"
></progress>
<span className="pt-4 text-sm font-bold opacity-50">
7 / {welcomeSteps[app.account.control].length}
</span>
<Icons
done={welcomeSteps[app.account.control].slice(0, 6)}
todo={welcomeSteps[app.account.control].slice(7)}
current="img"
/>
</>
) : null}
</>
) : (
<>
<SaveSettingsButton app={app} btnProps={{ onClick: save }} />
<BackToAccountButton loading={app.loading} />
</>
)}
2023-01-26 19:31:23 +01:00
</>
)
}