// Dependencies import { capitalize, shortDate } from 'shared/utils.mjs' // Hooks import { useState, useContext } from 'react' import { useTranslation } from 'next-i18next' import { useRouter } from 'next/router' import { useAccount } from 'shared/hooks/use-account.mjs' import { useBackend } from 'shared/hooks/use-backend.mjs' import { useToast } from 'shared/hooks/use-toast.mjs' // Context import { LoadingContext } from 'shared/context/loading-context.mjs' // Components import { Spinner } from 'shared/components/spinner.mjs' export const ns = ['wbsave'] const whereAreWe = (router = false, design) => { const info = {} if (router?.asPath) { const chunks = router.asPath.split('/') info.new = chunks[1] === 'new' info.design = chunks[3] info.from = chunks[4] info.fromId = chunks[5].split('#').shift() info.locale = router.locale } if (info.design !== design) throw 'Design passed to view does not match URL state' return info } const defaultName = (info) => `${capitalize(info.design)} / ${info.from}-${info.fromId} / ${shortDate(info.locale)}` const SaveNewPattern = ({ t, design, settings, info, backend, loading, startLoading, stopLoading, toast, router, }) => { // State const [name, setName] = useState(defaultName(info)) const update = async (evt) => { evt.preventDefault() if (evt.target.value !== name) setName(evt.target.value) } const save = async () => { startLoading() const data = { data: {}, design, name, settings, } if (data.settings.measurements) delete data.settings.measurements if (data.settings.embed) delete data.settings.embed if (info.from === 'set' && info.fromId) data.set = Number(info.fromId) else if (info.from === 'cset' && info.fromId) data.cset = Number(info.fromId) else return toast.error(¯\_(ツ)_/¯) const result = await backend.createPattern(data) if (result.success) { toast.for.settingsSaved() router.push(`/patterns/${result.data.pattern.id}`) } else toast.for.backendError() stopLoading() } return (

Save to your FreeSewing account

) } export const SaveView = ({ design, setView, settings, ui, update, language, DynamicDocs }) => { // Hooks const { t } = useTranslation(ns) const { token } = useAccount() const backend = useBackend(token) const router = useRouter() const toast = useToast() // Context const { loading, startLoading, stopLoading } = useContext(LoadingContext) const info = whereAreWe(router, design) return (

{t('wbsave:title')}

{info.new ? ( ) : null}
) }