// Dependencies
import { useState, useEffect, useContext, useCallback } from 'react'
import { useTranslation } from 'next-i18next'
import orderBy from 'lodash.orderby'
import { measurements } from 'config/measurements.mjs'
import { measurements as designMeasurements } from 'shared/prebuild/data/design-measurements.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 { Collapse, useCollapseButton } from 'shared/components/collapse.mjs'
import { BackToAccountButton, Choice } from './shared.mjs'
import { PageLink } from 'shared/components/page-link.mjs'
import { ModalDesignPicker } from 'shared/components/modal/design-picker.mjs'
import {
FilterIcon,
ClearIcon,
OkIcon,
NoIcon,
TrashIcon,
EditIcon,
} 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'
import { MeasieRow } from 'shared/components/sets/measie-input.mjs'
export const ns = ['account', 'patterns', 'toast']
export const StandAloneNewSet = () => {
const { t } = useTranslation(['account'])
const toast = useToast()
const { account, token } = useAccount()
const backend = useBackend(token)
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 === 'imperial') 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}
>
}
toggle={ }
toggleClasses="btn btn-secondary"
>
)
const EditImg = ({ t, mset, account, backend, startLoading, stopLoading, toast, refresh }) => {
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 })
const save = async () => {
startLoading()
const result = await backend.updateSet(mset.id, { img })
if (result.success) {
toast.for.settingsSaved()
refresh()
} else toast.for.backendError()
stopLoading()
}
return (
)
}
const EditName = ({ t, mset, backend, toast, refresh, loading, startLoading, stopLoading }) => {
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 (
{loading ? (
<>
{t('processing')}
>
) : (
t('save')
)}
)
}
const EditNotes = ({ t, mset, backend, toast, refresh, loading, startLoading, stopLoading }) => {
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 (
{activeTab === 'edit' ? (
) : (
{value}
)}
{loading ? (
<>
{t('processing')}
>
) : (
t('save')
)}
)
}
const EditUnits = ({ t, mset, backend, toast, refresh, startLoading, stopLoading }) => {
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()
}
}
//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 (
<>
{['metric', 'imperial'].map((val) => (
", no: cm }}
>
{selection === 1 && val === 2 ? t('showMore') : t(`${val}Units`)}
{t(`${val}Unitsd`)}
))}
>
)
}
const EditPublic = ({ t, mset, backend, toast, refresh, startLoading, stopLoading }) => {
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) => (
{val ? (
<>
{t('publicSet')}
>
) : (
<>
{t('privateSet')}
>
)}
{val ? t('publicSetDesc') : t('privateSetDesc')}
))}
>
)
}
export const EditSectionTitle = ({ title }) => (
{title}
)
const EditMeasurementsSet = (props) => {
const [filter, setFilter] = useState(false)
const { mset, t, setModal, startLoading, stopLoading, toast, refresh, backend } = props
const filterMeasurements = () => {
if (!filter) return measurements.map((m) => t(`measurements:${m}`)).sort()
else return designMeasurements[filter].map((m) => t(`measurements:${m}`)).sort()
}
const saveProps = { t, startLoading, stopLoading, toast, refresh, backend }
return (
{/* Meta info */}
{props.account.control > 2 ? (
{t('permalink')}:
{mset.public ? (
) : (
)}
{t('created')} :
{t('updated')} :
) : null}
{/* JSON & YAML links */}
{props.account.control > 3 ? (
) : null}
{/* Name is always shown */}
{mset.name}
{/* img: Control level determines whether or not to show this */}
{props.account.control >= conf.account.sets.img ? (
) : null}
{/* public: Control level determines whether or not to show this */}
{props.account.control >= conf.account.sets.public ? (
{mset.public ? (
<>
{t('publicSet')}
>
) : (
<>
{t('privateSet')}
>
)}
) : null}
{/* units: Control level determines whether or not to show this */}
{props.account.control >= conf.account.sets.units ? (
{mset.imperial ? t('imperialUnits') : t('metricUnits')}
) : null}
{/* notes: Control level determines whether or not to show this */}
{props.account.control >= conf.account.sets.notes ? (
{mset.notes}
) : null}
setModal(
)
}
>
{filter ? t(`designs:${filter}.t`) : t(`designs:allDesigns`)}
setFilter(false)}
>
{filterMeasurements().map((m) => (
))}
)
}
const MeasurementsSet = ({ mset, 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.removeSet(mset.id)
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
refresh()
stopLoading()
}
const removeModal = () => {
setModal(
{t('areYouCertain')}
{t('deleteSetWarning')}
{t('cancel')}
{t('delete')}
)
}
return (
{mset.name}
>
}
openTitle={mset.name}
buttons={[
4 ? remove : removeModal}
>
,
]}
>
)
}
// Component for the 'new/apikey' page
//export const NewApikey = ({ app, standAlone = false }) => {
// const { account, token } = useAccount()
// const backend = useBackend(token)
// const { t } = useTranslation(ns)
// const toast = useToast()
//
// const [keys, setKeys] = useState([])
// const [generate, setGenerate] = useState(false)
// const [added, setAdded] = useState(0)
//
// const oneAdded = () => setAdded(added + 1)
//
// return (
//
//
//
// )
//}
// Component for the account/sets page
export const Sets = ({ title = true }) => {
// Hooks
const { account, token } = useAccount()
const backend = useBackend(token)
const { t } = useTranslation(ns)
const toast = useToast()
const { CollapseButton, closeCollapseButton } = useCollapseButton()
// State
const [sets, setSets] = useState([])
const [added, setAdded] = useState(0)
// Effects
useEffect(() => {
const getSets = async () => {
const result = await backend.getSets()
if (result.success) setSets(result.data.sets)
}
getSets()
}, [added])
// Helper method to force a refresh
const refresh = () => {
setAdded(added + 1)
}
return (
{title ?
{t('sets')} : null}
{orderBy(sets, ['name'], ['asc']).map((mset) => (
))}
)
}