From 5abbc559b91256a8244c55a26a300ed41c4ef5f5 Mon Sep 17 00:00:00 2001 From: joostdecock Date: Tue, 26 Sep 2023 13:49:38 +0200 Subject: [PATCH] feat(org): Load curated sets in workbench --- sites/org/pages/curate/sets/index.mjs | 4 +- sites/shared/components/account/sets.mjs | 106 ------------- sites/shared/components/curated-sets.mjs | 144 +++++++++++++++++- .../workbench/views/measies/index.mjs | 2 +- 4 files changed, 143 insertions(+), 113 deletions(-) diff --git a/sites/org/pages/curate/sets/index.mjs b/sites/org/pages/curate/sets/index.mjs index 737dffe4355..87fdb7057ff 100644 --- a/sites/org/pages/curate/sets/index.mjs +++ b/sites/org/pages/curate/sets/index.mjs @@ -7,7 +7,7 @@ import { useTranslation } from 'next-i18next' import { PageWrapper, ns as pageNs } from 'shared/components/wrappers/page.mjs' import { ns as authNs } from 'shared/components/wrappers/auth/index.mjs' import { AuthWrapper } from 'shared/components/wrappers/auth/index.mjs' -import { CuratedSets } from 'shared/components/curated-sets.mjs' +import { CuratedSetsList } from 'shared/components/curated-sets.mjs' import { CsetSubmissions } from 'shared/components/submissions/index.mjs' // Translation namespaces used on this page @@ -29,7 +29,7 @@ const CuratorPage = ({ page }) => {

{t('curate:suggestedSets')}

{t('curate:sets')}

- `/curate/sets/${id}`} /> + `/curate/sets/${id}`} /> diff --git a/sites/shared/components/account/sets.mjs b/sites/shared/components/account/sets.mjs index aa6633e5e4e..7931da9b12f 100644 --- a/sites/shared/components/account/sets.mjs +++ b/sites/shared/components/account/sets.mjs @@ -934,112 +934,6 @@ export const UserSetPicker = ({ design, t, href, clickHandler, size = 'lg' }) => ) } -export const CuratedSetPicker = ({ design, href, clickHandler, size }) => { - // Hooks - const backend = useBackend() - const { t, i18n } = useTranslation('sets') - const { language } = i18n - const { control } = useAccount() - - // State - const [curatedSets, setCuratedSets] = useState([]) - const [filter, setFilter] = useState([]) - const [tags, setTags] = useState([]) - - // Effects - useEffect(() => { - const getCuratedSets = async () => { - const result = await backend.getCuratedSets() - if (result.success) { - const all = [] - const allTags = new Set() - for (const set of result.data.curatedSets) { - all.push(set) - for (const tag of set[`tags${capitalize(language)}`]) allTags.add(tag) - } - setCuratedSets(all) - setTags([...allTags]) - } - } - getCuratedSets() - }, [backend, language]) - - const addFilter = (tag) => { - const newFilter = [...filter, tag] - setFilter(newFilter) - } - - const removeFilter = (tag) => { - const newFilter = filter.filter((t) => t !== tag) - setFilter(newFilter) - } - - const applyFilter = () => { - const newList = new Set() - for (const set of curatedSets) { - const setTags = [] - for (const lang of siteConfig.languages) { - const key = `tags${capitalize(lang)}` - if (set[key]) setTags.push(...set[key]) - } - let match = 0 - for (const tag of filter) { - if (setTags.includes(tag)) match++ - } - if (match === filter.length) newList.add(set) - } - - return [...newList] - } - - const list = applyFilter() - - // Need to sort designs by their translated title - const translated = {} - for (const d of list) translated[t(`${d}.t`)] = d - - return ( - <> -

{t('account:curatedSets')}

- - {tags.map((tag) => ( - addFilter(tag)} tag={tag} key={tag}> - {tag} - - ))} -
-
- - - {list.length} / {curatedSets.length} - - -
- {filter.map((tag) => ( - removeFilter(tag)} color="success" hoverColor="error" key={tag}> - {tag} - - ))} -
-
- {orderBy(list, ['name'], ['asc']).map((set) => ( - - ))} -
- - ) -} - export const BookmarkedSetPicker = ({ design, clickHandler, t, size, href }) => { // Hooks const { account, control } = useAccount() diff --git a/sites/shared/components/curated-sets.mjs b/sites/shared/components/curated-sets.mjs index 81d934870ba..0cfef9970c4 100644 --- a/sites/shared/components/curated-sets.mjs +++ b/sites/shared/components/curated-sets.mjs @@ -26,7 +26,15 @@ import { ModalWrapper } from 'shared/components/wrappers/modal.mjs' import Markdown from 'react-markdown' import Timeago from 'react-timeago' import { MeasieVal } from './account/sets.mjs' -import { CameraIcon, UploadIcon, OkIcon, NoIcon } from 'shared/components/icons.mjs' +import { + TrashIcon, + CameraIcon, + UploadIcon, + OkIcon, + NoIcon, + BoolNoIcon, + BoolYesIcon, +} from 'shared/components/icons.mjs' import { Link, PageLink } from 'shared/components/link.mjs' import { StringInput, @@ -63,7 +71,7 @@ const SetLineup = ({ sets = [], href = false, onClick = false }) => ( backgroundPosition: 'center', }, } - if (onClick) props.onClick = () => onClick(set.id) + if (onClick) props.onClick = () => onClick(set) else if (typeof href === 'function') props.href = href(set.id) if (onClick) return @@ -185,8 +193,11 @@ export const CuratedSet = ({ id }) => { ) } +// Picker version +export const CuratedSetPicker = (props) => + // Component for the curated-sets page -export const CuratedSets = ({ href = false }) => { +export const CuratedSets = ({ href = false, clickHandler = false }) => { // Hooks const backend = useBackend() const { setLoadingStatus } = useContext(LoadingStatusContext) @@ -214,7 +225,7 @@ export const CuratedSets = ({ href = false }) => { sets: Object.values(sets), } if (typeof href === 'function') lineupProps.href = href - else lineupProps.onClick = setSelected + else lineupProps.onClick = clickHandler ? clickHandler : (set) => setSelected(set.id) return (
@@ -224,6 +235,131 @@ export const CuratedSets = ({ href = false }) => { ) } +// Component for the maintaining the list of curated-sets +export const CuratedSetsList = ({ href = false }) => { + // Hooks + const { t } = useTranslation(ns) + const backend = useBackend() + const { setLoadingStatus, LoadingProgress } = useContext(LoadingStatusContext) + const [refresh, setRefresh] = useState(0) + + // State + const [sets, setSets] = useState([]) + const [selected, setSelected] = useState(false) + + // Effects + useEffect(() => { + const getSets = async () => { + setLoadingStatus([true, 'contactingBackend']) + const result = await backend.getCuratedSets() + if (result.success) { + const allSets = [] + for (const set of result.data.curatedSets) allSets.push(set) + setSets(allSets) + setLoadingStatus([true, 'status:dataLoaded', true, true]) + } else setLoadingStatus([true, 'status:backendError', true, false]) + } + getSets() + }, [refresh]) + + // Helper var to see how many are selected + const selCount = Object.keys(selected).length + + // 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 === selected.length) setSelected({}) + else { + const newSelected = {} + for (const set of selected) newSelected[set.id] = 1 + setSelected(newSelected) + } + } + + // Helper to delete one or more sets + const removeSelected = async () => { + let i = 0 + for (const key in selected) { + i++ + await backend.removeCuratedMeasurementsSet(key) + setLoadingStatus([ + true, + , + ]) + } + setSelected({}) + setRefresh(refresh + 1) + setLoadingStatus([true, 'nailedIt', true, true]) + } + + const lineupProps = { + sets: Object.values(sets), + } + if (typeof href === 'function') lineupProps.href = href + else lineupProps.onClick = setSelected + + return ( +
+ {selCount ? ( + + ) : null} + + + + + + + + + + + + + {sets.map((set) => ( + + + + + + + + + ))} + +
+ + {t('curate:id')}{t('curate:img')}{t('curate:name')}{t('curate:published')}{t('curate:createdAt')}
+ toggleSelect(set.id)} + /> + {set.id} + + {set.nameEn}{set.published ? : }{set.createdAt}
+ +
+ ) +} + export const EditCuratedSet = ({ id }) => { // Hooks const { account } = useAccount() diff --git a/sites/shared/components/workbench/views/measies/index.mjs b/sites/shared/components/workbench/views/measies/index.mjs index aba05b62da0..9cd7ef2a8ad 100644 --- a/sites/shared/components/workbench/views/measies/index.mjs +++ b/sites/shared/components/workbench/views/measies/index.mjs @@ -9,9 +9,9 @@ import { useTranslation } from 'next-i18next' import { UserSetPicker, BookmarkedSetPicker, - CuratedSetPicker, ns as setsNs, } from 'shared/components/account/sets.mjs' +import { CuratedSetPicker } from 'shared/components/curated-sets.mjs' import { MeasiesEditor } from './editor.mjs' import { Popout } from 'shared/components/popout/index.mjs' import { Accordion } from 'shared/components/accordion.mjs'