Skip to content

Commit

Permalink
#106 Loading handlers for dashboard and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
blms committed Dec 28, 2020
1 parent 2acf09a commit cab35c4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,23 @@ const DashboardAnnotationList = ({
if (session && (session.user.groups || session.user.id)) {
if (key === 'shared') {
if (session.user.groups && session.user.groups.length > 0) {
setAnnotations(
await getSharedAnnotations(session.user.groups, limit)
.then(setListLoading(false))
.catch((err) => setAlerts([...alerts, { text: err.message, variant: 'danger' }])),
);
const annos = await getSharedAnnotations(session.user.groups, limit);
setAnnotations(annos);
} else {
setAnnotations([]);
}
} else if (key === 'mine') {
setAnnotations(
await getOwnAnnotations(session.user.id, limit)
.then(setListLoading(false))
.catch((err) => setAlerts([...alerts, { text: err.message, variant: 'danger' }])),
);
const annos = await getOwnAnnotations(session.user.id, limit);
setAnnotations(annos);
}
}
}
fetchData();
fetchData()
.then(setListLoading(false))
.catch((err) => {
setAlerts([...alerts, { text: err.message, variant: 'danger' }]);
setListLoading(false);
});
}, [key]);


Expand Down Expand Up @@ -81,7 +80,12 @@ const DashboardAnnotationList = ({
transition={false}
style={{ justifyContent: 'flex-end', float: 'right', marginTop: '-2rem' }}
activeKey={key}
onSelect={(k) => setKey(k)}
onSelect={(k) => {
if (k !== key) {
setListLoading(true);
}
setKey(k);
}}
>
<Tab eventKey="shared" title="Shared" />
<Tab eventKey="mine" title="Mine" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,20 @@ const DashboardDocumentList = ({
async function fetchData() {
if (session && (session.user.groups || session.user.id)) {
if (key === 'shared') {
setDocuments(
await getSharedDocumentsByGroup(session.user.groups, 10)
.then(setListLoading(false))
.catch((err) => setAlerts([...alerts, { text: err.message, variant: 'danger' }])),
);
const docs = await getSharedDocumentsByGroup(session.user.groups, 10);
setDocuments(docs);
} else if (key === 'mine') {
setDocuments(
await getDocumentsByUser(session.user.id, 10)
.then(setListLoading(false))
.catch((err) => setAlerts([...alerts, { text: err.message, variant: 'danger' }])),
);
const docs = await getDocumentsByUser(session.user.id, 10);
setDocuments(docs);
}
}
}
fetchData();
fetchData()
.then(setListLoading(false))
.catch((err) => {
setAlerts([...alerts, { text: err.message, variant: 'danger' }]);
setListLoading(false);
});
}, [key]);


Expand Down Expand Up @@ -66,7 +65,12 @@ const DashboardDocumentList = ({
transition={false}
style={{ justifyContent: 'flex-end', float: 'right', marginTop: '-2rem' }}
activeKey={key}
onSelect={(k) => setKey(k)}
onSelect={(k) => {
if (k !== key) {
setListLoading(true);
}
setKey(k);
}}
>
<Tab eventKey="shared" title="Shared" />
<Tab eventKey="mine" title="Mine" />
Expand Down
3 changes: 3 additions & 0 deletions src/components/DocumentList/DocumentList.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const DocumentList = ({
documents,
setDocuments,
loading,
setLoading,
userId,
alerts,
setAlerts,
Expand Down Expand Up @@ -163,6 +164,7 @@ const DocumentList = ({
handleCloseModal={handleCloseModal}
show={showModal === document._id}
onClick={(event) => {
setLoading(true);
event.target.setAttribute('disabled', 'true');
deleteDocumentById(document._id).then(() => {
setDocuments(documents.filter((d) => d._id !== document._id));
Expand All @@ -172,6 +174,7 @@ const DocumentList = ({
}]);
}).catch((err) => {
setAlerts([...alerts, { text: err.message, variant: 'danger' }]);
setLoading(false);
});
handleCloseModal();
}}
Expand Down
33 changes: 19 additions & 14 deletions src/pages/documents/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,25 @@ const DocumentsIndex = ({
async function fetchData() {
if (session && (session.user.groups || session.user.id)) {
if (key === 'shared') {
setDocuments(
await getSharedDocumentsByGroup(session.user.groups)
.then(setListLoading(false))
.catch((err) => setAlerts([{ text: err.message, variant: 'danger' }])),
);
const docs = await getSharedDocumentsByGroup(session.user.groups);
setDocuments(docs);
} else if (key === 'mine') {
setDocuments(
await getDocumentsByUser(session.user.id)
.then(setListLoading(false))
.catch((err) => setAlerts([{ text: err.message, variant: 'danger' }])),
);
const docs = await getDocumentsByUser(session.user.id);
setDocuments(docs);
}
}
}
fetchData();
fetchData()
.then(setListLoading(false))
.catch((err) => {
setAlerts([{ text: err.message, variant: 'danger' }]);
setListLoading(false);
});
}, [session, key]);

return (
<Layout alerts={alerts} type="document" statefulSession={statefulSession}>
{loading && !session && (
{((loading && !session) || listLoading) && (
<Card>
<Card.Header>
<Card.Title>
Expand All @@ -54,7 +53,7 @@ const DocumentsIndex = ({
</Card.Body>
</Card>
)}
{!loading && session && (
{!loading && session && !listLoading && (
<Card>
<Card.Header>
<Card.Title>
Expand All @@ -67,7 +66,12 @@ const DocumentsIndex = ({
id="document-list-tabs"
style={{ justifyContent: 'flex-end', marginBottom: '0', marginRight: '0' }}
activeKey={key}
onSelect={(k) => setKey(k)}
onSelect={(k) => {
if (k !== key) {
setListLoading(true);
}
setKey(k);
}}
>
<Tab eventKey="shared" title="Shared" />
<Tab eventKey="mine" title="Mine" />
Expand All @@ -79,6 +83,7 @@ const DocumentsIndex = ({
alerts={alerts}
setAlerts={setAlerts}
loading={listLoading}
setLoading={setListLoading}
userId={session.user.id}
/>
)}
Expand Down

0 comments on commit cab35c4

Please sign in to comment.