// Hooks import React, { useState, useEffect, Fragment, useContext } from 'react' import { useBackend } from '@freesewing/react/hooks/useBackend' // Context import { LoadingStatusContext } from '@freesewing/react/context/LoadingStatus' // Components import { PlusIcon, TrashIcon, LeftIcon } from '@freesewing/react/components/Icon' import { Link as WebLink } from '@freesewing/react/components/Link' //import { DisplayRow } from './shared.mjs' //import { StringInput } from 'shared/components/inputs.mjs' //import { DynamicMdx } from 'shared/components/mdx/dynamic.mjs' /** * Component for the account/bookmarks page * * @param {object} props - All React props * @param {function} Link - An optional custom Link component */ export const AccountBookmarks = ({ Link = false }) => { if (!Link) Link = WebLink // Hooks const backend = useBackend() const { setLoadingStatus, LoadingProgress } = useContext(LoadingStatusContext) // State const [bookmarks, setBookmarks] = useState([]) const [selected, setSelected] = useState({}) const [refresh, setRefresh] = useState(0) // Helper var to see how many are selected const selCount = Object.keys(selected).length // Effects useEffect(() => { const getBookmarks = async () => { const [status, body] = await backend.getBookmarks() if (status === 200 && body.result === 'success') setBookmarks(body.bookmarks) } getBookmarks() }, [refresh]) // 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 === bookmarks.length) setSelected({}) else { const newSelected = {} for (const bookmark of bookmarks) newSelected[bookmark.id] = 1 setSelected(newSelected) } } // Helper to delete one or more bookmarks const removeSelectedBookmarks = async () => { let i = 0 for (const id in selected) { i++ await backend.removeBookmark(id) setLoadingStatus([ true, , ]) } setSelected({}) setRefresh(refresh + 1) setLoadingStatus([true, 'nailedIt', true, true]) } const perType = {} for (const type in types) perType[type] = bookmarks.filter((b) => b.type === type) return (

New Bookmark

{bookmarks.length > 0 ? ( ) : null} {Object.entries(types).map(([type, title]) => perType[type].length > 0 ? (

{title}

{bookmarks .filter((bookmark) => bookmark.type === type) .map((bookmark, i) => ( ))}
Title Location
toggleSelect(bookmark.id)} /> {bookmark.title} {bookmark.url.length > 30 ? bookmark.url.slice(0, 30) + '...' : bookmark.url}
) : null )}
) } const types = { design: 'Designs', pattern: 'Patterns', set: 'Measurements Sets', cset: 'Curated Measurements Sets', doc: 'Documentation', custom: 'Custom Bookmarks', } export const Bookmark = ({ bookmark }) => { const { t } = useTranslation(ns) return bookmark ? (
{bookmark.title} {bookmark.url.length > 30 ? bookmark.url.slice(0, 30) + '...' : bookmark.url} {t(`${bookmark.type}Bookmark`)}
{t('bookmarks')}
) : null } // Component for the 'new/bookmark' page export const NewBookmark = () => { // Hooks const { setLoadingStatus } = useContext(LoadingStatusContext) const router = useRouter() const backend = useBackend() const { t, i18n } = useTranslation(ns) const docs = {} for (const option of ['title', 'location', 'type']) { docs[option] = ( ) } // State const [title, setTitle] = useState('') const [url, setUrl] = useState('') const createBookmark = async () => { setLoadingStatus([true, 'processingUpdate']) const result = await backend.createBookmark({ title, url, type: 'custom', }) if (result.success) { setLoadingStatus([true, 'nailedIt', true, true]) router.push('/account/bookmarks') } else setLoadingStatus([true, 'backendError', true, false]) } return (
val.length > 0} placeholder={t('account')} /> val.length > 0} placeholder={'https://freesewing.org/account'} />
) } const t = (input) => input