// Dependencies
import { useState, useEffect, useContext, useCallback } from 'react'
import { useTranslation } from 'next-i18next'
import orderBy from 'lodash.orderby'
import { capitalize } from 'shared/utils.mjs'
import { freeSewingConfig as conf } from 'shared/config/freesewing.config.mjs'
// Hooks
import { useDropzone } from 'react-dropzone'
import { useAccount } from 'shared/hooks/use-account.mjs'
import { useBackend } from 'shared/hooks/use-backend.mjs'
import { useToast } from 'shared/hooks/use-toast.mjs'
import { useRouter } from 'next/router'
// Context
import { LoadingContext } from 'shared/context/loading-context.mjs'
import { ModalContext } from 'shared/context/modal-context.mjs'
// Components
import { PageLink, Link } from 'shared/components/link.mjs'
import { Collapse, MimicCollapseLink } from 'shared/components/collapse.mjs'
import { BackToAccountButton, Choice } from './shared.mjs'
import {
OkIcon,
NoIcon,
TrashIcon,
SettingsIcon,
DownloadIcon,
PageIcon,
} from 'shared/components/icons.mjs'
import { ModalWrapper } from 'shared/components/wrappers/modal.mjs'
import Markdown from 'react-markdown'
import { Tab } from './bio.mjs'
import Timeago from 'react-timeago'
import { Spinner } from 'shared/components/spinner.mjs'
export const ns = ['account', 'patterns', 'toast']
export const StandAloneNewSet = () => {
const { t } = useTranslation(['account'])
const toast = useToast()
const { account } = useAccount()
const backend = useBackend()
return (
)
}
export const NewSet = ({
t,
refresh,
closeCollapseButton,
backend,
toast,
title = true,
standalone = false,
}) => {
// Context
const { startLoading, stopLoading } = useContext(LoadingContext)
// Hooks
const router = useRouter()
// State
const [name, setName] = useState('')
// Helper method to create a new set
const createSet = async () => {
startLoading()
const result = await backend.createSet({
name,
})
if (result.success) {
toast.success({t('nailedIt')} )
if (standalone) router.push('/account/sets/')
else {
refresh()
closeCollapseButton()
}
} else toast.for.backendError()
stopLoading()
}
return (
{title ?
{t('newSet')} : null}
{t('name')}
{t('setNameDesc')}
setName(evt.target.value)}
className="input w-full input-bordered flex flex-row"
type="text"
placeholder={'Georg Cantor'}
/>
{t('newSet')}
)
}
const EditField = (props) => {
if (props.field === 'name') return
if (props.field === 'notes') return
if (props.field === 'public') return
if (props.field === 'img') return
return FIXME: No edit component for this field
}
export const EditRow = (props) => (
{props.title}
{props.children}
>
}
>
)
const EditImg = ({ t, pattern, account, backend, toast, refresh }) => {
const [img, setImg] = useState(pattern.img)
const { startLoading, stopLoading } = useContext(LoadingContext)
const onDrop = useCallback((acceptedFiles) => {
const reader = new FileReader()
reader.onload = () => {
setImg(reader.result)
}
acceptedFiles.forEach((file) => reader.readAsDataURL(file))
}, [])
const { getRootProps, getInputProps } = useDropzone({ onDrop })
const save = async () => {
startLoading()
const result = await backend.updatePattern(pattern.id, { img })
if (result.success) {
toast.for.settingsSaved()
refresh()
} else toast.for.backendError()
stopLoading()
}
return (
)
}
const EditName = ({ t, pattern, backend, toast, refresh }) => {
const [value, setValue] = useState(pattern.name)
const { loading, startLoading, stopLoading } = useContext(LoadingContext)
const update = async (evt) => {
evt.preventDefault()
if (evt.target.value !== value) {
setValue(evt.target.value)
}
}
const save = async () => {
startLoading()
const result = await backend.updatePattern(pattern.id, { name: value })
if (result.success) {
refresh()
toast.for.settingsSaved()
} else toast.for.backendError()
stopLoading()
}
return (
{loading ? (
<>
{t('processing')}
>
) : (
t('save')
)}
)
}
const EditNotes = ({ t, pattern, backend, toast, refresh }) => {
const [value, setValue] = useState(pattern.notes)
const [activeTab, setActiveTab] = useState('edit')
const { loading, startLoading, stopLoading } = useContext(LoadingContext)
const update = async (evt) => {
evt.preventDefault()
if (evt.target.value !== value) {
setValue(evt.target.value)
}
}
const save = async () => {
startLoading()
const result = await backend.updatePattern(pattern.id, { notes: value })
if (result.success) {
refresh()
toast.for.settingsSaved()
} else toast.for.backendError()
stopLoading()
}
// Shared props for tabs
const tabProps = { activeTab, setActiveTab, t }
return (
{activeTab === 'edit' ? (
) : (
{value}
)}
{loading ? (
<>
{t('processing')}
>
) : (
t('save')
)}
)
}
const EditPublic = ({ t, pattern, backend, toast, refresh }) => {
const [selection, setSelection] = useState(pattern.public)
const { startLoading, stopLoading } = useContext(LoadingContext)
const update = async (val) => {
setSelection(val)
if (val !== pattern.public) {
startLoading()
const result = await backend.updatePattern(pattern.id, { public: val })
if (result.success) {
refresh()
toast.for.settingsSaved()
} else toast.for.backendError()
stopLoading()
}
}
return (
<>
{[true, false].map((val) => (
{val ? (
<>
{t('publicPattern')}
>
) : (
<>
{t('privatePattern')}
>
)}
{val ? t('publicPatternDesc') : t('privatePatternDesc')}
))}
>
)
}
export const EditSectionTitle = ({ title }) => (
{title}
)
const EditPattern = (props) => {
const { account, pattern, t } = props
return (
{/* Meta info */}
{account.control > 2 ? (
{t('permalink')}:
{pattern.public ? (
) : (
)}
{t('created')} :
{t('updated')} :
) : null}
{/* JSON & YAML links */}
{account.control > 3 ? (
) : null}
{/* Name is always shown */}
{pattern.name}
{/* img: Control level determines whether or not to show this */}
{account.control >= conf.account.patterns.img ? (
) : null}
{/* public: Control level determines whether or not to show this */}
{account.control >= conf.account.patterns.public ? (
{pattern.public ? (
<>
{t('publicPattern')}
>
) : (
<>
{t('privatePattern')}
>
)}
) : null}
{/* notes: Control level determines whether or not to show this */}
{account.control >= conf.account.patterns.notes ? (
{pattern.notes}
) : null}
)
}
const Pattern = ({ pattern, t, account, backend, refresh }) => {
// Context
const { startLoading, stopLoading } = useContext(LoadingContext)
const { setModal } = useContext(ModalContext)
// Hooks
const toast = useToast()
const remove = async () => {
startLoading()
const result = await backend.removePattern(pattern.id)
if (result) toast.success(t('gone'))
else toast.for.backendError()
// This just forces a refresh of the list from the server
refresh()
stopLoading()
}
const removeModal = () => {
setModal(
{t('areYouCertain')}
{t('deleteSetWarning')}
{t('cancel')}
{t('delete')}
)
}
const title = (
<>
{pattern.set?.img && (
)}
{pattern.cset?.img && (
)}
{capitalize(pattern.design)}
{pattern.name}
{pattern.set?.name &&
{pattern.set.name} }
{pattern.cset?.nameEn &&
{pattern.cset.nameEn} }
>
)
return (
<>
,
,
,
4 ? remove : removeModal}
title={t('removePattern')}
>
,
]}
>
{t('showPattern')}
{t('draftPattern')}
{t('exportPattern')}
>
)
}
// Component for the account/patterns page
export const Patterns = ({ standAlone = false }) => {
// Hooks
const { account } = useAccount()
const backend = useBackend()
const { t } = useTranslation(ns)
// State
const [patterns, setPatterns] = useState([])
const [changes, setChanges] = useState(0)
// Effects
useEffect(() => {
const getPatterns = async () => {
const result = await backend.getPatterns()
if (result.success) setPatterns(result.data.patterns)
}
getPatterns()
}, [changes])
// Helper method to force a refresh
const refresh = () => {
setChanges(changes + 1)
}
return (
{orderBy(patterns, ['name'], ['asc']).map((pattern) => (
))}
{t('createANewPattern')}
{standAlone ? null :
}
)
}