2023-04-22 16:41:13 +02:00
|
|
|
// Dependencies
|
2023-04-23 18:00:52 +02:00
|
|
|
import { useState, useEffect, useContext, useCallback } from 'react'
|
2023-04-22 16:41:13 +02:00
|
|
|
import { useTranslation } from 'next-i18next'
|
2023-04-29 08:29:12 +02:00
|
|
|
import orderBy from 'lodash.orderby'
|
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-04-30 18:29:56 +02:00
|
|
|
import { freeSewingConfig as conf } from 'shared/config/freesewing.config.mjs'
|
2023-04-22 16:41:13 +02:00
|
|
|
// Hooks
|
2023-04-23 18:00:52 +02:00
|
|
|
import { useDropzone } from 'react-dropzone'
|
2023-04-22 16:41:13 +02:00
|
|
|
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-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-04-30 17:50:15 +02:00
|
|
|
import { Collapse, useCollapseButton } from 'shared/components/collapse.mjs'
|
2023-04-22 16:41:13 +02:00
|
|
|
import { BackToAccountButton, Choice } from './shared.mjs'
|
2023-04-23 18:00:52 +02:00
|
|
|
import { PageLink } from 'shared/components/page-link.mjs'
|
2023-04-30 14:53:49 +02:00
|
|
|
import { ModalDesignPicker } from 'shared/components/modal/design-picker.mjs'
|
2023-04-23 18:00:52 +02:00
|
|
|
import {
|
2023-04-30 14:53:49 +02:00
|
|
|
FilterIcon,
|
|
|
|
ClearIcon,
|
2023-04-23 18:00:52 +02:00
|
|
|
OkIcon,
|
|
|
|
NoIcon,
|
|
|
|
TrashIcon,
|
|
|
|
EditIcon,
|
|
|
|
} 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'
|
|
|
|
import { Tab } from './bio.mjs'
|
2023-04-23 18:00:52 +02:00
|
|
|
import Timeago from 'react-timeago'
|
|
|
|
import { Spinner } from 'shared/components/spinner.mjs'
|
2023-06-22 11:29:19 -05:00
|
|
|
import { MeasieRow } from 'shared/components/sets/measie-input.mjs'
|
2023-04-22 16:41:13 +02:00
|
|
|
|
2023-04-30 14:53:49 +02:00
|
|
|
export const ns = ['account', 'patterns', 'toast']
|
2023-04-22 16:41:13 +02:00
|
|
|
|
2023-04-30 20:31:28 +02:00
|
|
|
export const StandAloneNewSet = () => {
|
|
|
|
const { t } = useTranslation(['account'])
|
|
|
|
const toast = useToast()
|
2023-08-20 18:48:40 +02:00
|
|
|
const { account } = useAccount()
|
|
|
|
const backend = useBackend()
|
2023-04-30 20:31:28 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="max-w-xl">
|
|
|
|
<NewSet {...{ t, account, backend, toast }} title={false} standalone={true} />
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const NewSet = ({
|
2023-04-30 17:50:15 +02:00
|
|
|
t,
|
|
|
|
refresh,
|
|
|
|
closeCollapseButton,
|
|
|
|
backend,
|
|
|
|
toast,
|
|
|
|
title = true,
|
2023-04-30 20:31:28 +02:00
|
|
|
standalone = false,
|
2023-04-30 17:50:15 +02:00
|
|
|
}) => {
|
2023-04-28 21:23:06 +02:00
|
|
|
// Context
|
2023-05-19 16:31:28 +02:00
|
|
|
const { startLoading, stopLoading } = useContext(LoadingContext)
|
2023-04-28 21:23:06 +02:00
|
|
|
|
|
|
|
// Hooks
|
2023-04-22 16:41:13 +02:00
|
|
|
const router = useRouter()
|
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-04-28 21:23:06 +02:00
|
|
|
startLoading()
|
2023-04-22 16:41:13 +02:00
|
|
|
const result = await backend.createSet({
|
|
|
|
name,
|
|
|
|
})
|
|
|
|
if (result.success) {
|
|
|
|
toast.success(<span>{t('nailedIt')}</span>)
|
2023-04-30 20:31:28 +02:00
|
|
|
if (standalone) router.push('/account/sets/')
|
|
|
|
else {
|
|
|
|
refresh()
|
|
|
|
closeCollapseButton()
|
|
|
|
}
|
2023-04-22 16:41:13 +02:00
|
|
|
} else toast.for.backendError()
|
2023-04-28 21:23:06 +02:00
|
|
|
stopLoading()
|
2023-04-22 16:41:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
2023-04-30 17:50:15 +02:00
|
|
|
{title ? <h2>{t('newSet')}</h2> : null}
|
|
|
|
<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-04-23 18:00:52 +02:00
|
|
|
const EditField = (props) => {
|
|
|
|
if (props.field === 'name') return <EditName {...props} />
|
|
|
|
if (props.field === 'notes') return <EditNotes {...props} />
|
|
|
|
if (props.field === 'imperial') return <EditUnits {...props} />
|
|
|
|
if (props.field === 'public') return <EditPublic {...props} />
|
|
|
|
if (props.field === 'img') return <EditImg {...props} />
|
|
|
|
|
|
|
|
return <p>FIXME: No edit component for this field</p>
|
|
|
|
}
|
|
|
|
|
2023-05-08 14:03:47 +02:00
|
|
|
export const EditRow = (props) => (
|
2023-04-30 17:50:15 +02:00
|
|
|
<Collapse
|
|
|
|
color="secondary"
|
|
|
|
openTitle={props.title}
|
|
|
|
title={
|
|
|
|
<>
|
|
|
|
<div className="w-24 text-left md:text-right block md:inline font-bold pr-4">
|
|
|
|
{props.title}
|
|
|
|
</div>
|
|
|
|
<div className="grow">{props.children}</div>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
toggle={<EditIcon />}
|
|
|
|
toggleClasses="btn btn-secondary"
|
2023-04-30 11:58:17 +02:00
|
|
|
>
|
2023-04-30 17:50:15 +02:00
|
|
|
<EditField field="name" {...props} />
|
|
|
|
</Collapse>
|
2023-04-30 11:58:17 +02:00
|
|
|
)
|
|
|
|
|
2023-06-06 20:19:36 +02:00
|
|
|
const EditImg = ({ t, mset, account, backend, startLoading, stopLoading, toast, refresh }) => {
|
2023-04-23 18:00:52 +02:00
|
|
|
const [img, setImg] = useState(mset.img)
|
|
|
|
|
|
|
|
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-06-06 20:19:36 +02:00
|
|
|
const save = async () => {
|
|
|
|
startLoading()
|
|
|
|
const result = await backend.updateSet(mset.id, { img })
|
|
|
|
if (result.success) {
|
|
|
|
toast.for.settingsSaved()
|
|
|
|
refresh()
|
|
|
|
} else toast.for.backendError()
|
|
|
|
stopLoading()
|
2023-05-26 13:35:03 +02:00
|
|
|
}
|
2023-04-23 18:00:52 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div>
|
|
|
|
<img
|
|
|
|
alt="img"
|
|
|
|
src={img || account.img}
|
|
|
|
className="shadow mb-4 mask mask-squircle bg-neutral aspect-square"
|
|
|
|
/>
|
|
|
|
<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>
|
|
|
|
<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>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="flex flex-row gap-2 items-center justify-center mt-2">
|
|
|
|
<button className="btn btn-secondary" onClick={save}>
|
|
|
|
{t('save')}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-06-06 20:19:36 +02:00
|
|
|
const EditName = ({ t, mset, backend, toast, refresh, loading, startLoading, stopLoading }) => {
|
2023-04-23 18:00:52 +02:00
|
|
|
const [value, setValue] = useState(mset.name)
|
|
|
|
|
|
|
|
const update = async (evt) => {
|
|
|
|
evt.preventDefault()
|
|
|
|
if (evt.target.value !== value) {
|
|
|
|
setValue(evt.target.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const save = async () => {
|
|
|
|
startLoading()
|
|
|
|
const result = await backend.updateSet(mset.id, { name: value })
|
|
|
|
if (result.success) {
|
|
|
|
refresh()
|
|
|
|
toast.for.settingsSaved()
|
|
|
|
} else toast.for.backendError()
|
|
|
|
stopLoading()
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="flex flex-row gap-2">
|
|
|
|
<input
|
|
|
|
value={value}
|
|
|
|
onChange={update}
|
|
|
|
className="input w-full input-bordered flex flex-row"
|
|
|
|
type="text"
|
|
|
|
placeholder={t('name')}
|
|
|
|
/>
|
|
|
|
<button className="btn btn-secondary" onClick={save} disabled={value === mset.name}>
|
|
|
|
<span className="flex flex-row items-center gap-2">
|
|
|
|
{loading ? (
|
|
|
|
<>
|
|
|
|
<Spinner />
|
|
|
|
<span>{t('processing')}</span>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
t('save')
|
|
|
|
)}
|
|
|
|
</span>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-06-06 20:19:36 +02:00
|
|
|
const EditNotes = ({ t, mset, backend, toast, refresh, loading, startLoading, stopLoading }) => {
|
2023-04-23 18:00:52 +02:00
|
|
|
const [value, setValue] = useState(mset.notes)
|
|
|
|
const [activeTab, setActiveTab] = useState('edit')
|
|
|
|
|
|
|
|
const update = async (evt) => {
|
|
|
|
evt.preventDefault()
|
|
|
|
if (evt.target.value !== value) {
|
|
|
|
setValue(evt.target.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const save = async () => {
|
|
|
|
startLoading()
|
|
|
|
const result = await backend.updateSet(mset.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 (
|
|
|
|
<div>
|
|
|
|
<div className="tabs w-full">
|
|
|
|
<Tab id="edit" {...tabProps} />
|
|
|
|
<Tab id="preview" {...tabProps} />
|
|
|
|
</div>
|
|
|
|
<div className="flex flex-row items-center mt-4">
|
|
|
|
{activeTab === 'edit' ? (
|
|
|
|
<textarea
|
|
|
|
rows="5"
|
|
|
|
className="textarea textarea-bordered textarea-lg w-full"
|
|
|
|
placeholder={t('placeholder')}
|
|
|
|
onChange={update}
|
|
|
|
value={value}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<div className="text-left px-4 border w-full">
|
|
|
|
<Markdown>{value}</Markdown>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
<div className="my-2 flex gap-2 items-center justify-center">
|
|
|
|
<button className="btn btn-secondary" onClick={save} disabled={value === mset.name}>
|
|
|
|
<span className="flex flex-row items-center gap-2">
|
|
|
|
{loading ? (
|
|
|
|
<>
|
|
|
|
<Spinner />
|
|
|
|
<span>{t('processing')}</span>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
t('save')
|
|
|
|
)}
|
|
|
|
</span>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-06-06 20:19:36 +02:00
|
|
|
const EditUnits = ({ t, mset, backend, toast, refresh, startLoading, stopLoading }) => {
|
2023-04-23 18:00:52 +02:00
|
|
|
const [selection, setSelection] = useState(mset?.imperial === true ? 'imperial' : 'metric')
|
|
|
|
|
|
|
|
const update = async (val) => {
|
|
|
|
setSelection(val)
|
|
|
|
const asBool = val === 'imperial' ? true : false
|
|
|
|
if (asBool !== mset.imperial) {
|
|
|
|
startLoading()
|
|
|
|
const result = await backend.updateSet(mset.id, { imperial: asBool })
|
|
|
|
if (result.success) {
|
|
|
|
refresh()
|
|
|
|
toast.for.settingsSaved()
|
|
|
|
} else toast.for.backendError()
|
|
|
|
stopLoading()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-19 18:27:36 +02:00
|
|
|
//const save = async () => {
|
|
|
|
// startLoading()
|
|
|
|
// const result = await backend.updateSet(mset.id, { name: value })
|
|
|
|
// if (result.success) {
|
|
|
|
// refresh()
|
|
|
|
// toast.for.settingsSaved()
|
|
|
|
// } else toast.for.backendError()
|
|
|
|
// stopLoading()
|
|
|
|
//}
|
2023-04-23 18:00:52 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{['metric', 'imperial'].map((val) => (
|
|
|
|
<Choice
|
|
|
|
val={val}
|
|
|
|
t={t}
|
|
|
|
update={update}
|
|
|
|
current={selection}
|
|
|
|
bool
|
|
|
|
key={val}
|
|
|
|
boolIcons={{ yes: <span>"</span>, no: <span>cm</span> }}
|
|
|
|
>
|
|
|
|
<span className="block text-lg leading-5">
|
|
|
|
{selection === 1 && val === 2 ? t('showMore') : t(`${val}Units`)}
|
|
|
|
</span>
|
|
|
|
<span className="block text-normal font-light normal-case pt-1">{t(`${val}Unitsd`)}</span>
|
|
|
|
</Choice>
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-06-06 20:19:36 +02:00
|
|
|
const EditPublic = ({ t, mset, backend, toast, refresh, startLoading, stopLoading }) => {
|
2023-04-23 18:00:52 +02:00
|
|
|
const [selection, setSelection] = useState(mset.public)
|
|
|
|
|
|
|
|
const update = async (val) => {
|
|
|
|
setSelection(val)
|
|
|
|
if (val !== mset.public) {
|
|
|
|
startLoading()
|
|
|
|
const result = await backend.updateSet(mset.id, { public: val })
|
|
|
|
if (result.success) {
|
|
|
|
refresh()
|
|
|
|
toast.for.settingsSaved()
|
|
|
|
} else toast.for.backendError()
|
|
|
|
stopLoading()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{[true, false].map((val) => (
|
|
|
|
<Choice val={val} t={t} update={update} current={selection} bool key={val}>
|
|
|
|
<div className="flex flex-row gap-2 text-lg leading-5 items-center">
|
|
|
|
{val ? (
|
|
|
|
<>
|
|
|
|
<OkIcon className="w-6 h-6 text-success" /> <span>{t('publicSet')}</span>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<NoIcon className="w-6 h-6 text-error" /> <span>{t('privateSet')}</span>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
<div className="flex flex-row gap-2 text-normal font-light normal-case pt-1 items-center">
|
|
|
|
{val ? t('publicSetDesc') : t('privateSetDesc')}
|
|
|
|
</div>
|
|
|
|
</Choice>
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-05-08 14:03:47 +02:00
|
|
|
export const EditSectionTitle = ({ title }) => (
|
2023-04-30 14:53:49 +02:00
|
|
|
<h5 className="border border-solid border-b-2 border-r-0 border-l-0 border-t-0 border-primary mt-4 mb-2">
|
|
|
|
{title}
|
|
|
|
</h5>
|
|
|
|
)
|
|
|
|
|
2023-04-23 18:00:52 +02:00
|
|
|
const EditMeasurementsSet = (props) => {
|
2023-04-30 14:53:49 +02:00
|
|
|
const [filter, setFilter] = useState(false)
|
2023-06-06 20:19:36 +02:00
|
|
|
const { mset, t, setModal, startLoading, stopLoading, toast, refresh, backend } = props
|
2023-04-23 18:00:52 +02:00
|
|
|
|
2023-04-30 14:53:49 +02:00
|
|
|
const filterMeasurements = () => {
|
|
|
|
if (!filter) return measurements.map((m) => t(`measurements:${m}`)).sort()
|
|
|
|
else return designMeasurements[filter].map((m) => t(`measurements:${m}`)).sort()
|
|
|
|
}
|
2023-04-23 18:00:52 +02:00
|
|
|
|
2023-06-06 20:19:36 +02:00
|
|
|
const saveProps = { t, startLoading, stopLoading, toast, refresh, backend }
|
|
|
|
|
2023-04-23 18:00:52 +02:00
|
|
|
return (
|
2023-04-30 17:50:15 +02:00
|
|
|
<div className="p-4">
|
2023-04-30 18:29:56 +02:00
|
|
|
{/* Meta info */}
|
|
|
|
{props.account.control > 2 ? (
|
|
|
|
<div className="flex flex-row gap-4 text-sm items-center justify-center mb-2">
|
|
|
|
<div className="flex flex-row gap-2 items-center">
|
|
|
|
<b>{t('permalink')}:</b>
|
|
|
|
{mset.public ? (
|
|
|
|
<PageLink href={`/sets/${mset.id}`} txt={`/sets/${mset.id}`} />
|
|
|
|
) : (
|
|
|
|
<NoIcon className="w-4 h-4 text-error" />
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<b>{t('created')}</b>: <Timeago date={mset.createdAt} />
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<b>{t('updated')}</b>: <Timeago date={mset.updatedAt} />
|
|
|
|
</div>
|
2023-04-23 18:00:52 +02:00
|
|
|
</div>
|
2023-04-30 18:29:56 +02:00
|
|
|
) : null}
|
|
|
|
|
|
|
|
{/* JSON & YAML links */}
|
|
|
|
{props.account.control > 3 ? (
|
|
|
|
<div className="flex flex-row gap-4 text-sm items-center justify-center">
|
|
|
|
<a
|
|
|
|
className="badge badge-secondary font-bold"
|
|
|
|
href={`${conf.backend}/sets/${mset.id}.json`}
|
|
|
|
>
|
|
|
|
JSON
|
|
|
|
</a>
|
|
|
|
<a
|
|
|
|
className="badge badge-success font-bold"
|
|
|
|
href={`${conf.backend}/sets/${mset.id}.yaml`}
|
|
|
|
>
|
|
|
|
YAML
|
|
|
|
</a>
|
2023-04-30 17:50:15 +02:00
|
|
|
</div>
|
2023-04-30 18:29:56 +02:00
|
|
|
) : null}
|
2023-04-30 14:53:49 +02:00
|
|
|
|
2023-04-30 17:50:15 +02:00
|
|
|
<EditSectionTitle title={t('data')} />
|
2023-04-30 18:29:56 +02:00
|
|
|
|
|
|
|
{/* Name is always shown */}
|
2023-06-06 20:19:36 +02:00
|
|
|
<EditRow title={t('name')} field="name" {...props} {...saveProps}>
|
2023-04-30 17:50:15 +02:00
|
|
|
{mset.name}
|
|
|
|
</EditRow>
|
2023-04-30 18:29:56 +02:00
|
|
|
|
|
|
|
{/* img: Control level determines whether or not to show this */}
|
|
|
|
{props.account.control >= conf.account.sets.img ? (
|
2023-06-06 20:19:36 +02:00
|
|
|
<EditRow title={t('image')} field="img" {...props} {...saveProps}>
|
2023-04-30 18:29:56 +02:00
|
|
|
<img src={mset.img} className="w-10 mask mask-squircle bg-neutral aspect-square" />
|
|
|
|
</EditRow>
|
|
|
|
) : null}
|
|
|
|
|
|
|
|
{/* public: Control level determines whether or not to show this */}
|
|
|
|
{props.account.control >= conf.account.sets.public ? (
|
2023-06-06 20:19:36 +02:00
|
|
|
<EditRow title={t('public')} field="public" {...props} {...saveProps}>
|
2023-04-30 18:29:56 +02:00
|
|
|
<div className="flex flex-row gap-2">
|
|
|
|
{mset.public ? (
|
|
|
|
<>
|
|
|
|
<OkIcon className="h-6 w-6 text-success" /> <span>{t('publicSet')}</span>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<NoIcon className="h-6 w-6 text-error" /> <span>{t('privateSet')}</span>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</EditRow>
|
|
|
|
) : null}
|
|
|
|
|
|
|
|
{/* units: Control level determines whether or not to show this */}
|
|
|
|
{props.account.control >= conf.account.sets.units ? (
|
2023-06-06 20:19:36 +02:00
|
|
|
<EditRow title={t('units')} field="imperial" {...props} {...saveProps}>
|
2023-04-30 18:29:56 +02:00
|
|
|
{mset.imperial ? t('imperialUnits') : t('metricUnits')}
|
|
|
|
</EditRow>
|
|
|
|
) : null}
|
|
|
|
|
|
|
|
{/* notes: Control level determines whether or not to show this */}
|
|
|
|
{props.account.control >= conf.account.sets.notes ? (
|
2023-06-06 20:19:36 +02:00
|
|
|
<EditRow title={t('notes')} field="notes" {...props} {...saveProps}>
|
2023-04-30 18:29:56 +02:00
|
|
|
<Markdown>{mset.notes}</Markdown>
|
|
|
|
</EditRow>
|
|
|
|
) : null}
|
2023-04-30 17:50:15 +02:00
|
|
|
|
|
|
|
<EditSectionTitle title={t('measies')} />
|
|
|
|
<div className="flex flex-row items-center justify-center">
|
|
|
|
<button
|
|
|
|
className="btn btn-secondary btn-outline flex flex-row gap-4 rounded-r-none"
|
|
|
|
onClick={() =>
|
|
|
|
setModal(
|
|
|
|
<ModalDesignPicker
|
|
|
|
designs={Object.keys(designMeasurements)}
|
|
|
|
setModal={setModal}
|
|
|
|
setter={setFilter}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<FilterIcon />
|
|
|
|
{filter ? t(`designs:${filter}.t`) : t(`designs:allDesigns`)}
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
className="btn btn-secondary btn-outline rounded-l-none border-l-0"
|
|
|
|
onClick={() => setFilter(false)}
|
|
|
|
>
|
|
|
|
<ClearIcon />
|
|
|
|
</button>
|
2023-04-23 18:00:52 +02:00
|
|
|
</div>
|
2023-04-30 17:50:15 +02:00
|
|
|
{filterMeasurements().map((m) => (
|
2023-06-10 11:59:08 +02:00
|
|
|
<MeasieRow key={m} m={m} {...props} {...saveProps} />
|
2023-04-30 17:50:15 +02:00
|
|
|
))}
|
2023-04-23 18:00:52 +02:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const MeasurementsSet = ({ mset, t, account, backend, refresh }) => {
|
2023-04-28 21:23:06 +02:00
|
|
|
// Context
|
2023-05-19 16:31:28 +02:00
|
|
|
const { startLoading, stopLoading } = useContext(LoadingContext)
|
2023-04-28 21:23:06 +02:00
|
|
|
const { setModal } = useContext(ModalContext)
|
|
|
|
|
|
|
|
// Hooks
|
2023-04-22 16:41:13 +02:00
|
|
|
const toast = useToast()
|
|
|
|
|
|
|
|
const remove = async () => {
|
2023-04-28 21:23:06 +02:00
|
|
|
startLoading()
|
2023-04-29 08:29:12 +02:00
|
|
|
const result = await backend.removeSet(mset.id)
|
2023-04-22 16:41:13 +02:00
|
|
|
if (result) toast.success(t('gone'))
|
|
|
|
else toast.for.backendError()
|
|
|
|
// This just forces a refresh of the list from the server
|
|
|
|
// We obviously did not add a key here, but rather removed one
|
2023-04-23 18:00:52 +02:00
|
|
|
refresh()
|
2023-04-28 21:23:06 +02:00
|
|
|
stopLoading()
|
2023-04-22 16:41:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const removeModal = () => {
|
2023-04-28 21:23:06 +02:00
|
|
|
setModal(
|
|
|
|
<ModalWrapper slideFrom="top">
|
2023-04-22 16:41:13 +02:00
|
|
|
<h2>{t('areYouCertain')}</h2>
|
2023-04-29 08:29:12 +02:00
|
|
|
<p>{t('deleteSetWarning')}</p>
|
2023-04-22 16:41:13 +02:00
|
|
|
<p className="flex flex-row gap-4 items-center justify-center">
|
|
|
|
<button className="btn btn-neutral btn-outline px-8">{t('cancel')}</button>
|
|
|
|
<button className="btn btn-error px-8" onClick={remove}>
|
|
|
|
{t('delete')}
|
|
|
|
</button>
|
|
|
|
</p>
|
|
|
|
</ModalWrapper>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-04-30 17:50:15 +02:00
|
|
|
return (
|
|
|
|
<Collapse
|
|
|
|
primary
|
|
|
|
top
|
|
|
|
bottom
|
|
|
|
title={
|
|
|
|
<>
|
|
|
|
<img src={mset.img} className="w-10 mask mask-squircle bg-neutral aspect-square" />
|
|
|
|
<span>{mset.name}</span>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
openTitle={mset.name}
|
|
|
|
buttons={[
|
|
|
|
<button
|
|
|
|
key="rm"
|
|
|
|
className="btn btn-error hover:text-error-content border-0"
|
|
|
|
onClick={account.control > 4 ? remove : removeModal}
|
|
|
|
>
|
|
|
|
<TrashIcon key="button2" />
|
|
|
|
</button>,
|
|
|
|
]}
|
|
|
|
>
|
2023-06-06 20:19:36 +02:00
|
|
|
<EditMeasurementsSet
|
|
|
|
{...{ t, mset, account, backend, toast, refresh, setModal, startLoading, stopLoading }}
|
|
|
|
/>
|
2023-04-30 17:50:15 +02:00
|
|
|
</Collapse>
|
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-20 18:48:40 +02:00
|
|
|
const { account } = useAccount()
|
|
|
|
const backend = useBackend()
|
2023-04-22 16:41:13 +02:00
|
|
|
const { t } = useTranslation(ns)
|
|
|
|
const toast = useToast()
|
2023-04-30 17:50:15 +02:00
|
|
|
const { CollapseButton, closeCollapseButton } = useCollapseButton()
|
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([])
|
|
|
|
const [added, setAdded] = useState(0)
|
|
|
|
|
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()
|
|
|
|
}, [added])
|
|
|
|
|
2023-04-28 21:23:06 +02:00
|
|
|
// Helper method to force a refresh
|
2023-04-30 17:50:15 +02:00
|
|
|
const refresh = () => {
|
|
|
|
setAdded(added + 1)
|
|
|
|
}
|
2023-04-22 16:41:13 +02:00
|
|
|
|
|
|
|
return (
|
2023-04-23 18:00:52 +02:00
|
|
|
<div className="max-w-2xl xl:pl-4">
|
2023-04-30 17:50:15 +02:00
|
|
|
{title ? <h2>{t('sets')}</h2> : null}
|
|
|
|
{orderBy(sets, ['name'], ['asc']).map((mset) => (
|
|
|
|
<MeasurementsSet {...{ account, mset, t, backend, refresh }} key={mset.id} />
|
|
|
|
))}
|
|
|
|
<CollapseButton
|
|
|
|
primary
|
|
|
|
title={t('newSet')}
|
|
|
|
className="btn btn-primary w-full capitalize mt-4"
|
|
|
|
>
|
|
|
|
<NewSet {...{ t, account, backend, toast, refresh, closeCollapseButton }} title={false} />
|
|
|
|
</CollapseButton>
|
2023-05-26 10:41:36 +02:00
|
|
|
<BackToAccountButton />
|
2023-04-22 16:41:13 +02:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|