import { MapOverlayPage } from '../Templates' import { useAddItem, useItems, useRemoveItem, useUpdateItem } from '../Map/hooks/useItems' import { useLocation, useNavigate } from 'react-router-dom' import { useEffect, useRef, useState } from 'react'; import { Item, Tag } from '../../types'; import { useMap } from 'react-leaflet'; import { LatLng } from 'leaflet'; import { StartEndView, TextView } from '../Map'; import { useAddTag, useTags } from '../Map/hooks/useTags'; import { useAddFilterTag, useResetFilterTags } from '../Map/hooks/useFilter'; import { useHasUserPermission } from '../Map/hooks/usePermissions'; 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'; import { HeaderView } from '../Map/Subcomponents/ItemPopupComponents/HeaderView'; import { useSelectPosition, useSetSelectPosition } from '../Map/hooks/useSelectPosition'; import { useClusterRef } from '../Map/hooks/useClusterRef'; import { useLeafletRefs } from '../Map/hooks/useLeafletRefs'; import { getValue } from '../../Utils/GetValue'; import { TagView } from '../Templates/TagView'; import RelationCard from "./RelationCard"; import ContactInfo from "./ContactInfo"; import ProfileSubHeader from "./ProfileSubHeader"; export function OverlayItemProfile({ userType }: { userType: string }) { const [updatePermission, setUpdatePermission] = useState(false); const [relations, setRelations] = useState>([]); const [activeTab, setActiveTab] = useState(1); const [addItemPopupType, setAddItemPopupType] = useState(""); const [loading, setLoading] = useState(false); const [offers, setOffers] = useState>([]); const [needs, setNeeds] = useState>([]); const location = useLocation(); const items = useItems(); const updateItem = useUpdateItem(); const [item, setItem] = useState({} as Item) const map = useMap(); const layers = useLayers(); const selectPosition = useSelectPosition(); const removeItem = useRemoveItem(); const tags = useTags(); const navigate = useNavigate(); const addTag = useAddTag(); const resetFilterTags = useResetFilterTags(); const addItem = useAddItem(); const { user } = useAuth(); const hasUserPermission = useHasUserPermission(); const setSelectPosition = useSetSelectPosition(); const clusterRef = useClusterRef(); const leafletRefs = useLeafletRefs(); const addFilterTag = useAddFilterTag(); const tabRef = useRef(null); function scroll() { tabRef.current?.scrollIntoView(); } useEffect(() => { scroll(); }, [addItemPopupType]) const [profile, setProfile] = useState(); useEffect(() => { setProfile(items.find(i => (i.user_created?.id === item.user_created?.id) && i.layer?.itemType.name === userType)); }, [item, items]) const updateActiveTab = (id: number) => { setActiveTab(id); let params = new URLSearchParams(window.location.search); let urlTab = params.get("tab"); if (!urlTab?.includes(id.toString())) params.set("tab", `${id ? id : ""}`) window.history.pushState('', '', "?" + params.toString()); } useEffect(() => { const itemId = location.pathname.split("/")[2]; const item = items.find(i => i.id === itemId); item && setItem(item); }, [items, location]) useEffect(() => { setOffers([]); setNeeds([]); setRelations([]); item.layer?.itemOffersField && getValue(item, item.layer.itemOffersField)?.map(o => { const tag = tags.find(t => t.id === o.tags_id); tag && setOffers(current => [...current, tag]) }) item.layer?.itemNeedsField && getValue(item, item.layer.itemNeedsField)?.map(n => { const tag = tags.find(t => t.id === n.tags_id); tag && setNeeds(current => [...current, tag]) }) item.relations?.map(r => { const item = items.find(i => i.id == r.related_items_id) item && setRelations(current => [...current, item]) }) }, [item, items]) useEffect(() => { const setMap = async (marker, x) => { await map.setView(new LatLng(item?.position?.coordinates[1]!, item?.position?.coordinates[0]! + x / 4), undefined); setTimeout(() => { marker.openPopup(); }, 500); } if (item) { if (item.position) { const marker = Object.entries(leafletRefs).find(r => r[1].item == item)?.[1].marker; marker && clusterRef.hasLayer(marker) && clusterRef?.zoomToShowLayer(marker, () => { const bounds = map.getBounds(); const x = bounds.getEast() - bounds.getWest(); setMap(marker, x); } ); } else { const parent = getFirstAncestor(item); const marker = Object.entries(leafletRefs).find(r => r[1].item == parent)?.[1].marker; marker && clusterRef.hasLayer(marker) && clusterRef?.zoomToShowLayer(marker, () => { const bounds = map.getBounds(); const x = bounds.getEast() - bounds.getWest(); setMap(marker, x); } ); } } }, [item]) const getFirstAncestor = (item: Item): Item | undefined => { const parent = items.find(i => i.id === item.parent); if (parent?.parent) { return getFirstAncestor(parent); } else { return parent; } }; useEffect(() => { let params = new URLSearchParams(location.search); let urlTab = params.get("tab"); urlTab ? setActiveTab(Number(urlTab)) : setActiveTab(1); }, [location]) useEffect(() => { item && hasUserPermission("items", "update", item) && setUpdatePermission(true); }, [item]) useEffect(() => { selectPosition && map.closePopup(); }, [selectPosition]) 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(); const layer = layers.find(l => l.name.toLocaleLowerCase().replace("s", "") == addItemPopupType.toLocaleLowerCase()) let success = false; try { await layer?.api?.createItem!({ ...formItem, id: uuid, type: type, parent: item.id }); await linkItem(uuid); success = true; } catch (error) { toast.error(error.toString()); } if (success) { addItem({ ...formItem, id: uuid, type: type, layer: layer, user_created: user, parent: item.id }); 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 } let success = false; try { await item?.layer?.api?.updateItem!(updatedItem) success = true; } catch (error) { toast.error(error.toString()); } if (success) { updateItem({ ...item, relations: new_relations }) toast.success("Item linked"); } } 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 } let success = false; try { await item?.layer?.api?.updateItem!(updatedItem) success = true; } catch (error) { toast.error(error.toString()); } if (success) { updateItem({ ...item, relations: new_relations }) toast.success("Item unlinked"); } } const handleDelete = async (event: React.MouseEvent) => { event.stopPropagation(); setLoading(true); let success = false; try { await item.layer?.api?.deleteItem!(item.id) success = true; } catch (error) { toast.error(error.toString()); } if (success) { removeItem(item); toast.success("Item deleted"); } setLoading(false); map.closePopup(); let params = new URLSearchParams(window.location.search); window.history.pushState({}, "", "/" + `${params ? `?${params}` : ""}`); navigate("/"); } const typeMapping = { 'default': 'Würdekompass', 'themenkompass': 'Themenkompass-Gruppe', 'liebevoll.jetzt': 'liebevoll.jetzt', }; let groupType = item.group_type ? item.group_type : 'default'; let groupTypeText = typeMapping[groupType]; const [template, setTemplate] = useState("") useEffect(() => { setTemplate(item.layer?.itemType.template || userType); }, [userType, item]) return ( <> {item && <>
navigate("/edit-item/" + item.id)} setPositionCallback={() => { map.closePopup(); setSelectPosition(item); navigate("/") }} big truncateSubname={false} /> {template == "onepager" && }
{template == "onepager" &&
{item.user_created.first_name && ( )} {/* Description Section */}
{/* Next Appointment Section */} {item.next_appointment && (

Nächste Termine

)}; {/* Relations Section */} {/*{d.relations && (*/} {/*
*/} {/*

Projekte

*/} {/* {d.relations.map((project, index) => (*/} {/* */} {/* ))}*/} {/*
*/} {/*)}*/}
} {template == "simple" &&
} {template == "tabs" &&
updateActiveTab(1)} />
{item.layer?.itemType.show_start_end &&
}
{item.layer?.itemType.offers_and_needs && <> updateActiveTab(3)} />
{ offers.length > 0 ?

Offers

< div className='tw-flex tw-flex-wrap tw-mb-4'> { offers.map(o => { addFilterTag(o) }} />) }
: "" } { needs.length > 0 ?

Needs

< div className='tw-flex tw-flex-wrap tw-mb-4'> { needs.map(n => addFilterTag(n)} />) }
: "" }
} {item.layer?.itemType.relations && <> updateActiveTab(7)} />
{relations && relations.map(i =>
navigate('/item/' + i.id)}>
)} {updatePermission && }
} }
} ) }