1
0
Fork 0
freesewing/packages/react/components/Account/AccountBookmarks.mjs

250 lines
7.8 KiB
JavaScript
Raw Normal View History

2024-12-14 11:34:23 +01:00
// Hooks
2024-12-14 13:40:17 +01:00
import React, { useState, useEffect, Fragment, useContext } from 'react'
import { useBackend } from '@freesewing/react/hooks/useBackend'
// Context
import { LoadingStatusContext } from '@freesewing/react/context/LoadingStatus'
2024-12-14 11:34:23 +01:00
// Components
2024-12-14 13:40:17 +01:00
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
2024-12-14 11:34:23 +01:00
// 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 () => {
2024-12-14 13:40:17 +01:00
const [status, body] = await backend.getBookmarks()
if (status === 200 && body.result === 'success') setBookmarks(body.bookmarks)
2024-12-14 11:34:23 +01:00
}
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,
<LoadingProgress val={i} max={selCount} msg={t('removingBookmarks')} key="linter" />,
])
}
setSelected({})
setRefresh(refresh + 1)
setLoadingStatus([true, 'nailedIt', true, true])
}
const perType = {}
2024-12-14 13:40:17 +01:00
for (const type in types) perType[type] = bookmarks.filter((b) => b.type === type)
2024-12-14 11:34:23 +01:00
return (
<div className="max-w-4xl xl:pl-4">
<p className="text-center md:text-right">
<Link
2024-12-14 13:40:17 +01:00
className="daisy-btn daisy-btn-primary capitalize w-full md:w-auto hover:text-primary-content hover:no-underline"
2024-12-14 11:34:23 +01:00
href="/new/bookmark"
>
<PlusIcon />
2024-12-14 13:40:17 +01:00
New Bookmark
2024-12-14 11:34:23 +01:00
</Link>
</p>
{bookmarks.length > 0 ? (
2024-12-14 13:40:17 +01:00
<button
className="daisy-btn daisy-btn-error"
onClick={removeSelectedBookmarks}
disabled={selCount < 1}
>
<TrashIcon /> {selCount} Bookmarks
2024-12-14 11:34:23 +01:00
</button>
) : null}
2024-12-14 13:40:17 +01:00
{Object.entries(types).map(([type, title]) =>
2024-12-14 11:34:23 +01:00
perType[type].length > 0 ? (
<Fragment key={type}>
2024-12-14 13:40:17 +01:00
<h2>{title}</h2>
<table className="table tableauto w-full">
2024-12-14 11:34:23 +01:00
<thead className="border border-base-300 border-b-2 border-t-0 border-x-0">
2024-12-14 13:40:17 +01:00
<tr>
<th className="text-base-300 text-base text-left w-8">
2024-12-14 11:34:23 +01:00
<input
type="checkbox"
2024-12-14 13:40:17 +01:00
className="daisy-checkbox daisy-checkbox-secondary"
2024-12-14 11:34:23 +01:00
onClick={toggleSelectAll}
checked={bookmarks.length === selCount}
/>
</th>
2024-12-14 13:40:17 +01:00
<th className="w-1/2">Title</th>
<th>Location</th>
2024-12-14 11:34:23 +01:00
</tr>
</thead>
<tbody>
{bookmarks
.filter((bookmark) => bookmark.type === type)
.map((bookmark, i) => (
<tr key={i}>
<td className="text-base font-medium">
<input
type="checkbox"
checked={selected[bookmark.id] ? true : false}
2024-12-14 13:40:17 +01:00
className="daisy-checkbox daisy-checkbox-secondary"
2024-12-14 11:34:23 +01:00
onClick={() => toggleSelect(bookmark.id)}
/>
</td>
<td className="text-base font-medium">
2024-12-14 13:40:17 +01:00
<Link href={`/account/bookmark?id=${bookmark.id}`}>{bookmark.title}</Link>
2024-12-14 11:34:23 +01:00
</td>
<td className="text-base font-medium">
2024-12-14 13:40:17 +01:00
<WebLink href={bookmark.url}>
{bookmark.url.length > 30
? bookmark.url.slice(0, 30) + '...'
: bookmark.url}
</WebLink>
2024-12-14 11:34:23 +01:00
</td>
</tr>
))}
</tbody>
</table>
</Fragment>
) : null
)}
</div>
)
}
2024-12-14 13:40:17 +01:00
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 ? (
<div>
<DisplayRow title={t('title')}>{bookmark.title}</DisplayRow>
<DisplayRow title={t('url')}>
{bookmark.url.length > 30 ? bookmark.url.slice(0, 30) + '...' : bookmark.url}
</DisplayRow>
<DisplayRow title={t('type')}>{t(`${bookmark.type}Bookmark`)}</DisplayRow>
<div className="flex flex-row flex-wrap md:gap-2 md:items-center md:justify-between mt-8">
<Link
href="/account/bookmarks"
className="w-full md:w-auto daisy-btn daisy-btn-secondary pr-6 flex flex-row items-center gap-2"
>
<LeftIcon />
{t('bookmarks')}
</Link>
</div>
</div>
) : 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] = (
<DynamicMdx language={i18n.language} slug={`docs/about/site/bookmarks/${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 (
<div className="max-w-2xl xl:pl-4">
<StringInput
id="bookmark-title"
label={t('title')}
docs={docs.title}
update={setTitle}
current={title}
valid={(val) => val.length > 0}
placeholder={t('account')}
/>
<StringInput
id="bookmark-url"
label={t('location')}
docs={docs.location}
update={setUrl}
current={url}
valid={(val) => val.length > 0}
placeholder={'https://freesewing.org/account'}
/>
<div className="flex flex-row gap-2 items-center w-full my-8">
<button
className="daisy-btn daisy-btn-primary grow capitalize"
disabled={!(title.length > 0 && url.length > 0)}
onClick={createBookmark}
>
{t('newBookmark')}
</button>
</div>
</div>
)
}
const t = (input) => input