Skip to content

Commit

Permalink
#56 Annotations list view
Browse files Browse the repository at this point in the history
  • Loading branch information
blms committed Nov 18, 2020
1 parent c71955d commit 437b4f2
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,27 @@ const DashboardAnnotationList = ({
session,
alerts,
setAlerts,
tab,
mode,
}) => {
const [groupState, setGroupState] = useState({});
const [key, setKey] = useState('mine');
const [key, setKey] = useState(tab || 'mine');
const [listLoading, setListLoading] = useState(true);
const [annotations, setAnnotations] = useState([]);
const limit = mode === 'dashboard' ? 10 : undefined;

useEffect(() => {
async function fetchData() {
if (session && (session.user.groups || session.user.id)) {
if (key === 'shared') {
setAnnotations(
await getSharedAnnotations(session.user.groups, 10)
await getSharedAnnotations(session.user.groups, limit)
.then(setListLoading(false))
.catch((err) => setAlerts([...alerts, { text: err.message, variant: 'danger' }])),
);
} else if (key === 'mine') {
setAnnotations(
await getOwnAnnotations(session.user.id, 10)
await getOwnAnnotations(session.user.id, limit)
.then(setListLoading(false))
.catch((err) => setAlerts([...alerts, { text: err.message, variant: 'danger' }])),
);
Expand All @@ -58,7 +61,14 @@ const DashboardAnnotationList = ({
return (
<Card>
<Card.Header>
<Card.Title><Link href="/annotations">Annotations</Link></Card.Title>
<Card.Title>
{mode === 'dashboard' && (
<Link href="/annotations">Annotations</Link>
)}
{mode === 'list' && (
<>Annotations</>
)}
</Card.Title>
<Tabs
transition={false}
style={{ justifyContent: 'flex-end', float: 'right', marginTop: '-2rem' }}
Expand All @@ -85,15 +95,6 @@ const DashboardAnnotationList = ({
</Link>
</Col>
<Col className="text-right" xl={4}>
{annotation.permissions.groups && annotation.permissions.groups.length > 0 && (
<Badge
variant="info"
key={annotation.permissions.groups.sort()[0]}
className="mr-2"
>
{groupState[annotation.permissions.groups.sort()[0]]}
</Badge>
)}
<small style={{ whiteSpace: 'nowrap' }}>{format(new Date(annotation.created), 'Ppp')}</small>
</Col>
</Row>
Expand All @@ -109,18 +110,30 @@ const DashboardAnnotationList = ({
{' ('}
{FirstNameLastInitial(annotation.creator.name)}
)
{annotation.permissions.groups
&& annotation.permissions.groups.length > 0 && (
<Badge
variant="info"
key={annotation.permissions.groups.sort()[0]}
className="mr-2"
>
{groupState[annotation.permissions.groups.sort()[0]]}
</Badge>
)}
</small>
</Col>
</Row>
</ListGroup.Item>
),
)}
</ListGroup>
{mode === 'dashboard' && (
<Card.Footer style={{ fontWeight: 'bold', borderTop: 0 }} key="all-annotations">
<Link href={`/annotations?tab=${key}`} disabled>
{`See all ${key === 'shared' ? key : 'created'} annotations...`}
</Link>
</Card.Footer>
)}
</>
)}
{!listLoading && (!annotations || annotations.length === 0) && (
Expand Down
5 changes: 5 additions & 0 deletions src/components/SecondNavbar/SecondNavbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ const SecondNavbar = ({
Registration
</Breadcrumb.Item>
)}
{type === 'annotations' && (
<Breadcrumb.Item active>
Annotations
</Breadcrumb.Item>
)}
{title && (
<Breadcrumb.Item active>{title}</Breadcrumb.Item>
)}
Expand Down
50 changes: 50 additions & 0 deletions src/pages/annotations/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React, { useState } from 'react';
import { useSession } from 'next-auth/client';
import { Card } from 'react-bootstrap';
import Layout from '../../components/Layout';
import LoadingSpinner from '../../components/LoadingSpinner';
import DashboardAnnotationList from '../../components/Dashboard/DashboardAnnotationList';

const AnnotationsListVIew = ({
props,
}) => {
const { tab } = props;
const [session, loading] = useSession();
const [alerts, setAlerts] = useState([]);

return (
<Layout alerts={alerts} type="annotations">
{loading && (
<Card>
<Card.Body>
<LoadingSpinner />
</Card.Body>
</Card>
)}
{!session && !loading && (
<Card>
<Card.Header><Card.Title>Not authorized</Card.Title></Card.Header>
<Card.Body>Please log in to use the application.</Card.Body>
</Card>
)}
{session && !loading && (
<DashboardAnnotationList
session={session}
alerts={alerts}
setAlerts={setAlerts}
mode="list"
tab={tab}
/>
)}
</Layout>
);
};

AnnotationsListVIew.getInitialProps = async (context) => {
const { tab } = context.query;
let props = {};
if (tab) props = { tab };
return { props };
};

export default AnnotationsListVIew;
1 change: 1 addition & 0 deletions src/pages/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export default function Home({
session={session}
alerts={alerts}
setAlerts={setAlerts}
mode="dashboard"
/>
</Col>
</Row>
Expand Down

0 comments on commit 437b4f2

Please sign in to comment.