Skip to content

Commit

Permalink
Merge pull request #114 from mitaai/blms/ui-tweaks
Browse files Browse the repository at this point in the history
#106 Fix loading indicators and auto-remove alerts
  • Loading branch information
blms committed Dec 28, 2020
2 parents 88ac709 + 887470c commit affef45
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 37 deletions.
10 changes: 9 additions & 1 deletion src/components/Alerts/Alerts.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ function Alerts({ alerts }) {
for (let i = 0; i < alerts.length; i += 1) {
if (!Object.keys(show).includes(`alert[${i}]`)) {
alertsToAdd[`alert[${i}]`] = true;
setTimeout(() => {
alertsToAdd[`alert[${i}]`] = false;
setShow(
(prevState) => ({ ...prevState, ...alertsToAdd }),
);
}, 2000);
}
}
setShow((prevState) => ({ ...prevState, ...alertsToAdd }));
setShow(
(prevState) => ({ ...prevState, ...alertsToAdd }),
);
}, [alerts]);

return (
Expand Down
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, 10);
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, 10);
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const DashboardGroupList = ({
const [groups, setGroups] = useState([]);

useEffect(() => {
if (session && !deepEqual(session.user.groups.slice(0, 10), groups)) {
setGroups(session.user.groups.slice(0, 10));
if (session && !deepEqual(session.user.groups.slice(0, 7), groups)) {
setGroups(session.user.groups.slice(0, 7));
}
}, [session]);

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 affef45

Please sign in to comment.