Skip to content

Commit

Permalink
#50 Allow document modify/delete by admin
Browse files Browse the repository at this point in the history
  • Loading branch information
blms committed Oct 20, 2020
1 parent 8234e27 commit d6cb215
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 10 deletions.
55 changes: 52 additions & 3 deletions src/pages/admin/document/[slug].jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ import React, { useState } from 'react';
import Router from 'next/router';
import { useSession } from 'next-auth/client';
import {
Card,
Button, Card, Container,
} from 'react-bootstrap';
import AdminHeader from '../../../components/Admin/AdminHeader';
import LoadingSpinner from '../../../components/LoadingSpinner';
import Layout from '../../../components/Layout';
import { prefetchDocumentBySlug } from '../../../utils/docUtil';
import ConfirmationDialog from '../../../components/ConfirmationDialog';
import { prefetchDocumentBySlug, deleteDocumentById } from '../../../utils/docUtil';
import AdminDocumentTable from '../../../components/Admin/Document/AdminDocumentTable';

const AdminManageDocument = (props) => {
const { document, initAlert } = props;
const [session, loading] = useSession();
const [alerts, setAlerts] = useState(initAlert || []);
const [showModal, setShowModal] = useState(false);
const handleCloseModal = () => setShowModal(false);
const handleShowModal = () => setShowModal(true);
return (
<Layout type="admin" alerts={alerts}>
{loading && (
Expand All @@ -40,7 +44,52 @@ const AdminManageDocument = (props) => {
/>
<Card.Body>
{document && (
<AdminDocumentTable document={document} />
<>
<AdminDocumentTable document={document} alerts={alerts} setAlerts={setAlerts} />
<Container style={{ display: 'flex', justifyContent: 'space-between' }} className="p-0">
<Button
type="button"
href={`/documents/${document.slug}`}
>
View Document
</Button>
<Button
type="button"
variant="warning"
href={`/documents/${document.slug}/edit`}
>
Modify Document
</Button>
<Button
type="button"
variant="danger"
onClick={handleShowModal}
>
Delete Document
</Button>
<ConfirmationDialog
name={document.title}
type="document"
handleCloseModal={handleCloseModal}
show={showModal}
onClick={(event) => {
event.target.setAttribute('disabled', 'true');
deleteDocumentById(document.id).then(() => {
Router.push({
pathname: '/admin',
query: {
alert: { text: 'Document deleted successfully', variant: 'sucess' },
tab: 'documents',
},
});
}).catch((err) => {
setAlerts([...alerts, { text: err.message, variant: 'danger' }]);
});
handleCloseModal();
}}
/>
</Container>
</>
)}
</Card.Body>
</Card>
Expand Down
7 changes: 4 additions & 3 deletions src/pages/admin/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import LoadingSpinner from '../../components/LoadingSpinner';
import Layout from '../../components/Layout';

const AdminView = ({ props }) => {
const { tab } = props;
const { tab, initAlerts } = props;
const [key, setKey] = useState(tab || 'dashboard');
const [session, loading] = useSession();
const [alerts, setAlerts] = useState([]);
const [alerts, setAlerts] = useState(initAlerts || []);
return (
<Layout type="admin" alerts={alerts}>
{loading && (
Expand Down Expand Up @@ -42,9 +42,10 @@ const AdminView = ({ props }) => {
};

AdminView.getInitialProps = async (context) => {
const { tab } = context.query;
const { tab, alert } = context.query;
let props = {};
if (tab) props = { ...props, tab };
if (alert) props = { ...props, initAlerts: [alert] };
return { props };
};

Expand Down
7 changes: 3 additions & 4 deletions src/pages/api/document/slug/[slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ const handler = async (req, res) => {
const userGroups = (userObj.groups && userObj.groups.length > 0)
? userObj.groups.map((group) => group.id)
: [];
const findCondition = { slug: req.query.slug };
if (userObj.role !== 'admin') findCondition.$or = [{ groups: { $in: userGroups } }, { owner: token.id }];
const doc = await db
.collection('documents')
.find({
slug: req.query.slug,
$or: [{ groups: { $in: userGroups } }, { owner: token.id }],
})
.find(findCondition)
.toArray();
if (doc[0]) {
const document = doc[0];
Expand Down

0 comments on commit d6cb215

Please sign in to comment.