Skip to content

Commit

Permalink
#106 Fix loading indication in useEffects
Browse files Browse the repository at this point in the history
  • Loading branch information
blms committed Dec 28, 2020
1 parent 7bc20fe commit 887470c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,32 @@ const DashboardAnnotationList = ({
if (session && (session.user.groups || session.user.id)) {
if (key === 'shared') {
if (session.user.groups && session.user.groups.length > 0) {
const annos = await getSharedAnnotations(session.user.groups, limit);
setAnnotations(annos);
await getSharedAnnotations(session.user.groups, limit)
.then((annos) => {
setAnnotations(annos);
setListLoading(false);
})
.catch((err) => {
setAlerts([...alerts, { text: err.message, variant: 'danger' }]);
setListLoading(false);
});
} else {
setAnnotations([]);
}
} else if (key === 'mine') {
const annos = await getOwnAnnotations(session.user.id, limit);
setAnnotations(annos);
await getOwnAnnotations(session.user.id, limit)
.then((annos) => {
setAnnotations(annos);
setListLoading(false);
})
.catch((err) => {
setAlerts([...alerts, { text: err.message, variant: 'danger' }]);
setListLoading(false);
});
}
}
}
fetchData()
.then(setListLoading(false))
.catch((err) => {
setAlerts([...alerts, { text: err.message, variant: 'danger' }]);
setListLoading(false);
});
fetchData();
}, [key]);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,29 @@ const DashboardDocumentList = ({
useEffect(() => {
async function fetchData() {
if (session && (session.user.groups || session.user.id)) {
setListLoading(true);
if (key === 'shared') {
const docs = await getSharedDocumentsByGroup(session.user.groups, 7);
setDocuments(docs);
await getSharedDocumentsByGroup(session.user.groups, 7)
.then((docs) => {
setDocuments(docs);
setListLoading(false);
}).catch((err) => {
setAlerts([...alerts, { text: err.message, variant: 'danger' }]);
setListLoading(false);
});
} else if (key === 'mine') {
const docs = await getDocumentsByUser(session.user.id, 7);
setDocuments(docs);
await getDocumentsByUser(session.user.id, 7)
.then((docs) => {
setDocuments(docs);
setListLoading(false);
}).catch((err) => {
setAlerts([...alerts, { text: err.message, variant: 'danger' }]);
setListLoading(false);
});
}
}
}
fetchData()
.then(setListLoading(false))
.catch((err) => {
setAlerts([...alerts, { text: err.message, variant: 'danger' }]);
setListLoading(false);
});
fetchData();
}, [key]);


Expand Down Expand Up @@ -69,9 +77,6 @@ const DashboardDocumentList = ({
style={{ justifyContent: 'flex-end', float: 'right', marginTop: '-2rem' }}
activeKey={key}
onSelect={(k) => {
if (k !== key) {
setListLoading(true);
}
setKey(k);
}}
>
Expand Down Expand Up @@ -116,7 +121,7 @@ const DashboardDocumentList = ({
</ListGroup.Item>
</ListGroup>
)}
{!listLoading && (!documents || documents.length === 0) && (
{!listLoading && documents && documents.length === 0 && (
<Card.Body>
{`You have no ${key === 'shared' ? key : 'created'} documents.`}
</Card.Body>
Expand Down
29 changes: 19 additions & 10 deletions src/pages/documents/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,29 @@ const DocumentsIndex = ({
async function fetchData() {
if (session && (session.user.groups || session.user.id)) {
if (key === 'shared') {
const docs = await getSharedDocumentsByGroup(session.user.groups);
setDocuments(docs);
await getSharedDocumentsByGroup(session.user.groups)
.then((docs) => {
setDocuments(docs);
setListLoading(false);
})
.catch((err) => {
setAlerts([...alerts, { text: err.message, variant: 'danger' }]);
setListLoading(false);
});
} else if (key === 'mine') {
const docs = await getDocumentsByUser(session.user.id);
setDocuments(docs);
await getDocumentsByUser(session.user.id)
.then((docs) => {
setDocuments(docs);
setListLoading(false);
})
.catch((err) => {
setAlerts([...alerts, { text: err.message, variant: 'danger' }]);
setListLoading(false);
});
}
}
}
fetchData()
.then(setListLoading(false))
.catch((err) => {
setAlerts([{ text: err.message, variant: 'danger' }]);
setListLoading(false);
});
fetchData();
}, [session, key]);

return (
Expand Down

0 comments on commit 887470c

Please sign in to comment.