2023-04-22 16:41:13 +02:00
|
|
|
// Dependencies
|
2023-08-24 18:39:12 +02:00
|
|
|
import { useState, useEffect, useContext } from 'react'
|
2023-04-22 16:41:13 +02:00
|
|
|
import { useTranslation } from 'next-i18next'
|
2023-06-22 11:29:19 -05:00
|
|
|
import { measurements } from 'config/measurements.mjs'
|
2023-05-17 17:32:19 +02:00
|
|
|
import { measurements as designMeasurements } from 'shared/prebuild/data/design-measurements.mjs'
|
2023-08-23 17:41:34 +02:00
|
|
|
import { freeSewingConfig as conf, controlLevels } from 'shared/config/freesewing.config.mjs'
|
2023-04-22 16:41:13 +02:00
|
|
|
// Hooks
|
|
|
|
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'
|
2023-08-23 12:18:20 +02:00
|
|
|
import { useLoadingStatus } from 'shared/hooks/use-loading-status.mjs'
|
2023-04-28 21:23:06 +02:00
|
|
|
// Context
|
|
|
|
import { LoadingContext } from 'shared/context/loading-context.mjs'
|
|
|
|
import { ModalContext } from 'shared/context/modal-context.mjs'
|
2023-04-22 16:41:13 +02:00
|
|
|
// Components
|
2023-08-24 18:39:12 +02:00
|
|
|
import { BackToAccountButton } from './shared.mjs'
|
2023-08-23 12:18:20 +02:00
|
|
|
import { AnchorLink, PageLink, Link } from 'shared/components/link.mjs'
|
2023-04-23 18:00:52 +02:00
|
|
|
import {
|
|
|
|
OkIcon,
|
|
|
|
NoIcon,
|
|
|
|
TrashIcon,
|
|
|
|
EditIcon,
|
2023-08-23 12:18:20 +02:00
|
|
|
UploadIcon,
|
|
|
|
ResetIcon,
|
2023-08-23 17:41:34 +02:00
|
|
|
MeasieIcon,
|
|
|
|
CalendarIcon,
|
2023-08-23 18:49:21 +02:00
|
|
|
PlusIcon,
|
2023-04-23 18:00:52 +02:00
|
|
|
} from 'shared/components/icons.mjs'
|
2023-04-22 16:41:13 +02:00
|
|
|
import { ModalWrapper } from 'shared/components/wrappers/modal.mjs'
|
|
|
|
import Markdown from 'react-markdown'
|
2023-04-23 18:00:52 +02:00
|
|
|
import Timeago from 'react-timeago'
|
2023-08-23 17:47:21 +02:00
|
|
|
import { DisplayRow } from './shared.mjs'
|
2023-08-23 12:18:20 +02:00
|
|
|
import { shortDate, cloudflareImageUrl, formatMm } from 'shared/utils.mjs'
|
|
|
|
import { useSetDocs } from 'shared/hooks/use-set-docs.mjs'
|
|
|
|
import { useMeasurementDocs } from 'shared/hooks/use-measurement-docs.mjs'
|
|
|
|
import { isDegreeMeasurement } from 'config/measurements.mjs'
|
2023-08-23 17:41:34 +02:00
|
|
|
import { TextOnBg } from 'shared/components/text-on-bg.mjs'
|
2023-04-22 16:41:13 +02:00
|
|
|
|
2023-08-23 12:18:20 +02:00
|
|
|
import {
|
|
|
|
StringInput,
|
|
|
|
PassiveImageInput,
|
|
|
|
ListInput,
|
|
|
|
MarkdownInput,
|
|
|
|
MeasieInput,
|
2023-08-23 17:41:34 +02:00
|
|
|
DesignDropdown,
|
|
|
|
ns as inputNs,
|
2023-08-23 12:18:20 +02:00
|
|
|
} from 'shared/components/inputs.mjs'
|
2023-04-30 20:31:28 +02:00
|
|
|
|
2023-08-23 17:41:34 +02:00
|
|
|
export const ns = [inputNs, 'account', 'patterns', 'status', 'measurements']
|
2023-04-28 21:23:06 +02:00
|
|
|
|
2023-08-23 12:18:20 +02:00
|
|
|
export const NewSet = () => {
|
2023-04-28 21:23:06 +02:00
|
|
|
// Hooks
|
2023-08-23 12:18:20 +02:00
|
|
|
const { setLoadingStatus, LoadingStatus } = useLoadingStatus()
|
|
|
|
const backend = useBackend()
|
|
|
|
const { t } = useTranslation(ns)
|
2023-04-22 16:41:13 +02:00
|
|
|
const router = useRouter()
|
2023-04-28 21:23:06 +02:00
|
|
|
|
2023-08-23 12:18:20 +02:00
|
|
|
// State
|
|
|
|
const [generate, setGenerate] = useState(false)
|
|
|
|
const [added, setAdded] = useState(0)
|
|
|
|
|
2023-04-28 21:23:06 +02:00
|
|
|
// State
|
2023-04-22 16:41:13 +02:00
|
|
|
const [name, setName] = useState('')
|
|
|
|
|
2023-04-28 21:23:06 +02:00
|
|
|
// Helper method to create a new set
|
2023-04-22 16:41:13 +02:00
|
|
|
const createSet = async () => {
|
2023-08-23 12:18:20 +02:00
|
|
|
setLoadingStatus([true, 'processingUpdate'])
|
|
|
|
const result = await backend.createSet({ name })
|
2023-04-22 16:41:13 +02:00
|
|
|
if (result.success) {
|
2023-08-23 12:18:20 +02:00
|
|
|
setLoadingStatus([true, t('nailedIt'), true, true])
|
|
|
|
router.push(`/account/sets/${result.data.set.id}`)
|
|
|
|
} else setLoadingStatus([true, 'backendError', true, false])
|
2023-04-22 16:41:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2023-08-23 12:18:20 +02:00
|
|
|
<div className="max-w-xl">
|
2023-08-24 18:39:12 +02:00
|
|
|
<LoadingStatus />
|
2023-04-30 17:50:15 +02:00
|
|
|
<h5>{t('name')}</h5>
|
2023-04-22 16:41:13 +02:00
|
|
|
<p>{t('setNameDesc')}</p>
|
|
|
|
<input
|
2023-04-23 18:00:52 +02:00
|
|
|
autoFocus
|
2023-04-22 16:41:13 +02:00
|
|
|
value={name}
|
|
|
|
onChange={(evt) => setName(evt.target.value)}
|
|
|
|
className="input w-full input-bordered flex flex-row"
|
|
|
|
type="text"
|
|
|
|
placeholder={'Georg Cantor'}
|
|
|
|
/>
|
2023-04-30 17:50:15 +02:00
|
|
|
<div className="flex flex-row gap-2 items-center w-full mt-8 mb-2">
|
2023-04-22 16:41:13 +02:00
|
|
|
<button
|
|
|
|
className="btn btn-primary grow capitalize"
|
|
|
|
disabled={name.length < 1}
|
|
|
|
onClick={createSet}
|
|
|
|
>
|
|
|
|
{t('newSet')}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-08-23 12:18:20 +02:00
|
|
|
export const MeasieVal = ({ val, m, imperial }) =>
|
|
|
|
isDegreeMeasurement(m) ? <span>{val}°</span> : <span>{formatMm(val, imperial)}</span>
|
2023-04-23 18:00:52 +02:00
|
|
|
|
2023-08-23 17:41:34 +02:00
|
|
|
export const MsetBanner = ({ set, control, onClick = false, href = false }) => {
|
2023-08-24 18:39:12 +02:00
|
|
|
const { t } = useTranslation(ns)
|
2023-08-23 17:41:34 +02:00
|
|
|
const info = []
|
|
|
|
if (control > 1)
|
|
|
|
info.push([
|
|
|
|
<CalendarIcon />,
|
|
|
|
<b>
|
|
|
|
<TextOnBg txt={shortDate(i18n.language, set.createdAt, false)} />
|
|
|
|
</b>,
|
|
|
|
])
|
|
|
|
info.push([
|
|
|
|
<MeasieIcon />,
|
|
|
|
<b>
|
|
|
|
<TextOnBg txt={(set.measies ? Object.keys(set.measies).length : 0) + ' ' + t('measies')} />
|
|
|
|
</b>,
|
|
|
|
])
|
|
|
|
if (control > 2)
|
|
|
|
info.push([
|
|
|
|
set.public ? (
|
|
|
|
<OkIcon className="w-6 h-6 text-success" stroke={4} />
|
|
|
|
) : (
|
|
|
|
<NoIcon className="w-6 h-6 text-error" stroke={3} />
|
|
|
|
),
|
|
|
|
<b>
|
|
|
|
<TextOnBg txt={t(set.public ? 'publicSet' : 'privateSet')} />
|
|
|
|
</b>,
|
|
|
|
])
|
|
|
|
const inner = (
|
|
|
|
<>
|
|
|
|
<h2 className="bg-base-100 px-4 rounded-lg bg-opacity-50 py-2 rounded-l-none">
|
|
|
|
<TextOnBg txt={set.name} />
|
|
|
|
</h2>
|
|
|
|
{info.map((item) => (
|
2023-08-24 18:39:12 +02:00
|
|
|
<div
|
|
|
|
className="flex flex-row flex-wrap gap-2 bg-base-100 p-4 rounded bg-opacity-50 py-1 mt-2 rounded-l-none"
|
|
|
|
key={item[0]}
|
|
|
|
>
|
2023-08-23 17:41:34 +02:00
|
|
|
{item[0]}
|
|
|
|
{item[1]}
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
const props = {
|
|
|
|
className:
|
|
|
|
'bg-base-100 w-full mb-2 mx-auto flex flex-col items-start text-center justify-center rounded shadow py-4',
|
|
|
|
style: {
|
|
|
|
backgroundImage: `url(${cloudflareImageUrl({ type: 'w1000', id: set.img })})`,
|
|
|
|
backgroundSize: 'cover',
|
|
|
|
backgroundRepeat: 'no-repeat',
|
|
|
|
backgroundPosition: '50%',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return onClick ? (
|
|
|
|
<button {...props} onClick={onClick}>
|
|
|
|
{inner}
|
|
|
|
</button>
|
|
|
|
) : (
|
|
|
|
<Link {...props} href={href}>
|
|
|
|
{inner}
|
|
|
|
</Link>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-08-23 12:18:20 +02:00
|
|
|
export const Mset = ({ id, publicOnly = false }) => {
|
|
|
|
// Hooks
|
2023-08-23 17:41:34 +02:00
|
|
|
const { account, control } = useAccount()
|
2023-08-23 12:18:20 +02:00
|
|
|
const { setLoadingStatus, LoadingStatus } = useLoadingStatus()
|
|
|
|
const backend = useBackend()
|
|
|
|
const { t } = useTranslation(ns)
|
|
|
|
const docs = useSetDocs(locale)
|
|
|
|
const measieDocs = useMeasurementDocs(locale)
|
2023-04-23 18:00:52 +02:00
|
|
|
|
2023-08-23 12:18:20 +02:00
|
|
|
// Context
|
|
|
|
const { setModal } = useContext(ModalContext)
|
|
|
|
|
|
|
|
const [filter, setFilter] = useState(false)
|
|
|
|
const [edit, setEdit] = useState(false)
|
|
|
|
const [mset, setMset] = useState()
|
|
|
|
// Set fields for editing
|
|
|
|
const [name, setName] = useState(mset?.name)
|
|
|
|
const [image, setImage] = useState(mset?.image)
|
|
|
|
const [isPublic, setIsPublic] = useState(mset?.public ? true : false)
|
|
|
|
const [imperial, setImperial] = useState(mset?.imperial ? true : false)
|
|
|
|
const [notes, setNotes] = useState(mset?.notes || '')
|
|
|
|
const [measies, setMeasies] = useState({})
|
|
|
|
|
|
|
|
// Effect
|
|
|
|
useEffect(() => {
|
|
|
|
const getSet = async () => {
|
|
|
|
setLoadingStatus([true, t('backendLoadingStarted')])
|
|
|
|
const result = await backend.getSet(id)
|
|
|
|
if (result.success) {
|
|
|
|
setMset(result.data.set)
|
|
|
|
setName(result.data.set.name)
|
|
|
|
setImage(result.data.set.image)
|
|
|
|
setIsPublic(result.data.set.public ? true : false)
|
|
|
|
setImperial(result.data.set.imperial ? true : false)
|
|
|
|
setNotes(result.data.set.notes)
|
|
|
|
setMeasies(result.data.set.measies)
|
|
|
|
setLoadingStatus([true, 'backendLoadingCompleted', true, true])
|
|
|
|
} else setLoadingStatus([true, 'backendError', true, false])
|
2023-04-30 17:50:15 +02:00
|
|
|
}
|
2023-08-23 12:18:20 +02:00
|
|
|
const getPublicSet = async () => {
|
|
|
|
setLoadingStatus([true, t('backendLoadingStarted')])
|
|
|
|
const result = await backend.getPublicSet(id)
|
|
|
|
if (result.success) {
|
|
|
|
setMset({
|
|
|
|
...result.data,
|
|
|
|
public: true,
|
|
|
|
measies: result.data.measurements,
|
|
|
|
})
|
|
|
|
setName(result.data.name)
|
|
|
|
setImage(result.data.image)
|
|
|
|
setIsPublic(result.data.public ? true : false)
|
|
|
|
setImperial(result.data.imperial ? true : false)
|
|
|
|
setNotes(result.data.notes)
|
|
|
|
setMeasies(result.data.measurements)
|
|
|
|
setLoadingStatus([true, 'backendLoadingCompleted', true, true])
|
|
|
|
} else setLoadingStatus([true, 'backendError', true, false])
|
2023-04-23 18:00:52 +02:00
|
|
|
}
|
2023-08-23 12:18:20 +02:00
|
|
|
if (id) {
|
|
|
|
if (publicOnly) getPublicSet()
|
|
|
|
else getSet()
|
2023-04-23 18:00:52 +02:00
|
|
|
}
|
2023-08-23 12:18:20 +02:00
|
|
|
}, [id, publicOnly])
|
2023-04-23 18:00:52 +02:00
|
|
|
|
2023-08-23 12:18:20 +02:00
|
|
|
const filterMeasurements = () => {
|
|
|
|
if (!filter) return measurements.map((m) => t(`measurements:${m}`) + `|${m}`).sort()
|
|
|
|
else return designMeasurements[filter].map((m) => t(`measurements:${m}`) + `|${m}`).sort()
|
2023-04-23 18:00:52 +02:00
|
|
|
}
|
|
|
|
|
2023-08-23 12:18:20 +02:00
|
|
|
if (!id || !mset) return null
|
2023-04-23 18:00:52 +02:00
|
|
|
|
2023-08-23 12:18:20 +02:00
|
|
|
const updateMeasies = (m, val) => {
|
|
|
|
const newMeasies = { ...measies }
|
|
|
|
newMeasies[m] = val
|
|
|
|
setMeasies(newMeasies)
|
2023-04-23 18:00:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const save = async () => {
|
2023-08-23 12:18:20 +02:00
|
|
|
setLoadingStatus([true, 'gatheringInfo'])
|
|
|
|
// Compile data
|
|
|
|
const data = { measies: {} }
|
|
|
|
if (name || name !== mset.name) data.name = name
|
|
|
|
if (image || image !== mset.image) data.img = image
|
|
|
|
if ([true, false].includes(isPublic) && isPublic !== mset.public) data.public = isPublic
|
|
|
|
if ([true, false].includes(imperial) && imperial !== mset.imperial) data.imperial = imperial
|
|
|
|
if (notes || notes !== mset.notes) data.notes = notes
|
|
|
|
// Add measurements
|
|
|
|
for (const m of measurements) {
|
|
|
|
console.log(m)
|
|
|
|
if (measies[m] || measies[m] !== mset.measies[m]) data.measies[m] = measies[m]
|
2023-04-23 18:00:52 +02:00
|
|
|
}
|
2023-08-23 12:18:20 +02:00
|
|
|
setLoadingStatus([true, 'savingSet'])
|
|
|
|
console.log({ measies, data })
|
|
|
|
const result = await backend.updateSet(mset.id, data)
|
|
|
|
if (result.success) {
|
|
|
|
setMset(result.data.set)
|
|
|
|
setEdit(false)
|
|
|
|
setLoadingStatus([true, 'nailedIt', true, true])
|
|
|
|
} else setLoadingStatus([true, 'backendError', true, false])
|
2023-04-23 18:00:52 +02:00
|
|
|
}
|
|
|
|
|
2023-08-23 12:18:20 +02:00
|
|
|
const heading = (
|
|
|
|
<div className="flex flex-row flex-wrap gap-4 text-sm items-center justify-between mb-2">
|
|
|
|
<LoadingStatus />
|
2023-08-23 17:41:34 +02:00
|
|
|
<MsetBanner
|
|
|
|
set={mset}
|
|
|
|
control={control}
|
2023-08-23 12:18:20 +02:00
|
|
|
onClick={() =>
|
|
|
|
setModal(
|
|
|
|
<ModalWrapper flex="col" justify="top lg:justify-center" slideFrom="right">
|
|
|
|
<img src={cloudflareImageUrl({ type: 'public', id: mset.img })} />
|
|
|
|
</ModalWrapper>
|
|
|
|
)
|
|
|
|
}
|
2023-08-23 17:41:34 +02:00
|
|
|
/>
|
2023-08-23 12:18:20 +02:00
|
|
|
{account.control > 3 && mset.public ? (
|
|
|
|
<div className="flex flex-row gap-2 items-center">
|
2023-04-30 18:29:56 +02:00
|
|
|
<a
|
2023-08-23 12:18:20 +02:00
|
|
|
className="badge badge-secondary font-bold badge-lg"
|
2023-04-30 18:29:56 +02:00
|
|
|
href={`${conf.backend}/sets/${mset.id}.json`}
|
|
|
|
>
|
|
|
|
JSON
|
|
|
|
</a>
|
|
|
|
<a
|
2023-08-23 12:18:20 +02:00
|
|
|
className="badge badge-success font-bold badge-lg"
|
2023-04-30 18:29:56 +02:00
|
|
|
href={`${conf.backend}/sets/${mset.id}.yaml`}
|
|
|
|
>
|
|
|
|
YAML
|
|
|
|
</a>
|
2023-04-30 17:50:15 +02:00
|
|
|
</div>
|
2023-08-23 12:18:20 +02:00
|
|
|
) : (
|
|
|
|
<span></span>
|
|
|
|
)}
|
|
|
|
{!publicOnly && (
|
|
|
|
<>
|
|
|
|
{edit ? (
|
|
|
|
<div className="flex flex-row gap-2">
|
|
|
|
<button
|
|
|
|
onClick={() => setEdit(false)}
|
|
|
|
className="btn btn-primary btn-outline flex flex-row items-center gap-4"
|
|
|
|
>
|
|
|
|
<ResetIcon />
|
|
|
|
{t('cancel')}
|
|
|
|
</button>
|
|
|
|
<button onClick={save} className="btn btn-primary flex flex-row items-center gap-4">
|
|
|
|
<UploadIcon />
|
|
|
|
{t('saveThing', { thing: t('account:set') })}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<button onClick={() => setEdit(true)} className="btn btn-primary">
|
|
|
|
<EditIcon /> {t('editThing', { thing: t('account:set') })}
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)
|
2023-04-30 18:29:56 +02:00
|
|
|
|
2023-08-23 12:18:20 +02:00
|
|
|
if (!edit)
|
|
|
|
return (
|
|
|
|
<div className="max-w-2xl">
|
|
|
|
{heading}
|
|
|
|
{Object.keys(mset.measies).length > 0 && (
|
|
|
|
<>
|
|
|
|
<h2>{t('measies')}</h2>
|
|
|
|
{Object.entries(mset.measies).map(([m, val]) => (
|
2023-08-23 17:47:21 +02:00
|
|
|
<DisplayRow title={<MeasieVal m={m} val={val} />} key={m}>
|
2023-08-23 12:18:20 +02:00
|
|
|
<span className="font-medium">{t(m)}</span>
|
2023-08-23 17:47:21 +02:00
|
|
|
</DisplayRow>
|
2023-08-23 12:18:20 +02:00
|
|
|
))}
|
|
|
|
</>
|
|
|
|
)}
|
2023-04-30 18:29:56 +02:00
|
|
|
|
2023-08-23 12:18:20 +02:00
|
|
|
<h2>{t('data')}</h2>
|
2023-08-23 17:47:21 +02:00
|
|
|
<DisplayRow title={t('name')}>{mset.name}</DisplayRow>
|
|
|
|
<DisplayRow title={t('units')}>
|
|
|
|
{mset.imperial ? t('imerialUnits') : t('metricUnits')}
|
|
|
|
</DisplayRow>
|
2023-08-23 17:41:34 +02:00
|
|
|
{control >= controlLevels.sets.notes && (
|
2023-08-23 17:47:21 +02:00
|
|
|
<DisplayRow title={t('notes')}>
|
2023-08-23 17:41:34 +02:00
|
|
|
<Markdown>{mset.notes}</Markdown>
|
2023-08-23 17:47:21 +02:00
|
|
|
</DisplayRow>
|
2023-08-23 12:18:20 +02:00
|
|
|
)}
|
2023-08-23 17:41:34 +02:00
|
|
|
{control >= controlLevels.sets.public && (
|
|
|
|
<>
|
2023-08-23 17:47:21 +02:00
|
|
|
<DisplayRow title={t('public')}>
|
2023-08-23 17:41:34 +02:00
|
|
|
{mset.public ? (
|
|
|
|
<OkIcon className="w-6 h-6 text-success" stroke={4} />
|
|
|
|
) : (
|
|
|
|
<NoIcon className="w-6 h-6 text-error" stroke={3} />
|
|
|
|
)}
|
2023-08-23 17:47:21 +02:00
|
|
|
</DisplayRow>
|
2023-08-23 17:41:34 +02:00
|
|
|
{mset.public && (
|
2023-08-23 17:47:21 +02:00
|
|
|
<DisplayRow title={t('permalink')}>
|
2023-08-23 17:41:34 +02:00
|
|
|
<PageLink href={`/sets/${mset.id}`} txt={`/sets/${mset.id}`} />
|
2023-08-23 17:47:21 +02:00
|
|
|
</DisplayRow>
|
2023-08-23 17:41:34 +02:00
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{control >= controlLevels.sets.createdAt && (
|
2023-08-23 17:47:21 +02:00
|
|
|
<DisplayRow title={t('created')}>
|
2023-08-23 17:41:34 +02:00
|
|
|
<Timeago date={mset.createdAt} />
|
|
|
|
<span className="px-2 opacity-50">|</span>
|
|
|
|
{shortDate(locale, mset.createdAt, false)}
|
2023-08-23 17:47:21 +02:00
|
|
|
</DisplayRow>
|
2023-08-23 17:41:34 +02:00
|
|
|
)}
|
|
|
|
{control >= controlLevels.sets.createdAt && (
|
2023-08-23 17:47:21 +02:00
|
|
|
<DisplayRow title={t('updated')}>
|
2023-08-23 17:41:34 +02:00
|
|
|
<Timeago date={mset.updatedAt} />
|
|
|
|
<span className="px-2 opacity-50">|</span>
|
|
|
|
{shortDate(locale, mset.createdAt, false)}
|
2023-08-23 17:47:21 +02:00
|
|
|
</DisplayRow>
|
2023-08-23 17:41:34 +02:00
|
|
|
)}
|
2023-08-23 17:47:21 +02:00
|
|
|
{control >= controlLevels.sets.id && <DisplayRow title={t('id')}>{mset.id}</DisplayRow>}
|
2023-08-23 12:18:20 +02:00
|
|
|
</div>
|
|
|
|
)
|
2023-04-30 17:50:15 +02:00
|
|
|
|
2023-08-23 12:18:20 +02:00
|
|
|
return (
|
|
|
|
<div className="max-w-2xl">
|
|
|
|
{heading}
|
|
|
|
<ul className="list list-disc list-inside ml-4">
|
|
|
|
{['measies', 'data'].map((s) => (
|
|
|
|
<li key={s}>
|
|
|
|
<AnchorLink id={s} txt={t(s)} />
|
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
<ul className="list list-disc list-inside ml-4">
|
2023-08-23 17:41:34 +02:00
|
|
|
<li>
|
|
|
|
<AnchorLink id="name" txt={t('name')} />
|
|
|
|
</li>
|
|
|
|
{account.control >= conf.account.sets.img ? (
|
2023-08-23 12:18:20 +02:00
|
|
|
<li>
|
2023-08-23 17:41:34 +02:00
|
|
|
<AnchorLink id="image" txt={t('image')} />
|
2023-08-23 12:18:20 +02:00
|
|
|
</li>
|
2023-08-23 17:41:34 +02:00
|
|
|
) : null}
|
|
|
|
{['public', 'units', 'notes'].map((id) =>
|
|
|
|
account.control >= conf.account.sets[id] ? (
|
2023-08-24 18:39:12 +02:00
|
|
|
<li key={id}>
|
2023-08-23 17:41:34 +02:00
|
|
|
<AnchorLink id="units" txt={t(id)} />
|
|
|
|
</li>
|
|
|
|
) : null
|
|
|
|
)}
|
2023-08-23 12:18:20 +02:00
|
|
|
</ul>
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
<h2 id="measies">{t('measies')}</h2>
|
2023-08-23 17:41:34 +02:00
|
|
|
<div className="bg-secondary px-4 pt-1 pb-4 rounded-lg shadow bg-opacity-10">
|
|
|
|
<DesignDropdown
|
|
|
|
update={setFilter}
|
|
|
|
label={t('filterByDesign')}
|
|
|
|
current={filter}
|
|
|
|
firstOption={<option value="">{t('noFilter')}</option>}
|
|
|
|
docs={
|
|
|
|
<div className="max-w-prose">
|
|
|
|
<h2>
|
|
|
|
{t('measies')}: {t('filterByDesign')}
|
|
|
|
</h2>
|
|
|
|
<p>
|
|
|
|
If you have a specific design in mind, you can <b>filter by design</b> to only list
|
|
|
|
those measurements that are required for this design.
|
|
|
|
</p>
|
|
|
|
</div>
|
2023-04-30 17:50:15 +02:00
|
|
|
}
|
2023-08-23 17:41:34 +02:00
|
|
|
/>
|
2023-04-23 18:00:52 +02:00
|
|
|
</div>
|
2023-08-23 12:18:20 +02:00
|
|
|
{filterMeasurements().map((mplus) => {
|
|
|
|
const [translated, m] = mplus.split('|')
|
|
|
|
|
|
|
|
return (
|
|
|
|
<MeasieInput
|
|
|
|
key={m}
|
|
|
|
m={m}
|
|
|
|
docs={measieDocs[m]}
|
|
|
|
imperial={mset.imperial}
|
|
|
|
label={translated}
|
|
|
|
current={mset.measies[m]}
|
|
|
|
original={mset.measies[m]}
|
|
|
|
update={updateMeasies}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
})}
|
2023-04-23 18:00:52 +02:00
|
|
|
|
2023-08-23 12:18:20 +02:00
|
|
|
<h2 id="data">{t('data')}</h2>
|
2023-04-28 21:23:06 +02:00
|
|
|
|
2023-08-23 12:18:20 +02:00
|
|
|
{/* Name is always shown */}
|
|
|
|
<span id="name"></span>
|
|
|
|
<StringInput
|
|
|
|
label={t('name')}
|
|
|
|
update={setName}
|
|
|
|
current={name}
|
|
|
|
original={mset.name}
|
|
|
|
docs={docs.name}
|
|
|
|
placeholder="Georg Cantor"
|
|
|
|
valid={(val) => val && val.length > 0}
|
|
|
|
/>
|
2023-04-22 16:41:13 +02:00
|
|
|
|
2023-08-23 12:18:20 +02:00
|
|
|
{/* img: Control level determines whether or not to show this */}
|
|
|
|
<span id="image"></span>
|
|
|
|
{account.control >= conf.account.sets.img ? (
|
|
|
|
<PassiveImageInput
|
|
|
|
label={t('image')}
|
|
|
|
update={setImage}
|
|
|
|
current={image}
|
|
|
|
docs={docs.image}
|
|
|
|
valid={(val) => val.length > 0}
|
|
|
|
/>
|
|
|
|
) : null}
|
2023-04-22 16:41:13 +02:00
|
|
|
|
2023-08-23 12:18:20 +02:00
|
|
|
{/* public: Control level determines whether or not to show this */}
|
|
|
|
<span id="public"></span>
|
|
|
|
{account.control >= conf.account.sets.public ? (
|
|
|
|
<ListInput
|
|
|
|
label={t('public')}
|
|
|
|
update={setIsPublic}
|
|
|
|
docs={docs.public}
|
|
|
|
list={[
|
|
|
|
{
|
|
|
|
val: true,
|
|
|
|
label: (
|
|
|
|
<div className="flex flex-row items-center flex-wrap justify-between w-full">
|
|
|
|
<span>{t('publicSet')}</span>
|
|
|
|
<OkIcon
|
|
|
|
className="w-8 h-8 text-success bg-base-100 rounded-full p-1"
|
|
|
|
stroke={4}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
desc: t('publicSetDesc'),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
val: false,
|
|
|
|
label: (
|
|
|
|
<div className="flex flex-row items-center flex-wrap justify-between w-full">
|
|
|
|
<span>{t('privateSet')}</span>
|
|
|
|
<NoIcon className="w-8 h-8 text-error bg-base-100 rounded-full p-1" stroke={3} />
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
desc: t('privateSetDesc'),
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
current={isPublic}
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
|
|
|
|
{/* units: Control level determines whether or not to show this */}
|
|
|
|
<span id="units"></span>
|
|
|
|
{account.control >= conf.account.sets.units ? (
|
|
|
|
<ListInput
|
|
|
|
label={t('units')}
|
|
|
|
docs={docs.units}
|
|
|
|
update={setImperial}
|
|
|
|
list={[
|
|
|
|
{
|
|
|
|
val: false,
|
|
|
|
label: (
|
|
|
|
<div className="flex flex-row items-center flex-wrap justify-between w-full">
|
|
|
|
<span>{t('metricUnits')}</span>
|
|
|
|
<span className="text-inherit text-2xl pr-2">cm</span>
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
desc: t('metricUnitsd'),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
val: true,
|
|
|
|
label: (
|
|
|
|
<div className="flex flex-row items-center flex-wrap justify-between w-full">
|
|
|
|
<span>{t('imperialUnits')}</span>
|
|
|
|
<span className="text-inherit text-4xl pr-2">″</span>
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
desc: t('imperialUnitsd'),
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
current={imperial}
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
|
|
|
|
{/* notes: Control level determines whether or not to show this */}
|
|
|
|
<span id="notes"></span>
|
|
|
|
{account.control >= conf.account.sets.notes ? (
|
|
|
|
<MarkdownInput
|
|
|
|
label={t('notes')}
|
|
|
|
update={setNotes}
|
|
|
|
docs={docs.notes}
|
|
|
|
current={notes}
|
|
|
|
placeholder={t('mdSupport')}
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
<button
|
|
|
|
onClick={save}
|
|
|
|
className="btn btn-primary btn-lg flex flex-row items-center gap-4 mx-auto mt-8"
|
|
|
|
>
|
|
|
|
<UploadIcon />
|
|
|
|
{t('saveThing', { thing: t('account:set') })}
|
|
|
|
</button>
|
|
|
|
</div>
|
2023-04-22 16:41:13 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Component for the account/sets page
|
2023-04-30 17:50:15 +02:00
|
|
|
export const Sets = ({ title = true }) => {
|
2023-04-28 21:23:06 +02:00
|
|
|
// Hooks
|
2023-08-23 17:41:34 +02:00
|
|
|
const { control } = useAccount()
|
2023-08-20 18:48:40 +02:00
|
|
|
const backend = useBackend()
|
2023-08-23 17:41:34 +02:00
|
|
|
const { t, i18n } = useTranslation(ns)
|
2023-08-23 12:18:20 +02:00
|
|
|
const { setLoadingStatus, LoadingStatus, LoadingProgress } = useLoadingStatus()
|
2023-04-22 16:41:13 +02:00
|
|
|
|
2023-04-28 21:23:06 +02:00
|
|
|
// State
|
2023-04-22 16:41:13 +02:00
|
|
|
const [sets, setSets] = useState([])
|
2023-08-23 12:18:20 +02:00
|
|
|
const [selected, setSelected] = useState({})
|
|
|
|
const [refresh, setRefresh] = useState(0)
|
2023-04-22 16:41:13 +02:00
|
|
|
|
2023-04-28 21:23:06 +02:00
|
|
|
// Effects
|
2023-04-22 16:41:13 +02:00
|
|
|
useEffect(() => {
|
|
|
|
const getSets = async () => {
|
|
|
|
const result = await backend.getSets()
|
|
|
|
if (result.success) setSets(result.data.sets)
|
|
|
|
}
|
|
|
|
getSets()
|
2023-08-23 12:18:20 +02:00
|
|
|
}, [refresh])
|
|
|
|
|
|
|
|
// Helper var to see how many are selected
|
|
|
|
const selCount = Object.keys(selected).length
|
2023-04-22 16:41:13 +02:00
|
|
|
|
2023-08-23 12:18:20 +02:00
|
|
|
// Helper method to toggle single selection
|
|
|
|
const toggleSelect = (id) => {
|
|
|
|
const newSelected = { ...selected }
|
|
|
|
if (newSelected[id]) delete newSelected[id]
|
|
|
|
else newSelected[id] = 1
|
|
|
|
setSelected(newSelected)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper method to toggle select all
|
|
|
|
const toggleSelectAll = () => {
|
|
|
|
if (selCount === sets.length) setSelected({})
|
|
|
|
else {
|
|
|
|
const newSelected = {}
|
|
|
|
for (const set of sets) newSelected[set.id] = 1
|
|
|
|
setSelected(newSelected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper to delete one or more measurements sets
|
|
|
|
const removeSelectedSets = async () => {
|
|
|
|
let i = 0
|
|
|
|
for (const id in selected) {
|
|
|
|
i++
|
|
|
|
await backend.removeSet(id)
|
|
|
|
setLoadingStatus([true, <LoadingProgress val={i} max={selCount} msg={t('removingSets')} />])
|
|
|
|
}
|
|
|
|
setSelected({})
|
|
|
|
setRefresh(refresh + 1)
|
|
|
|
setLoadingStatus([true, 'nailedIt', true, true])
|
2023-04-30 17:50:15 +02:00
|
|
|
}
|
2023-04-22 16:41:13 +02:00
|
|
|
|
|
|
|
return (
|
2023-08-23 12:18:20 +02:00
|
|
|
<div className="max-w-4xl xl:pl-4">
|
|
|
|
<LoadingStatus />
|
2023-08-23 17:41:34 +02:00
|
|
|
<p className="text-center md:text-right">
|
|
|
|
<Link
|
|
|
|
className="btn btn-primary capitalize w-full md:w-auto"
|
|
|
|
bottom
|
|
|
|
primary
|
|
|
|
href="/new/set"
|
|
|
|
>
|
2023-08-23 18:49:21 +02:00
|
|
|
<PlusIcon />
|
2023-08-23 12:18:20 +02:00
|
|
|
{t('newSet')}
|
|
|
|
</Link>
|
|
|
|
</p>
|
|
|
|
{selCount ? (
|
|
|
|
<button className="btn btn-error" onClick={removeSelectedSets}>
|
|
|
|
<TrashIcon /> {selCount} {t('sets')}
|
|
|
|
</button>
|
|
|
|
) : null}
|
|
|
|
<table className="table table-auto">
|
|
|
|
<thead className="border border-base-300 border-b-2 border-t-0 border-x-0">
|
|
|
|
<tr className="b">
|
2023-08-23 17:41:34 +02:00
|
|
|
<th className="text-base-300 text-base w-4 px-1">
|
2023-08-23 12:18:20 +02:00
|
|
|
<input
|
|
|
|
type="checkbox"
|
|
|
|
className="checkbox checkbox-secondary"
|
|
|
|
onClick={toggleSelectAll}
|
|
|
|
checked={sets.length === selCount}
|
|
|
|
/>
|
|
|
|
</th>
|
2023-08-23 17:41:34 +02:00
|
|
|
<th className="text-base-300 text-base">{t('set')}</th>
|
2023-08-23 12:18:20 +02:00
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{sets.map((set, i) => (
|
|
|
|
<tr key={i}>
|
2023-08-23 17:41:34 +02:00
|
|
|
<td className="text-base font-medium px-0">
|
2023-08-23 12:18:20 +02:00
|
|
|
<input
|
|
|
|
type="checkbox"
|
|
|
|
checked={selected[set.id] ? true : false}
|
|
|
|
className="checkbox checkbox-secondary"
|
|
|
|
onClick={() => toggleSelect(set.id)}
|
|
|
|
/>
|
|
|
|
</td>
|
2023-08-23 17:41:34 +02:00
|
|
|
<td className="text-base font-medium px-0">
|
|
|
|
<MsetBanner control={control} href={`/account/sets/${set.id}`} set={set} />
|
2023-08-23 12:18:20 +02:00
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
))}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
2023-05-26 10:41:36 +02:00
|
|
|
<BackToAccountButton />
|
2023-04-22 16:41:13 +02:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|