import { MapOverlayPage } from '../Templates' import { useAddItem, useItems, useUpdateItem } from '../Map/hooks/useItems' import { useLocation, useNavigate } from 'react-router-dom' import { useEffect, useRef, useState } from 'react'; import { Item } from '../../types'; import { getValue } from '../../Utils/GetValue'; import { useMap } from 'react-leaflet'; import { LatLng } from 'leaflet'; import { PopupStartEndInput, StartEndView, TextView } from '../Map'; import useWindowDimensions from '../Map/hooks/useWindowDimension'; import { useAddTag, useTags } from '../Map/hooks/useTags'; import { useResetFilterTags } from '../Map/hooks/useFilter'; import { useHasUserPermission } from '../Map/hooks/usePermissions'; import { TextAreaInput, TextInput } from '../Input'; import { hashTagRegex } from '../../Utils/HashTagRegex'; import { randomColor } from '../../Utils/RandomColor'; import { toast } from 'react-toastify'; import { useAuth } from '../Auth'; import { useLayers } from '../Map/hooks/useLayers'; import { ActionButton } from './ActionsButton'; import { LinkedItemsHeaderView } from './LinkedItemsHeaderView'; export function OverlayItemProfile() { const location = useLocation(); const items = useItems(); const updateItem = useUpdateItem(); const [item, setItem] = useState({} as Item) const map = useMap(); const windowDimension = useWindowDimensions(); const [addButton, setAddButton] = useState(false); const layers = useLayers(); const tags = useTags(); const navigate = useNavigate(); const [relations, setRelations] = useState>([]); const [activeTab, setActiveTab] = useState(1); const [addItemPopupType, setAddItemPopupType] = useState(""); const [loading, setLoading] = useState(false); const addTag = useAddTag(); const resetFilterTags = useResetFilterTags(); const addItem = useAddItem(); const { user } = useAuth(); const hasUserPermission = useHasUserPermission(); const tabRef = useRef(null); function scroll() { tabRef.current?.scrollIntoView(); } useEffect(() => { scroll(); }, [addItemPopupType]) useEffect(() => { console.log(addItemPopupType); }, [addItemPopupType]) useEffect(() => { const itemId = location.pathname.split("/")[2]; const item = items.find(i => i.id === itemId); item && setItem(item); const bounds = map.getBounds(); const x = bounds.getEast() - bounds.getWest() if (windowDimension.width > 768) if (item?.position && item?.position.coordinates[0]) map.setView(new LatLng(item?.position.coordinates[1]!, item?.position.coordinates[0]! + x / 4)) }, [location, items, activeTab]) useEffect(() => { setActiveTab(1); }, [location]) useEffect(() => { setRelations([]); item.relations?.map(r => { const item = items.find(i => i.id == r.related_items_id) item && setRelations(current => [...current, item]) }) }, [item, items]) useEffect(() => { item && item.user_created && hasUserPermission("items", "update", item) && setAddButton(true); }, [item]) 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(); console.log(layers); const layer = layers.find(l => l.name.toLocaleLowerCase().replace("s", "") == addItemPopupType.toLocaleLowerCase()) console.log(layer); let success = false; try { await layer?.api?.createItem!({ ...formItem, id: uuid, type: type }); await linkItem(uuid); success = true; } catch (error) { toast.error(error.toString()); } if (success) { addItem({ ...formItem, id: uuid, type: type, layer: layer, user_created: user }); toast.success("New item created"); resetFilterTags(); } setLoading(false); setAddItemPopupType(""); } const linkItem = async (id: string) => { let new_relations = item.relations || []; new_relations?.push({ items_id: item.id, related_items_id: id }) const updatedItem = { id: item.id, relations: new_relations } await item?.layer?.api?.updateItem!(updatedItem) updateItem({ ...item, relations: new_relations }) } const unlinkItem = async (id: string) => { console.log(id); let new_relations = item.relations?.filter(r => r.related_items_id !== id) console.log(new_relations); const updatedItem = { id: item.id, relations: new_relations } await item?.layer?.api?.updateItem!(updatedItem) updateItem({ ...item, relations: new_relations }) } return ( {item && <>

{item.layer?.itemAvatarField && getValue(item, item.layer.itemAvatarField) && } {item.layer?.itemNameField && getValue(item, item.layer.itemNameField)}

{(item.layer?.api?.updateItem && hasUserPermission(item.layer.api?.collectionName!, "update", item)) ? navigate("/edit-item/" + item.id)}> : "" }
setActiveTab(1)} />
setActiveTab(2)} />
{relations && relations.map(i => { if (i.type == 'project') return (
navigate('/item/' + i.id)}>
) else return null })} {addItemPopupType == "project" ?
submitNewItem(e, addItemPopupType)} >
: <> } {addButton && { setAddItemPopupType("project"); scroll() }} color={item.color}>}
setActiveTab(3)} />
{relations && relations.map(i => { if (i.type == 'event') return (
navigate('/item/' + i.id)}>
) else return null })} {addItemPopupType == "event" ?
submitNewItem(e, addItemPopupType)} >
: <> } {addButton && { setAddItemPopupType("event"); scroll() }} color={item.color}>}
setActiveTab(4)} />
}
) }