From bcf6b8145a1867bdb79e95acd5d8e773693244aa Mon Sep 17 00:00:00 2001 From: Joshua Mbogo <55801923+mbogo-mit@users.noreply.github.com> Date: Thu, 23 Nov 2023 21:38:13 -0500 Subject: [PATCH] adding proper count by group to dashboard --- .../DashboardChannels/AnnotationsChannel.js | 53 ++++++++++++++++--- src/pages/api/annotations.js | 48 +++++++++++++++-- src/utils/annotationUtil.js | 5 +- 3 files changed, 94 insertions(+), 12 deletions(-) diff --git a/src/components/DashboardChannels/AnnotationsChannel.js b/src/components/DashboardChannels/AnnotationsChannel.js index 3c66d8b7..d44e3d61 100644 --- a/src/components/DashboardChannels/AnnotationsChannel.js +++ b/src/components/DashboardChannels/AnnotationsChannel.js @@ -192,7 +192,7 @@ export default function AnnotationsChannel({ } const byWithGroupId = (anno) => ( - getDocumentFromDocuments()?.version === 4 ? byGroupFilterMatch(anno?.creator?.withGroupId ? [anno?.creator?.withGroupId] : [], [selectedGroupId]) : true + getDocumentFromDocuments()?.version >= 4 ? byGroupFilterMatch(anno?.creator?.withGroupId ? [anno?.creator?.withGroupId] : [], [selectedGroupId]) : true ); const loadComponent = loadMore @@ -256,11 +256,17 @@ export default function AnnotationsChannel({ byDateCreated: , }; + const newDocumentVersion = getDocumentFromDocuments()?.version >= 4; + const buttons = [ { text: 'Mine', textWidth: 40, - count: (annotations[slug]?.mine || [])?.filter(byWithGroupId).length || 0, + count: newDocumentVersion && annotations[slug]?.countByGroup && annotations[slug]?.countByGroup[selectedGroupId]?.mine !== undefined + ? annotations[slug]?.countByGroup[selectedGroupId]?.mine + : (annotations[slug]?.countByPermissions === undefined ? 0 : annotations[slug]?.countByPermissions.mine), + + // count: (annotations[slug]?.mine || [])?.filter(byWithGroupId).length || 0, // count: annotations[slug]?.countByPermissions === undefined // ? 0 // : annotations[slug]?.countByPermissions.mine, @@ -271,7 +277,10 @@ export default function AnnotationsChannel({ { text: 'Shared with group(s)', textWidth: 145, - count: (annotations[slug]?.shared || [])?.filter(byWithGroupId).length || 0, + count: newDocumentVersion && annotations[slug]?.countByGroup && annotations[slug]?.countByGroup[selectedGroupId]?.shared !== undefined + ? annotations[slug]?.countByGroup[selectedGroupId]?.shared + : (annotations[slug]?.countByPermissions === undefined ? 0 : annotations[slug]?.countByPermissions.shared), + // count: (annotations[slug]?.shared || [])?.filter(byWithGroupId).length || 0, // count: annotations[slug]?.countByPermissions === undefined // ? 0 // : annotations[slug]?.countByPermissions.shared, @@ -282,7 +291,10 @@ export default function AnnotationsChannel({ { text: 'Shared with me', textWidth: 115, - count: (annotations[slug] && (annotations[slug]['shared-with-me'] || [])?.filter(byWithGroupId).length) || 0, + count: newDocumentVersion && annotations[slug]?.countByGroup && annotations[slug]?.countByGroup[selectedGroupId]?.['shared-with-me'] !== undefined + ? annotations[slug]?.countByGroup[selectedGroupId]?.['shared-with-me'] + : (annotations[slug]?.countByPermissions === undefined ? 0 : annotations[slug]?.countByPermissions['shared-with-me']), + // count: (annotations[slug] && (annotations[slug]['shared-with-me'] || [])?.filter(byWithGroupId).length) || 0, // count: annotations[slug]?.countByPermissions === undefined // ? 0 // : annotations[slug]?.countByPermissions['shared-with-me'], @@ -295,6 +307,7 @@ export default function AnnotationsChannel({ const updateAnnotations = (annos) => { const { countByPermissions, + countByGroup, page = 1, mine = [], shared = [], @@ -306,13 +319,34 @@ export default function AnnotationsChannel({ annotations[slug].countByPermissions.mine = countByPermissions.mine; } if (countByPermissions?.shared) { - annotations[slug].countByPermissions.mine = countByPermissions.mine; + annotations[slug].countByPermissions.shared = countByPermissions.shared; } if (countByPermissions['shared-with-me']) { annotations[slug].countByPermissions['shared-with-me'] = countByPermissions['shared-with-me']; } } + if (countByGroup) { + for(const [groupId, counts] of Object.entries(countByGroup)) { + if (annotations[slug].countByGroup[groupId] === undefined) { + annotations[slug].countByGroup[groupId] = {}; + } + + if (counts?.mine !== undefined) { + annotations[slug].countByGroup[groupId].mine = counts.mine; + } + + if (counts?.shared !== undefined) { + annotations[slug].countByGroup[groupId].shared = counts.shared; + } + + if (counts['shared-with-me'] !== undefined) { + annotations[slug].countByGroup[groupId]['shared-with-me'] = counts['shared-with-me']; + } + + } + } + annotations[slug].page = page; annotations[slug].mine.push(...mine); annotations[slug].shared.push(...shared); @@ -320,6 +354,7 @@ export default function AnnotationsChannel({ } else { annotations[slug] = { countByPermissions, + countByGroup, page, mine, shared, @@ -1144,6 +1179,7 @@ export default function AnnotationsChannel({ ); useEffect(() => { + // user refreshing idea space if (refresh && mode === 'is') { fetchAllAnnotations(); @@ -1156,7 +1192,7 @@ export default function AnnotationsChannel({ return; } - if (annotations[slug] !== undefined && !refresh && !loadMore) { + if (annotations[slug] && annotations[slug]?.countByGroup?.[selectedGroupId] && !refresh && !loadMore) { // we already have the annotations for this document so we don't need to reload that // information return; @@ -1185,11 +1221,14 @@ export default function AnnotationsChannel({ userId: session.user.id, userEmail: session.user.email, selectedPermissions, + selectedGroupId, }) .then((data) => { + const a = { page: pageNumber, countByPermissions: data.countByPermissions, + countByGroup: data.countByGroup, }; if (data.annotationsByPermissions) { @@ -1228,7 +1267,7 @@ export default function AnnotationsChannel({ setLastUpdated(new Date()); }); // eslint-disable-next-line react-hooks/exhaustive-deps - }, [slug, refresh, loadMore]); + }, [slug, selectedGroupId, refresh, loadMore]); useEffect(() => { // this keeps the refresh popover text up-to-date diff --git a/src/pages/api/annotations.js b/src/pages/api/annotations.js index eb079db7..426a3987 100644 --- a/src/pages/api/annotations.js +++ b/src/pages/api/annotations.js @@ -2,6 +2,7 @@ import { ObjectID } from 'mongodb'; import { getToken } from 'next-auth/jwt'; import { byPermissionFilter, MAX_NUMBER_OF_ANNOTATIONS_REQUESTED } from '../../utils/annotationUtil'; import { connectToDatabase } from '../../utils/dbUtil'; +import { byGroupFilterMatch } from '../../utils/annotationFilteringUtil'; const secret = process.env.AUTH_SECRET; @@ -18,6 +19,7 @@ const handler = async (req, res) => { userId, userEmail, selectedPermissions, + selectedGroupId, perPage = MAX_NUMBER_OF_ANNOTATIONS_REQUESTED, } = req.body; @@ -35,7 +37,7 @@ const handler = async (req, res) => { const p = page || 1; const skip = p < 0 ? 0 : (p - 1) * perPage; - if (countByPermissions && selectedPermissions) { + if ((countByPermissions || selectedGroupId) && selectedPermissions) { // the user is loading annotations for a specific document with specific permissions for // the dashboard for the first time / refreshing arr = await db @@ -65,15 +67,35 @@ const handler = async (req, res) => { 'shared-with-me': cbp['shared-with-me'].slice(skip, perPage), }; + let countByGroup = undefined; + + if (selectedGroupId) { + // add in the count for this specific selectedGroupId + + const byWithGroupId = (anno) => ( + byGroupFilterMatch(anno?.creator?.withGroupId ? [anno?.creator?.withGroupId] : [], [selectedGroupId]) + ); + + countByGroup = { + [selectedGroupId]: { + mine: cbp.mine.filter(byWithGroupId).length, + shared: cbp.shared.filter(byWithGroupId).length, + 'shared-with-me': cbp['shared-with-me'].filter(byWithGroupId).length, + }, + } + + } + res.status(200).json({ annotations: annotationsByPermissions[selectedPermissions], annotationsByPermissions, count: arr.length, - countByPermissions: { + countByPermissions: countByPermissions ? { mine: cbp.mine.length, shared: cbp.shared.length, 'shared-with-me': cbp['shared-with-me'].length, - }, + } : {}, + countByGroup, }); } else if (selectedPermissions) { // The user is trying to load more annotations for a specific document with specific @@ -102,13 +124,31 @@ const handler = async (req, res) => { annotationsByPermissions[selectedPermissions] = arr.slice(skip, skip + perPage); + let countByGroup = undefined; + + if (selectedGroupId) { + // add in the count for this specific selectedGroupId + + const byWithGroupId = (anno) => ( + byGroupFilterMatch(anno?.creator?.withGroupId ? [anno?.creator?.withGroupId] : [], [selectedGroupId]) + ); + + countByGroup = { + [selectedGroupId]: { + [selectedPermissions]: annotationsByPermissions[selectedPermissions].filter(byWithGroupId).length, + }, + }; + + } + res.status(200).json({ annotations: annotationsByPermissions[selectedPermissions], annotationsByPermissions, + count: arr.length, countByPermissions: { [selectedPermissions]: arr.length, }, - count: arr.length, + countByGroup, }); } else { // user is trying to load annotations by slug for document view. Needs the first packet diff --git a/src/utils/annotationUtil.js b/src/utils/annotationUtil.js index 77ad28c3..b9db1faf 100644 --- a/src/utils/annotationUtil.js +++ b/src/utils/annotationUtil.js @@ -61,6 +61,7 @@ const fetchSharedAnnotationsOnDocument = async ({ userEmail, countByPermissions, selectedPermissions, + selectedGroupId, }) => { const url = `${prefetch ? appendProtocolIfMissing(process.env.SITE) : ''}/api/annotations`; /* @@ -91,6 +92,7 @@ const fetchSharedAnnotationsOnDocument = async ({ userEmail, countByPermissions, selectedPermissions, + selectedGroupId, }; // eslint-disable-next-line no-undef const f = prefetch ? fetch : unfetch; @@ -105,13 +107,14 @@ const fetchSharedAnnotationsOnDocument = async ({ if (res.status === 200) { const response = await res.json(); const { - annotations, count, countByPermissions: cbp, annotationsByPermissions, + annotations, count, countByPermissions: cbp, annotationsByPermissions, countByGroup, } = response; return Promise.resolve({ annotations, count, countByPermissions: cbp, annotationsByPermissions, + countByGroup, }); } if (res.status === 404) { return Promise.resolve([]);