From aef9f56148b88d86df33f2433b8dbb8e295b9d6b Mon Sep 17 00:00:00 2001 From: AT Date: Thu, 4 Aug 2022 11:51:04 +0200 Subject: [PATCH] basic useItems hook --- LICENSE.md | 5 ++ package.json | 2 +- src/Components/Map/Layer.tsx | 72 +++++++++++++------------ src/Components/Map/NewItemPopup.tsx | 33 +++++++++--- src/Components/Map/UtopiaMap.tsx | 82 ++++++++++++++++++----------- src/Components/Map/index.tsx | 1 + src/Components/Map/useItems.tsx | 73 +++++++++++++++++++++++++ src/index.tsx | 5 +- src/types.ts | 41 ++++++++++----- 9 files changed, 221 insertions(+), 93 deletions(-) create mode 100644 LICENSE.md create mode 100644 src/Components/Map/index.tsx create mode 100644 src/Components/Map/useItems.tsx diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 00000000..1f95d26c --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,5 @@ +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/package.json b/package.json index 7ca6660b..5b8edb06 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ ], "keywords": [], "author": "Anton Tranelis", - "license": "ISC", + "license": "MIT", "devDependencies": { "@types/leaflet": "^1.7.11", "@types/react": "^18.0.14", diff --git a/src/Components/Map/Layer.tsx b/src/Components/Map/Layer.tsx index cce614b0..15e72f0c 100644 --- a/src/Components/Map/Layer.tsx +++ b/src/Components/Map/Layer.tsx @@ -3,12 +3,13 @@ import { Marker } from 'react-leaflet' import { Item, Tag } from '../../types' import MarkerIconFactory from '../../Utils/MarkerIconFactory' import { Popup } from './Popup' +import { useItems } from './useItems' export interface LayerProps { data: Item[], - children?: React.ReactNode - name : string, + children?: React.ReactNode + name: string, menuIcon: string, menuColor: string, menuText: string, @@ -18,42 +19,43 @@ export interface LayerProps { tags?: Tag[] } - - export const Layer = (props: LayerProps) => { - // create a JS-Map with all Tags - let tagMap = new Map(props.tags?.map(key => [key.id, key])); + // create a JS-Map with all Tags + let tagMap = new Map(props.tags?.map(key => [key.id, key])); - // returns all tags for passed item - const getTags = (item: Item) => { - let tags: Tag[] = []; - item.tags && item.tags.forEach(element => { - if (tagMap.has(element)) { tags.push(tagMap.get(element)!); }; - }); - return tags; - }; + // returns all tags for passed item + const getTags = (item: Item) => { + let tags: Tag[] = []; + item.tags && item.tags.forEach(element => { + if (tagMap.has(element)) { tags.push(tagMap.get(element)!); }; + }); + return tags; + }; + + let items = useItems(); return ( <> - { - props.data.map((place: Item) => { - let tags = getTags(place); - let color1 = "#666"; - let color2 = "RGBA(35, 31, 32, 0.2)"; - if (tags[0]) { - color1 = tags[0].color; - } - if (tags[1]) { - color2 = tags[1].color; - } - return ( - - - - ); - }) - } - {props.children} + { + items.map((place: Item) => { + console.log("Items check in Layer: " + items); + let tags = getTags(place); + let color1 = "#666"; + let color2 = "RGBA(35, 31, 32, 0.2)"; + if (tags[0]) { + color1 = tags[0].color; + } + if (tags[1]) { + color2 = tags[1].color; + } + return ( + + + + ); + }) + } + {props.children} - ) - } + ) +} diff --git a/src/Components/Map/NewItemPopup.tsx b/src/Components/Map/NewItemPopup.tsx index 7b642f2a..10f25a15 100644 --- a/src/Components/Map/NewItemPopup.tsx +++ b/src/Components/Map/NewItemPopup.tsx @@ -1,22 +1,39 @@ import * as React from 'react' import { LatLng } from 'leaflet' -import { Popup as LeafletPopup } from 'react-leaflet' +import { Popup as LeafletPopup, useMap } from 'react-leaflet' +import { useState } from 'react' +import { useAddItem } from './useItems' +import { Item } from './UtopiaMap' +import { Geometry } from '../../types' export interface NewItemPopupProps { position: LatLng, - itemType: string, + layer: string, + onSubmit: (name: string, text: string, position : LatLng, layer : string) => void } export default function NewItemPopup(props: NewItemPopupProps) { - console.log(props.itemType); + const [name, setName] = useState('') + const [text, setText] = useState('') + + const map = useMap(); + const addItem = useAddItem(); + + const handleSubmit = async (evt: any) => { + evt.preventDefault() + addItem(new Item(123213, name, text, new Geometry(props.position.lng, props.position.lat))) + map.closePopup(); + } return ( -
New {props.itemType}
- - -
+
+
New {props.layer}
+ setName(e.target.value)} /> + +
+
) -} +} \ No newline at end of file diff --git a/src/Components/Map/UtopiaMap.tsx b/src/Components/Map/UtopiaMap.tsx index 62068165..8593d73e 100644 --- a/src/Components/Map/UtopiaMap.tsx +++ b/src/Components/Map/UtopiaMap.tsx @@ -1,14 +1,15 @@ import { TileLayer, MapContainer, useMapEvents } from "react-leaflet"; import "leaflet/dist/leaflet.css"; import * as React from "react"; -import { Item, Tag } from "../../types" +import { Item, Tag, API, Geometry } from "../../types" import "../../index.css" import { LatLng } from "leaflet"; import MarkerClusterGroup from 'react-leaflet-cluster' import AddButton from "./AddButton"; -import { Layer, LayerProps } from "./Layer"; -import { useState } from "react"; +import { LayerProps } from "./Layer"; +import { useCallback, useState } from "react"; import NewItemPopup, { NewItemPopupProps } from "./NewItemPopup"; +import { ItemsProvider, useAddItem, useItems } from "./useItems"; export interface UtopiaMapProps { height?: string, @@ -18,9 +19,11 @@ export interface UtopiaMapProps { places?: Item[], events?: Item[], tags?: Tag[], - children?: React.ReactNode + children?: React.ReactNode, + api?: API } + export interface MapEventListenerProps { selectMode: string | null, setSelectMode: React.Dispatch>, @@ -32,9 +35,9 @@ function MapEventListener(props: MapEventListenerProps) { click: (e) => { console.log(e.latlng.lat + ',' + e.latlng.lng); console.log(props.selectMode); - + if (props.selectMode != null) { - props.setNewItemPopup({ itemType: props.selectMode, position: e.latlng }) + props.setNewItemPopup({ layer: props.selectMode, position: e.latlng }) props.setSelectMode(null) } }, @@ -45,7 +48,7 @@ function MapEventListener(props: MapEventListenerProps) { return null } -function UtopiaMap(this: any, props: UtopiaMapProps) { +function UtopiaMap(props: UtopiaMapProps) { // init / set default values let center: LatLng = new LatLng(50.6, 9.5); if (props.center) @@ -60,51 +63,66 @@ function UtopiaMap(this: any, props: UtopiaMapProps) { if (props.width) width = props.width; - const [selectMode, setSelectMode] = useState(null); - const [newItemPopup, setNewItemPopup] = useState(undefined); + const [newItemPopup, setNewItemPopup] = useState(null); + + const addItem = useAddItem(); + const items = useItems(); - // all the layers - const layers: LayerProps[] = []; - // put places / events if provided as props - if (props.events) layers.push({ name: 'event', menuIcon: 'CalendarIcon', menuText: 'add new event', menuColor: '#f9a825', markerIcon: 'calendar-days-solid', markerShape: 'square', markerDefaultColor: '#777', data: props.events }); - if (props.places) layers.push({ name: 'place', menuIcon: 'LocationMarkerIcon', menuText: 'add new place', menuColor: '#2E7D32', markerIcon: 'circle-solid', markerShape: 'circle', markerDefaultColor: '#777', data: props.places }); + const handleSubmit = useCallback((name: string, text: string, position : LatLng, layer : string) => { + addItem(new Item(123,name,text,new Geometry(position.lng,position.lat))); + console.log(layer); + console.log("Items check in Callback: " + items); + + }, [addItem]); + + + let layers:LayerProps[] = []; + React.Children.toArray(props.children).map(layer => { + console.log(layer); + + if (React.isValidElement(layer)) + layers.push(layer.props) + }) + + let initial:Item[] = []; + if (props.events) initial = props.events; + + console.log("Items check in Map 1: " + items); + return ( +
- {layers && - layers.map(layer => ( - - )) - } - {console.log("children of UtopiaMap: " + props.children)} + {props.children} {newItemPopup && - + } + {selectMode != null && +
+
+
+ Select {selectMode} position! +
+
+
+ } +
+
); } -export { UtopiaMap, Item, Tag }; +export { UtopiaMap, Item, Tag, API }; diff --git a/src/Components/Map/index.tsx b/src/Components/Map/index.tsx new file mode 100644 index 00000000..b4bdfddb --- /dev/null +++ b/src/Components/Map/index.tsx @@ -0,0 +1 @@ +export { UtopiaMap, Item, Tag, API } from './UtopiaMap' diff --git a/src/Components/Map/useItems.tsx b/src/Components/Map/useItems.tsx new file mode 100644 index 00000000..0e044734 --- /dev/null +++ b/src/Components/Map/useItems.tsx @@ -0,0 +1,73 @@ +import { useCallback, useReducer, createContext, useContext } from "react"; +import * as React from "react"; +import { Item } from "../../types"; + +type ActionType = +| { type: "ADD"; item: Item } +| { type: "REMOVE"; id: number }; + +type UseItemManagerResult = ReturnType; + +const ItemContext = createContext({ + items: [], + addItem: () => {}, + removeItem: () => {} +}); + +function useItemsManager (initialItems: Item[]): { + items: Item[]; + addItem: (item: Item) => void; + removeItem: (id: number) => void; +} { + const [items, dispatch] = useReducer((state: Item[], action: ActionType) => { + switch (action.type) { + case "ADD": + return [ + ...state, + action.item, + ]; + case "REMOVE": + return state.filter(({ id }) => id !== action.id); + default: + throw new Error(); + } + }, initialItems); + + const addItem = useCallback((item: Item) => { + dispatch({ + type: "ADD", + item, + }); + }, []); + + const removeItem = useCallback((id: number) => { + dispatch({ + type: "REMOVE", + id, + }); + }, []); + return { items, addItem, removeItem }; +} + +export const ItemsProvider: React.FunctionComponent<{ + initialItems: Item[], children?: React.ReactNode +}> = ({ initialItems, children }) => ( + + {children} + +); + +export const useItems = (): Item[] => { + const { items } = useContext(ItemContext); + return items; +}; + +export const useAddItem = (): UseItemManagerResult["addItem"] => { + const { addItem } = useContext(ItemContext); + return addItem; +}; + +export const useRemoveItem = (): UseItemManagerResult["removeItem"] => { + const { removeItem } = useContext(ItemContext); + return removeItem; +}; \ No newline at end of file diff --git a/src/index.tsx b/src/index.tsx index bae84799..da105f6d 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,3 +1,2 @@ -import { UtopiaMap, Item, Tag } from './Components/Map/UtopiaMap' -import { Layer } from './Components/Map/Layer'; -export { UtopiaMap, Item, Tag, Layer }; \ No newline at end of file +export { UtopiaMap, Item, Tag, API } from './Components/Map/index' +export { Layer } from './Components/Map/Layer'; \ No newline at end of file diff --git a/src/types.ts b/src/types.ts index 2e2f6638..9192aaa5 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,27 +1,35 @@ -export interface Item { - id: number, - date_created?: string, - date_updated?: string | null, - name: string, - text: string, - position: Geometry, - start?: string, - end?: string, - tags?: number[], - [key: string]: any +export class Item { + id: number; + date_created?: string; + date_updated?: string | null; + name: string; + text: string; + position: Geometry; + start?: string; + end?: string; + tags?: number[]; + [key: string]: any; + constructor(id:number,name:string,text:string,position:Geometry){ + this.id = id; + this.name = name; + this.text = text; + this.position = position; + } } -export interface Geometry { +export class Geometry { type: string; coordinates: number[]; + constructor(lng: number, lat: number) { + this.coordinates = [lng,lat]; + this.type = "Point"; + } } export interface Tag { - color: string; id: number; name: string; - } @@ -32,4 +40,9 @@ export interface Layer { menuText: string, markerIcon: string, markerShape: string +} + +export interface API { + getAll(): Promise, + add(item : Item): Promise, } \ No newline at end of file