Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding proper count by group to dashboard #346

Merged
merged 1 commit into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 46 additions & 7 deletions src/components/DashboardChannels/AnnotationsChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -256,11 +256,17 @@ export default function AnnotationsChannel({
byDateCreated: <CalendarEventFill size={14} style={{ marginRight: 4 }} />,
};

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,
Expand All @@ -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,
Expand All @@ -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'],
Expand All @@ -295,6 +307,7 @@ export default function AnnotationsChannel({
const updateAnnotations = (annos) => {
const {
countByPermissions,
countByGroup,
page = 1,
mine = [],
shared = [],
Expand All @@ -306,20 +319,42 @@ 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);
annotations[slug]['shared-with-me'].push(...sharedWithMe);
} else {
annotations[slug] = {
countByPermissions,
countByGroup,
page,
mine,
shared,
Expand Down Expand Up @@ -1144,6 +1179,7 @@ export default function AnnotationsChannel({
);

useEffect(() => {

// user refreshing idea space
if (refresh && mode === 'is') {
fetchAllAnnotations();
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
48 changes: 44 additions & 4 deletions src/pages/api/annotations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -18,6 +19,7 @@ const handler = async (req, res) => {
userId,
userEmail,
selectedPermissions,
selectedGroupId,
perPage = MAX_NUMBER_OF_ANNOTATIONS_REQUESTED,
} = req.body;

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/utils/annotationUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const fetchSharedAnnotationsOnDocument = async ({
userEmail,
countByPermissions,
selectedPermissions,
selectedGroupId,
}) => {
const url = `${prefetch ? appendProtocolIfMissing(process.env.SITE) : ''}/api/annotations`;
/*
Expand Down Expand Up @@ -91,6 +92,7 @@ const fetchSharedAnnotationsOnDocument = async ({
userEmail,
countByPermissions,
selectedPermissions,
selectedGroupId,
};
// eslint-disable-next-line no-undef
const f = prefetch ? fetch : unfetch;
Expand All @@ -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([]);
Expand Down
Loading