From 7f711a5133afa84cf0a3240fe2c3b942e5da430e Mon Sep 17 00:00:00 2001 From: Anton Tranelis Date: Tue, 26 Mar 2024 23:58:16 +0100 Subject: [PATCH] added OverlayIndexItemPage --- .../Templates/OverlayItemsIndexPage.tsx | 169 ++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 src/Components/Templates/OverlayItemsIndexPage.tsx diff --git a/src/Components/Templates/OverlayItemsIndexPage.tsx b/src/Components/Templates/OverlayItemsIndexPage.tsx new file mode 100644 index 00000000..5bb8699e --- /dev/null +++ b/src/Components/Templates/OverlayItemsIndexPage.tsx @@ -0,0 +1,169 @@ +import { ReactNode, useEffect, useRef, useState } from 'react' +import { Link, useNavigate } from 'react-router-dom'; +import { Item, ItemsApi } from '../../types'; +import { getValue } from '../../Utils/GetValue'; +import { TextView } from '../Map'; +import { PlusButton } from '../Profile/PlusButton'; +import { TextInput, TextAreaInput } from '../Input'; +import { useAddTag, useTags } from '../Map/hooks/useTags'; +import { toast } from 'react-toastify'; +import { hashTagRegex } from '../../Utils/HashTagRegex'; +import { randomColor } from '../../Utils/RandomColor'; +import { useAuth } from '../Auth'; +import { useLayers } from '../Map/hooks/useLayers'; +import { HeaderView } from '../Map/Subcomponents/ItemPopupComponents/HeaderView'; +import { MapOverlayPage } from './MapOverlayPage'; + + +type breadcrumb = { + name: string, + path: string +} + + +export const OverlayItemsIndexPage = ({ api, url, parameterField, breadcrumbs, itemNameField, itemTextField, itemImageField, itemSymbolField, itemSubnameField, plusButton = true, children }: { api: ItemsApi, url: string, parameterField: string, breadcrumbs: Array, itemNameField: string, itemTextField: string, itemImageField: string, itemSymbolField: string, itemSubnameField: string, plusButton?: boolean, children?: ReactNode }) => { + + console.log(itemSymbolField); + + const [loading, setLoading] = useState(false); + const [addItemPopupType, setAddItemPopupType] = useState(""); + + const tabRef = useRef(null); + + function scroll() { + tabRef.current?.scrollIntoView(); + } + + useEffect(() => { + scroll(); + }, [addItemPopupType]) + + const [items, setItems] = useState([]); + + const loadProjects = async () => { + const items = await api?.getItems(); + setItems(items as any); + } + + const navigate = useNavigate(); + + const tags = useTags(); + const addTag = useAddTag(); + const { user } = useAuth(); + + + useEffect(() => { + loadProjects(); + }, [api]) + + const layers = useLayers(); + + const submitNewItem = async (evt: any, type: string) => { + evt.preventDefault(); + const formItem: Item = {} as Item; + Array.from(evt.target).forEach((input: HTMLInputElement) => { + if (input.name) { + formItem[input.name] = input.value; + } + }); + setLoading(true); + formItem.text && formItem.text.toLocaleLowerCase().match(hashTagRegex)?.map(tag => { + if (!tags.find((t) => t.name.toLocaleLowerCase() === tag.slice(1).toLocaleLowerCase())) { + addTag({ id: crypto.randomUUID(), name: tag.slice(1), color: randomColor() }) + } + }); + const uuid = crypto.randomUUID(); + let success = false; + try { + await api?.createItem!({ ...formItem, id: uuid, type: type }); + success = true; + } catch (error) { + toast.error(error.toString()); + } + if (success) { + toast.success("New item created"); + } + setLoading(false); + setAddItemPopupType(""); + setItems(current => [...current, { ...formItem, id: uuid, type: type, layer: layers.find(l => l.name == addItemPopupType), user_created: user }]) + } + + const deleteItem = async (item) => { + setLoading(true); + let success = false; + try { + await api?.deleteItem!(item.id) + success = true; + } catch (error) { + toast.error(error.toString()); + } + if (success) { + toast.success("Item deleted"); + } + setLoading(false); + setItems(items.filter(i => i.id != item.id)) + console.log("chaka"); + } + + + + return ( +<> + + + +
+ {breadcrumbs && +
+
    + {breadcrumbs.map((b, i) =>
  • {b.name}
  • )} +
+
} +
+
+ { + items?.map((i, k) => { + return ( +
navigate(url + getValue(i, parameterField))}> + navigate("/edit-item/" + i.id)} deleteCallback={() => deleteItem(i)}> +
+ +
+
+ + ) + + }) + } + {addItemPopupType == "project" ? + +
submitNewItem(e, addItemPopupType)} > + +
+ + + +
+ +
+
+
: <> + } + +
+ {children} + + +
+ + + {plusButton && { setAddItemPopupType("project"); scroll(); }} color={'#777'} collection='items' />} + + + + + ) +}