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 { useAssetApi } from '../AppShell/hooks/useAssets'; import { PlusButton } from '../Profile/PlusButton'; import { TextInput, TextAreaInput } from '../Input'; import { useAddTag, useTags } from '../Map/hooks/useTags'; import { useAddItem } from '../Map/hooks/useItems'; import { useResetFilterTags } from '../Map/hooks/useFilter'; 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'; type breadcrumb = { name: string, path: string } export const ItemsIndexPage = ({ api, url, parameterField, breadcrumbs, itemNameField, itemTextField, itemImageField, itemSymbolField, itemSubnameField, children }: { api: ItemsApi, url: string, parameterField: string, breadcrumbs: Array, itemNameField: string, itemTextField: string, itemImageField: string, itemSymbolField: string, itemSubnameField: string, 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}
  • )}
} {/**
{ setSearch(val) }}> { }} placeholder="Type" containerStyle=' hidden md:grid' defaultValue='PLACEHOLDER' options={[{ name: "local", value: "local" }, { name: "project", value: "project" }]} />
*/}
{ items?.map((i, k) => { return (
navigate(url + getValue(i, parameterField))}> navigate("/edit-item/"+i.id)} deleteCallback={()=>deleteItem(i)}>
) }) } {addItemPopupType == "project" ?
submitNewItem(e, addItemPopupType)} >
: <> }
{ setAddItemPopupType("project"); scroll(); }} color={'#777'} collection='items' /> {children}
) }