Skip to content

Commit

Permalink
create and delete report
Browse files Browse the repository at this point in the history
  • Loading branch information
BehshadBabai committed Dec 5, 2023
1 parent 473c9c5 commit 916c920
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 55 deletions.
21 changes: 0 additions & 21 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,27 +66,6 @@ const App = () => {
});
}, []);

React.useEffect(() => {
async function effect() {
const dbReports = await getAllReports();
const fullReports = await Promise.all(
dbReports.map(async (report) => {
const id = report.id;
const imageUrls = await getFileUrls(id, 'images');
const videoUrl = (await getFileUrls(id, 'video'))?.[0];
return {
...report,
id,
imageUrls,
videoUrl
};
})
);
dispatch(changeReports(fullReports));
}
effect();
}, []);

return (
<BrowserRouter>
<ConfigProvider
Expand Down
3 changes: 2 additions & 1 deletion src/Components/ReportParts/GridView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { FileTextOutlined } from '@ant-design/icons';
import { colors } from '../../Utilities/Constants';
import { useNavigate } from 'react-router-dom';
import { deleteDocument, getFullDate } from '../../Utilities/Util';
import { deleteDocument, deleteFiles, getFullDate } from '../../Utilities/Util';
import { useAppDispatch } from '../../Redux/hooks';
import { changeReports } from '../../Redux/features/report/report-slice';

Expand Down Expand Up @@ -80,6 +80,7 @@ const GridView: React.FC<ViewProps> = ({ reports }) => {
<Popconfirm
onConfirm={async () => {
await deleteDocument('reports', report.id);
await deleteFiles(report);
dispatch(
changeReports(
reports.filter((rep) => rep.id !== report.id)
Expand Down
16 changes: 4 additions & 12 deletions src/Components/ReportParts/ListView.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
import { FileTextOutlined } from '@ant-design/icons';
import React from 'react';
import {
Avatar,
List,
Image,
Typography,
Space,
Button,
Row,
Col,
Popconfirm
} from 'antd';
import { Avatar, List, Image, Typography, Space, Popconfirm } from 'antd';
import { ViewProps } from '../../Utilities/types';
import { colors } from '../../Utilities/Constants';
import { deleteDocument, getFullDate } from '../../Utilities/Util';
import { deleteDocument, deleteFiles, getFullDate } from '../../Utilities/Util';
import useScreenSize from '../../Hooks/useScreenSize';
import { changeReports } from '../../Redux/features/report/report-slice';
import { useAppDispatch } from '../../Redux/hooks';
Expand Down Expand Up @@ -43,9 +33,11 @@ const ListView: React.FC<ViewProps> = ({ reports }) => {
<Popconfirm
onConfirm={async () => {
await deleteDocument('reports', item.id);
await deleteFiles(item);
dispatch(
changeReports(reports.filter((rep) => rep.id !== item.id))
);
window.location.reload();
}}
title={'Delete Report'}
description='Are you sure you want to delete this report?'
Expand Down
198 changes: 196 additions & 2 deletions src/Pages/CreateReport.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,210 @@
import React from 'react';
import { useAppSelector } from '../Redux/hooks';
import { useNavigate } from 'react-router-dom';
import {
Button,
Col,
DatePicker,
Form,
Input,
Row,
TimePicker,
Upload,
notification
} from 'antd';
import TextArea from 'antd/es/input/TextArea';
import { UploadOutlined } from '@ant-design/icons';
import useScreenSize from '../Hooks/useScreenSize';
import {
addDocWithoutId,
openNotificationWithIcon,
uploadFiles
} from '../Utilities/Util';
import { useForm } from 'antd/es/form/Form';

const CreateReport: React.FC = () => {
const loggedIn = useAppSelector((state) => state.account.loggedIn);
const account = useAppSelector((state) => state.account);
const scSize = useScreenSize();
const { loggedIn, id, info } = account;
const name = info.name;
const [submitLoading, setSubmitLoading] = React.useState(false);
const [api, contextHolder] = notification.useNotification();
const [form] = useForm();
const navigate = useNavigate();

React.useEffect(() => {
if (!loggedIn) {
navigate('/');
}
}, []);
return <>Create Report</>;

const createReport = async (values) => {
try {
const { title, description, videos, images } = values;
const date = values.date.format('YYYY-MM-DD');
const time = values.time.format('HH:mm');
setSubmitLoading(true);
const reportRef = await addDocWithoutId('reports', {
title,
description,
date,
time,
reply: '',
creatorId: id,
creatorName: name
});
const reportId = reportRef.id;
const videoFiles = videos.fileList.map((rcFile) => rcFile.originFileObj);
await uploadFiles(videoFiles, reportId, 'video');
const imagesFiles = images.fileList.map((rcFile) => rcFile.originFileObj);
await uploadFiles(imagesFiles, reportId, 'images');
openNotificationWithIcon('success', api, 'created', 'report');
setTimeout(() => {
form.resetFields();
navigate('/reports');
}, 500);
} catch (error) {
api.error({
message: 'Failed',
description: 'Failed to Create Your Report',
placement: 'top'
});
} finally {
setTimeout(() => {
setSubmitLoading(false);
}, 500);
}
};

return (
<>
{contextHolder}
<Form size='large' onFinish={createReport} form={form}>
<Row justify={'space-between'}>
<Col xs={24} md={10}>
<Form.Item
name={'title'}
rules={[
{
required: true,
message: 'Please input report title!'
}
]}
>
<Input placeholder='Report Title' />
</Form.Item>
</Col>
<Col xs={24} md={14}>
<Row
justify={scSize.width > 767 ? 'end' : 'start'}
gutter={[10, 0]}
>
<Col>
<Form.Item
name={'date'}
rules={[
{
required: true,
message: 'Please input report date!'
}
]}
>
<DatePicker />
</Form.Item>
</Col>
<Col>
<Form.Item
name={'time'}
rules={[
{
required: true,
message: 'Please input report time!'
}
]}
>
<TimePicker format='HH:mm' minuteStep={5} />
</Form.Item>
</Col>
</Row>
</Col>
</Row>
<Form.Item
name={'description'}
rules={[
{
required: true,
message: 'Please input report description!'
}
]}
>
<TextArea
placeholder='Report Description'
style={{ height: '225px' }}
/>
</Form.Item>
<Form.Item
name={'videos'}
rules={[
{
required: true,
message: 'Please upload a video!',
validator: async (_rule, value) => {
if (value.fileList.length < 1) {
throw new Error('Something wrong!');
}
}
}
]}
>
<Upload
listType='picture'
maxCount={1}
accept='video/mp4,video/x-m4v,video/*'
customRequest={({ onSuccess }) => {
onSuccess('ok');
}}
>
<Button icon={<UploadOutlined />}>Upload Video</Button>
</Upload>
</Form.Item>
<Form.Item
name={'images'}
rules={[
{
required: true,
message: 'Please upload a minimum of 1 image!',
validator: async (_rule, value) => {
if (value.fileList.length < 1) {
throw new Error('Something wrong!');
}
}
}
]}
>
<Upload
listType='picture'
multiple
accept='image/png, image/jpeg, image/heic'
customRequest={({ onSuccess }) => {
onSuccess('ok');
}}
>
<Button icon={<UploadOutlined />}>Upload Images</Button>
</Upload>
</Form.Item>
<Form.Item>
<Button
type='primary'
htmlType='submit'
className='login-form-button'
loading={submitLoading}
>
Create Report
</Button>
</Form.Item>
</Form>
</>
);
};

export default CreateReport;
50 changes: 32 additions & 18 deletions src/Pages/Reports.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,58 @@ import {
TableOutlined,
UnorderedListOutlined
} from '@ant-design/icons';
import { addOrEditDoc, reduxToDbAccount } from '../Utilities/Util';
import {
addOrEditDoc,
getAllReports,
getFileUrls,
reduxToDbAccount
} from '../Utilities/Util';
import GridView from '../Components/ReportParts/GridView';
import ListView from '../Components/ReportParts/ListView';
import Loading from '../Components/Loading';
import { changeReports } from '../Redux/features/report/report-slice';

const Reports: React.FC = () => {
const account = useAppSelector((state) => state.account);
const reports = useAppSelector((state) => state.report.reports);
const dispatch = useAppDispatch();
const { loggedIn, view } = account;
const navigate = useNavigate();
const [selectLoading, setSelectLoading] = React.useState(false);
const [pageLoading, setPageLoading] = React.useState(true);
const [reports, setReports] = React.useState([]);
const [reportsToShow, setReportsToShow] = React.useState([]);
const [initialReports, setInitialReports] = React.useState([]);

React.useEffect(() => {
if (!loggedIn) {
navigate('/');
}
}, []);

React.useEffect(() => {
async function effect() {
const dbReports = await getAllReports();
const fullReports = await Promise.all(
dbReports.map(async (report) => {
const id = report.id;
const imageUrls = await getFileUrls(id, 'images');
const videoUrl = (await getFileUrls(id, 'video'))?.[0];
return {
...report,
id,
imageUrls,
videoUrl
};
})
);
dispatch(changeReports(fullReports));
setReports(fullReports);
setPageLoading(false);
}
effect();
}, []);

React.useEffect(() => {
if (reports) {
if (initialReports.length === 0) {
setInitialReports(
reports.filter((report) => {
if (
report.creatorId === account.id ||
report.creatorId === account.connectionId
) {
return true;
}
return false;
})
);
}
setReportsToShow(
reports.filter((report) => {
if (
Expand All @@ -59,7 +74,6 @@ const Reports: React.FC = () => {
return false;
})
);
setPageLoading(false);
}
}, [reports]);

Expand All @@ -82,7 +96,7 @@ const Reports: React.FC = () => {
);
setReportsToShow(newReportList);
} else {
setReportsToShow(initialReports);
setReportsToShow(reportsToShow);
}
}}
/>
Expand Down
Loading

0 comments on commit 916c920

Please sign in to comment.