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

87 lines
3 KiB
JavaScript
Raw Normal View History

2023-03-24 16:33:14 +01:00
// Dependencies
import { useState, useContext } from 'react'
2023-01-23 20:23:53 +01:00
import { useTranslation } from 'next-i18next'
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
2023-02-18 16:11:02 +01:00
import { BackToAccountButton, Choice, Icons, welcomeSteps } from './shared.mjs'
import { ContinueButton } from 'shared/components/buttons/continue-button.mjs'
2023-01-23 20:23:53 +01:00
2023-08-19 18:01:22 +02:00
export const ns = ['account', 'status']
2023-01-23 20:23:53 +01:00
export const NewsletterSettings = ({ title = false, welcome = false }) => {
// Hooks
2023-03-24 16:33:14 +01:00
const { account, setAccount, token } = useAccount()
const backend = useBackend(token)
const { t } = useTranslation(ns)
2023-08-19 18:01:22 +02:00
const { LoadingStatus, setLoadingStatus } = useLoadingStatus()
// State
2023-03-24 16:33:14 +01:00
const [selection, setSelection] = useState(account?.newsletter ? 'yes' : 'no')
2023-01-23 20:23:53 +01:00
// Helper method to update account
2023-01-23 20:23:53 +01:00
const update = async (val) => {
if (val !== selection) {
2023-08-19 18:01:22 +02:00
setLoadingStatus([true, 'processingUpdate'])
2023-01-23 20:23:53 +01:00
const result = await backend.updateAccount({ newsletter: val === 'yes' ? true : false })
2023-03-24 16:33:14 +01:00
if (result.success) {
setAccount(result.data.account)
setSelection(val)
2023-08-19 18:01:22 +02:00
setLoadingStatus([true, 'settingsSaved', true, true])
} else setLoadingStatus([true, 'backendError', true, true])
2023-01-23 20:23:53 +01:00
}
}
// Next step for onboarding
2023-01-26 19:31:23 +01:00
const nextHref =
2023-03-24 16:33:14 +01:00
welcomeSteps[account?.control].length > 2
? '/welcome/' + welcomeSteps[account?.control][2]
2023-01-26 19:31:23 +01:00
: '/docs/guide'
2023-01-23 20:23:53 +01:00
return (
2023-03-27 19:21:27 +02:00
<div className="max-w-xl">
2023-08-19 18:01:22 +02:00
<LoadingStatus />
{title ? <h1 className="text-4xl">{t('newsletterTitle')}</h1> : null}
2023-01-23 20:23:53 +01:00
{['yes', 'no'].map((val) => (
2023-01-27 21:09:44 +01:00
<Choice val={val} t={t} update={update} current={selection} bool key={val}>
2023-01-23 20:23:53 +01:00
<span className="block text-lg leading-5">
{selection === 1 && val === 2
? t('showMore')
: t(val === 'yes' ? 'newsletterYes' : 'noThanks')}
</span>
2023-02-18 16:11:02 +01:00
<span className="block text-normal font-light normal-case pt-1 leading-5">
{t(val === 'yes' ? 'newsletterYesd' : 'newsletterNod')}
2023-01-23 20:23:53 +01:00
</span>
</Choice>
))}
{welcome ? (
<>
<ContinueButton btnProps={{ href: nextHref }} link />
2023-03-24 16:33:14 +01:00
{welcomeSteps[account?.control].length > 0 ? (
2023-01-23 20:23:53 +01:00
<>
<progress
className="progress progress-primary w-full mt-12"
2023-03-24 16:33:14 +01:00
value={200 / welcomeSteps[account?.control].length}
2023-01-23 20:23:53 +01:00
max="100"
></progress>
<span className="pt-4 text-sm font-bold opacity-50">
2023-03-24 16:33:14 +01:00
2 / {welcomeSteps[account?.control].length}
2023-01-23 20:23:53 +01:00
</span>
2023-01-26 19:31:23 +01:00
<Icons
2023-03-24 16:33:14 +01:00
done={welcomeSteps[account?.control].slice(0, 1)}
todo={welcomeSteps[account?.control].slice(2)}
2023-01-26 19:31:23 +01:00
current="newsletter"
/>
2023-01-23 20:23:53 +01:00
</>
) : null}
</>
2023-02-18 16:11:02 +01:00
) : (
2023-08-19 18:01:22 +02:00
<BackToAccountButton />
2023-02-18 16:11:02 +01:00
)}
2023-03-27 19:21:27 +02:00
</div>
2023-01-23 20:23:53 +01:00
)
}
export default NewsletterSettings