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

#56 Get shared annotations #84

Merged
merged 1 commit into from
Nov 18, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const DashboardAnnotationList = ({
tab,
mode,
}) => {
const [groupState, setGroupState] = useState({});
const [annotationsGroupState, setAnnotationsGroupState] = useState({});
const [key, setKey] = useState(tab || 'mine');
const [listLoading, setListLoading] = useState(true);
const [annotations, setAnnotations] = useState([]);
Expand Down Expand Up @@ -49,14 +49,18 @@ const DashboardAnnotationList = ({
if (annotations) {
const fetchGroupState = async () => {
annotations.map((annotation) => annotation.permissions.groups.map(async (group) => {
if (!groupState[group]) {
setGroupState({ ...groupState, [group]: await getGroupNameById(group) });
if (!annotationsGroupState[group]) {
const name = await getGroupNameById(group);
setAnnotationsGroupState((prevState) => ({
...prevState,
[group]: name,
}));
}
}));
};
fetchGroupState();
}
}, [annotations, groupState]);
}, [annotations]);

return (
<Card>
Expand Down Expand Up @@ -109,15 +113,16 @@ const DashboardAnnotationList = ({
{annotation.target.document.title}
{' ('}
{FirstNameLastInitial(annotation.creator.name)}
)
{') '}
{annotation.permissions.groups
&& annotation.permissions.groups.length > 0 && (
&& annotation.permissions.groups.length > 0
&& annotation.permissions.private === false && (
<Badge
variant="info"
key={annotation.permissions.groups.sort()[0]}
className="mr-2"
className="mL-2"
>
{groupState[annotation.permissions.groups.sort()[0]]}
{annotationsGroupState[annotation.permissions.groups.sort()[0]]}
</Badge>
)}
</small>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const DashboardDocumentList = ({
alerts,
setAlerts,
}) => {
const [groupState, setGroupState] = useState({});
const [documentGroupState, setDocumentGroupState] = useState({});
const [key, setKey] = useState('shared');
const [listLoading, setListLoading] = useState(true);
const [documents, setDocuments] = useState([]);
Expand Down Expand Up @@ -45,14 +45,18 @@ const DashboardDocumentList = ({
if (documents) {
const fetchGroupState = async () => {
documents.map((document) => document.groups.map(async (group) => {
if (!groupState[group]) {
setGroupState({ ...groupState, [group]: await getGroupNameById(group) });
if (!documentGroupState[group]) {
const name = await getGroupNameById(group);
setDocumentGroupState((prevState) => ({
...prevState,
[group]: name,
}));
}
}));
};
fetchGroupState();
}
}, [documents, groupState]);
}, [documents, documentGroupState]);

return (
<Card>
Expand Down Expand Up @@ -87,7 +91,7 @@ const DashboardDocumentList = ({
key={document.groups.sort()[0]}
className="mr-2"
>
{groupState[document.groups.sort()[0]]}
{documentGroupState[document.groups.sort()[0]]}
</Badge>
)}
<>
Expand Down
22 changes: 21 additions & 1 deletion src/pages/api/annotations.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,27 @@ const handler = async (req, res) => {
}
} else res.status(403).end('Unauthorized');
} else if (groupIds) {
res.status(404).end('Not yet implemented');
const { db } = await connectToDatabase();
if (limit) {
const arr = await db
.collection('annotations')
.find({
'permissions.private': false,
'permissions.groups': { $in: groupIds },
})
.limit(parseInt(limit, 10))
.toArray();
res.status(200).json({ annotations: arr });
} else {
const arr = await db
.collection('annotations')
.find({
'permissions.private': false,
'permissions.groups': { $in: groupIds },
})
.toArray();
res.status(200).json({ annotations: arr });
}
} else res.status(400).end('Bad request');
} else res.status(403).end('Invalid or expired token');
} else if (method === 'PATCH') {
Expand Down