From 69de3b38745e9c918905bb22971016734bee11c7 Mon Sep 17 00:00:00 2001 From: Grzegorz Koperwas Date: Sun, 24 Apr 2022 20:25:05 +0200 Subject: [PATCH 1/2] Poprawki do ekranu PC --- .../editor/src/components/measurementRow.tsx | 17 ++ frontend/editor/src/pages/Pc.tsx | 154 +++++++++++------- 2 files changed, 115 insertions(+), 56 deletions(-) create mode 100644 frontend/editor/src/components/measurementRow.tsx diff --git a/frontend/editor/src/components/measurementRow.tsx b/frontend/editor/src/components/measurementRow.tsx new file mode 100644 index 0000000..8d70576 --- /dev/null +++ b/frontend/editor/src/components/measurementRow.tsx @@ -0,0 +1,17 @@ +import { Typography } from "@mui/material"; + +interface RowProps { + name: string; + children: any; +} + +export function MeasuermentRow(props: RowProps) { + const {name, children} = props; + return <> +
+ {name}: +
+ {children} +
+ +} diff --git a/frontend/editor/src/pages/Pc.tsx b/frontend/editor/src/pages/Pc.tsx index de170e4..30d86b9 100644 --- a/frontend/editor/src/pages/Pc.tsx +++ b/frontend/editor/src/pages/Pc.tsx @@ -1,38 +1,64 @@ -import { useState, useEffect } from "react"; -import { Button, CircularProgress, Typography } from "@mui/material"; +import { useState, useEffect, useReducer } from "react"; +import { Button, Chip, CircularProgress, LinearProgress, Typography } from "@mui/material"; import { useSnackbar } from "notistack"; -import { DataService, FilesService, Measurement, Location } from "../api"; +import { DataService, FilesService, Measurement, Location, FileReference } from "../api"; import { checkFiContainAllFiles } from "../utils/fileUtils"; +import { MeasuermentRow } from "../components/measurementRow"; import _ from 'lodash'; +type CountAction = + | { type: 'add', newFiles: number} + | { type: 'fileuploaded'}; + +interface CountState { + max: number; + now: number; +} + +function countReducer(state: CountState, action: CountAction): CountState { + switch (action.type) { + case 'add': + return {now: state.now + action.newFiles, max: action.newFiles}; + case 'fileuploaded': + return {now: state.now - 1, max: state.max} + default: + throw new Error() + } +} + export default function MobileEdit() { const { enqueueSnackbar } = useSnackbar(); const [measurements, setMeasurements] = useState(); + const [uploadingFilesCount, dispatch] = useReducer(countReducer, {now: 0, max: 0}); function showLocalization(localization: Location) { window.open(`https://www.google.com/maps/place/${localization.latitude} ${localization.longitude}`, "_blank"); } - function send(id: number) { + async function send(id: number) { const input = document.getElementById(`${id}`) as HTMLInputElement; if(!input || !input.files) { return; } - + dispatch({type: 'add', newFiles: input.files.length}) for (const file of input.files) { let body = { uploaded_file: file } - FilesService.uploadNewFileApiFilesPost(id, body).then(() => { + try { + await FilesService.uploadNewFileApiFilesPost(id, body) enqueueSnackbar("The file was added", { variant: "success", }); - }).catch(() => { + } + catch { enqueueSnackbar(`Ops! We have some error with file upload check your internet connection or login again`, { variant: "error", }); - }); + } + dispatch({type: 'fileuploaded'}) } + await fetchData() } async function fetchData() { @@ -44,6 +70,24 @@ export default function MobileEdit() { setMeasurements(measures); } + async function deleteFile(file: FileReference, e: Event) { + e.preventDefault() + if (confirm(`Do you really want to delete "${file.original_name}"?`)) { + try { + await FilesService.deleteFileApiFilesDeleteIdGet(file.file_id) + enqueueSnackbar(`Deleted ${file.original_name}`, { + variant: "success", + }); + } + catch { + enqueueSnackbar(`Ops! Grzez failed to delete ${file.original_name}`, { + variant: "error", + }); + } + await fetchData() + } + } + useEffect(() => { fetchData(); }, []); @@ -61,54 +105,52 @@ export default function MobileEdit() { return (
-
- title: -
- {measurement.title} -
-
- description: -
- {measurement.description} -
-
- notes: -
- {measurement.notes} -
-
- tags: -
- {measurement.tags.join(", ")} -
-
- laeq: -
- {measurement.laeq} -
-
- data: -
- {measurement.location.time} -
-
showLocalization(measurement.location)} className='measurement-row cursor-pointer'> - localization: -
- - {measurement.location.latitude} {measurement.location.longitude} -
-
- files: -
- {measurement.files.map((file, index, arr) => ( - - {file.mime}{index < arr.length - 1 ? ", " : ""} - - ))} -
-
- -
+ + {measurement.title} + + + {measurement.description} + + + {measurement.notes} + + + {measurement.tags.map( + (tag, index) => )} + + + {measurement.laeq} + + + {new Date(Date.parse(measurement.location.time)).toLocaleString()} + + + showLocalization(measurement.location)}/> + + + {measurement.files.map((file) => deleteFile(file, e)} + clickable + /> + )} + + {uploadingFilesCount.now > 0 ? + <> + + + : <> +
+ +
+ }