/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-unnecessary-condition */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/no-unsafe-call */ /* eslint-disable @typescript-eslint/no-non-null-assertion */ /* eslint-disable @typescript-eslint/no-non-null-asserted-optional-chain */ /* eslint-disable @typescript-eslint/prefer-optional-chain */ /* eslint-disable @typescript-eslint/restrict-template-expressions */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ import { LatLng } from 'leaflet' import { Children, cloneElement, forwardRef, isValidElement, useState } from 'react' import { Popup as LeafletPopup, useMap } from 'react-leaflet' import { useNavigate } from 'react-router-dom' import { toast } from 'react-toastify' import { useRemoveItem, useUpdateItem } from '#components/Map/hooks/useItems' import { useSetSelectPosition } from '#components/Map/hooks/useSelectPosition' import { Item, ItemFormPopupProps } from '#src/types' import { timeAgo } from '#utils/TimeAgo' import { HeaderView } from './ItemPopupComponents/HeaderView' import { TextView } from './ItemPopupComponents/TextView' export interface ItemViewPopupProps { item: Item children?: React.ReactNode setItemFormPopup?: React.Dispatch> } // eslint-disable-next-line react/display-name export const ItemViewPopup = forwardRef((props: ItemViewPopupProps, ref: any) => { const map = useMap() const [loading, setLoading] = useState(false) const removeItem = useRemoveItem() const updadateItem = useUpdateItem() const navigate = useNavigate() const setSelectPosition = useSetSelectPosition() const [infoExpanded, setInfoExpanded] = useState(false) const handleEdit = (event: React.MouseEvent) => { event.stopPropagation() map.closePopup() props.setItemFormPopup && props.setItemFormPopup({ position: new LatLng( props.item.position?.coordinates[1]!, props.item.position?.coordinates[0]!, ), layer: props.item.layer!, item: props.item, setItemFormPopup: props.setItemFormPopup, }) } const handleDelete = async (event: React.MouseEvent) => { event.stopPropagation() setLoading(true) let success = false try { !props.item.layer?.onlyOnePerOwner && (await props.item.layer?.api?.deleteItem!(props.item.id)) props.item.layer?.onlyOnePerOwner && (await props.item.layer.api?.updateItem!({ id: props.item.id, position: null })) success = true // eslint-disable-next-line no-catch-all/no-catch-all } catch (error) { toast.error(error.toString()) } if (success) { !props.item.layer?.onlyOnePerOwner && removeItem(props.item) props.item.layer?.onlyOnePerOwner && updadateItem({ ...props.item, position: undefined }) toast.success('Item deleted') } setLoading(false) map.closePopup() const params = new URLSearchParams(window.location.search) window.history.pushState({}, '', '/' + `${params ? `?${params}` : ''}`) navigate('/') } return (
{ map.closePopup() setSelectPosition(props.item) navigate('/') }} loading={loading} />
{props.children ? ( Children.toArray(props.children).map((child) => isValidElement<{ item: Item; test: string }>(child) ? cloneElement(child, { item: props.item }) : '', ) ) : ( )}
{infoExpanded ? (

{`${props.item.date_updated && props.item.date_updated !== props.item.date_created ? 'updated' : 'posted'} ${props.item && props.item.user_created && props.item.user_created.first_name ? `by ${props.item.user_created.first_name}` : ''} ${props.item.date_updated ? timeAgo(props.item.date_updated) : timeAgo(props.item.date_created!)}`}

) : (

setInfoExpanded(true)} > ⓘ

)}
{ //* * */ }
) })