diff --git a/src/app.tsx b/src/app.tsx index 05808fb..84b5190 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -1,38 +1,56 @@ -import React, { useState, useReducer } from "react"; +import React, { + useState, + useReducer, + MouseEvent, + FC, + ChangeEvent, + FormEvent, +} 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 "regenerator-runtime/runtime"; -import { - MapContainer, - TileLayer, - Marker, - Popup, - useMapEvents, -} from "react-leaflet"; +import { MapContainer, TileLayer, Marker, useMapEvents } from "react-leaflet"; import "./styles/reset.css"; import "./styles/app.scss"; -const App: React.FC = () => { - const [files, setFiles] = useState([]); - const [mapPin, setMapPin] = useState([52.39663, 16.89866]); +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. + //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: [] }); + + //This hook handles pining location on the map + const [mapPin, setMapPin] = useState([52, 16]); const handleSubmit = async ( - event: React.FormEvent + event: FormEvent | MouseEvent ): Promise => { event.preventDefault(); }; - const handleUpload = (e: React.ChangeEvent) => { + const handleUpload = (e: ChangeEvent) => { const files = e.target.files; - const newstate = []; - const readAndPreview = (file) => { + const readAndPreview = (file: any) => { // Make sure `file.name` matches our extensions criteria if (/\.(jpe?g|png|svg)$/i.test(file.name)) { let reader = new FileReader(); reader.addEventListener( "load", function () { - newstate.push(this.result); - setFiles([...newstate]); + dispatch({ + type: "ADD_FILE", + payload: { + img: this.result, + // + id: Date.now().toString(), + selected: false, + }, + }); }, false ); @@ -43,23 +61,37 @@ const App: React.FC = () => { [].forEach.call(files, readAndPreview); } }; + const MapComponent = () => { useMapEvents({ click: (e) => { setMapPin([e.latlng.lat, e.latlng.lng]); - console.log(mapPin); }, }); return null; }; + return (

Zdjęcia

- {files.map((file) => ( -
- + {fileState.files.map((file: File, index: number) => ( +
+ dispatch({ + type: "SELECT_FILE", + payload: { id: (e.target as any).id }, + }) + } + className="section__photos__item" + > +
))}
@@ -68,6 +100,7 @@ const App: React.FC = () => { name="image-upload" id="input" accept="image/*" + className="input" multiple onChange={(e) => handleUpload(e)} /> @@ -85,7 +118,7 @@ const App: React.FC = () => { attribution='© OpenStreetMap contributors' url="http://{s}.tile.osm.org/{z}/{x}/{y}.png" /> - +
@@ -104,10 +137,26 @@ const App: React.FC = () => {