From 2082dc1866226fbd88b9867eacc6d6fd5270d88b Mon Sep 17 00:00:00 2001 From: franciszek Date: Fri, 15 Jan 2021 22:59:06 +0100 Subject: [PATCH] Working form and selecting photos --- package-lock.json | 43 +++++++++++++- package.json | 2 + src/app.tsx | 142 ++++++++++++++++++++++++++++++++++++++------ src/fileReducer.ts | 3 + src/formReducer.ts | 60 +++++++++++++++++++ src/http.ts | 18 ++++++ src/location.ts | 22 +++++++ src/styles/app.scss | 30 +++++++++- 8 files changed, 296 insertions(+), 24 deletions(-) create mode 100644 src/formReducer.ts create mode 100644 src/http.ts create mode 100644 src/location.ts diff --git a/package-lock.json b/package-lock.json index 56c1f88..7087af4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1061,6 +1061,12 @@ "@types/node": "*" } }, + "@types/caseless": { + "version": "0.12.2", + "resolved": "https://registry.npmjs.org/@types/caseless/-/caseless-0.12.2.tgz", + "integrity": "sha512-6ckxMjBBD8URvjB6J3NcnuAn5Pkl7t3TizAg+xdlzzQGSPSmBcXf8KoIH0ua/i+tio+ZRUHEXp0HEmvaR4kt0w==", + "dev": true + }, "@types/connect": { "version": "3.4.34", "resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.34.tgz", @@ -1198,9 +1204,9 @@ "integrity": "sha512-Jus9s4CDbqwocc5pOAnh8ShfrnMcPHuJYzVcSUU7lrh8Ni5HuIqX3oilL86p3dlTrk0LzHRCgA/GQ7uNCw6l2Q==" }, "@types/node": { - "version": "14.14.19", - "resolved": "https://registry.npmjs.org/@types/node/-/node-14.14.19.tgz", - "integrity": "sha512-4nhBPStMK04rruRVtVc6cDqhu7S9GZai0fpXgPXrFpcPX6Xul8xnrjSdGB4KPBVYG/R5+fXWdCM8qBoiULWGPQ==" + "version": "14.14.20", + "resolved": "https://registry.npmjs.org/@types/node/-/node-14.14.20.tgz", + "integrity": "sha512-Y93R97Ouif9JEOWPIUyU+eyIdyRqQR0I8Ez1dzku4hDx34NWh4HbtIc3WNzwB1Y9ULvNGeu5B8h8bVL5cAk4/A==" }, "@types/prop-types": { "version": "15.7.3", @@ -1239,6 +1245,31 @@ "@types/react": "*" } }, + "@types/request": { + "version": "2.48.5", + "resolved": "https://registry.npmjs.org/@types/request/-/request-2.48.5.tgz", + "integrity": "sha512-/LO7xRVnL3DxJ1WkPGDQrp4VTV1reX9RkC85mJ+Qzykj2Bdw+mG15aAfDahc76HtknjzE16SX/Yddn6MxVbmGQ==", + "dev": true, + "requires": { + "@types/caseless": "*", + "@types/node": "*", + "@types/tough-cookie": "*", + "form-data": "^2.5.0" + }, + "dependencies": { + "form-data": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-2.5.1.tgz", + "integrity": "sha512-m21N3WOmEEURgk6B9GLOE4RuWOFf28Lhh9qGYeNlGq4VDXUlJy2th2slBNU8Gp8EzloYZOibZJ7t5ecIrFSjVA==", + "dev": true, + "requires": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.6", + "mime-types": "^2.1.12" + } + } + } + }, "@types/serve-static": { "version": "1.13.8", "resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.13.8.tgz", @@ -1248,6 +1279,12 @@ "@types/node": "*" } }, + "@types/tough-cookie": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/@types/tough-cookie/-/tough-cookie-4.0.0.tgz", + "integrity": "sha512-I99sngh224D0M7XgW1s120zxCt3VYQ3IQsuw3P3jbq5GG4yc79+ZjyKznyOGIQrflfylLgcfekeZW/vk0yng6A==", + "dev": true + }, "abab": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/abab/-/abab-2.0.5.tgz", diff --git a/package.json b/package.json index cb6fe60..1fea6b2 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,8 @@ }, "devDependencies": { "@types/leaflet": "^1.5.19", + "@types/node": "^14.14.20", + "@types/request": "^2.48.5", "sass": "^1.32.0", "typescript": "^4.1.3" } diff --git a/src/app.tsx b/src/app.tsx index 84b5190..2fe1b35 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -5,32 +5,39 @@ import React, { FC, ChangeEvent, FormEvent, + useEffect, } from "react"; import * as ReactDOM from "react-dom"; import { offenses } from "./offenses"; //Importing fileReducer to use in useReducer hook, and File interface to use in looping over array of files -import { fileReducer, File } from "./fileReducer"; +import { fileReducer, filesInitialState, File } from "./fileReducer"; +import { formReducer, fromInitialState } from "./formReducer"; import "regenerator-runtime/runtime"; import { MapContainer, TileLayer, Marker, useMapEvents } from "react-leaflet"; import "./styles/reset.css"; import "./styles/app.scss"; +import { getAddress } from "./location"; const App: FC = () => { //This reducer handles adding and selecting files. - //I'm passing in initial state "{files: []}" which can be changed by using dispatch function with object contaning right type and payload. + //I'm passing in initial state "{files: []}" from fileReducer file. + //State can be changed by using dispatch function with object contaning right type and payload. //Example: dispatch({type: "ADD_FILE", payload: {img: thisIsImgURL, selected: false}) //State can be accsesed by using fileState object //Example: fileState.files //Reducer it self is imported from fileReducer.ts in src/ dir - const [fileState, dispatch] = useReducer(fileReducer, { files: [] }); + const [fileState, fileDispatch] = useReducer(fileReducer, filesInitialState); + + const [formState, formDispatch] = useReducer(formReducer, fromInitialState); //This hook handles pining location on the map - const [mapPin, setMapPin] = useState([52, 16]); + const [mapPin, setMapPin] = useState({ lat: 52.39663, lon: 16.89866 }); const handleSubmit = async ( event: FormEvent | MouseEvent ): Promise => { event.preventDefault(); + console.log(fileState, formState); }; const handleUpload = (e: ChangeEvent) => { @@ -42,11 +49,10 @@ const App: FC = () => { reader.addEventListener( "load", function () { - dispatch({ + fileDispatch({ type: "ADD_FILE", payload: { img: this.result, - // id: Date.now().toString(), selected: false, }, @@ -61,15 +67,38 @@ const App: FC = () => { [].forEach.call(files, readAndPreview); } }; - + useEffect(() => { + getAddress(mapPin).then((blob) => + formDispatch({ + type: "CHANGE_FIELD", + payload: { + field: "address", + value: blob, + }, + }) + ); + }, [mapPin]); const MapComponent = () => { useMapEvents({ - click: (e) => { - setMapPin([e.latlng.lat, e.latlng.lng]); + click: async (e) => { + setMapPin({ lat: e.latlng.lat, lon: e.latlng.lng }); }, }); return null; }; + // console.log(getAddress(mapPin)); + const formMessage = `Rejestracja: ${formState.plate}\nMiejsce: ${ + formState.address.road === undefined ? "" : formState.address.road + } ${ + formState.address.house_number === undefined + ? "" + : formState.address.house_number + }\nData: ${new Date().toLocaleString()}\nMoje dane: ${formState.name + ","} ${ + formState.email + }\n${formState.my_address}\nPowód: ${formState.offenses.join( + ", \n" + )}\nKomentarz: ${formState.comment} + `; return (
@@ -80,7 +109,7 @@ const App: FC = () => {
- dispatch({ + fileDispatch({ type: "SELECT_FILE", payload: { id: (e.target as any).id }, }) @@ -111,14 +140,14 @@ const App: FC = () => { - +
@@ -130,14 +159,58 @@ const App: FC = () => { onSubmit={(e) => handleSubmit(e)} > - + + formDispatch({ + type: "CHANGE_FIELD", + payload: { field: e.target.id, value: e.target.value }, + }) + } + /> - + + formDispatch({ + type: "CHANGE_FIELD", + payload: { field: e.target.id, value: e.target.value }, + }) + } + /> + + + formDispatch({ + type: "CHANGE_FIELD", + payload: { field: e.target.id, value: e.target.value }, + }) + } + /> - - -