From 986f3fbff5ed3d094112ef1a30cee8c082a872ed Mon Sep 17 00:00:00 2001 From: Anton Tranelis Date: Thu, 17 Oct 2024 10:59:30 +0200 Subject: [PATCH] basic Market View --- src/Components/Templates/MarketView.tsx | 79 +++++++++++++++++++++++++ src/Components/Templates/TagView.tsx | 17 ++++-- 2 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 src/Components/Templates/MarketView.tsx diff --git a/src/Components/Templates/MarketView.tsx b/src/Components/Templates/MarketView.tsx new file mode 100644 index 00000000..0e35f918 --- /dev/null +++ b/src/Components/Templates/MarketView.tsx @@ -0,0 +1,79 @@ +import { useEffect } from "react"; +import { useItems } from "../Map/hooks/useItems" +import { useState } from "react"; +import { Tag } from "../../types"; +import { useTags } from "../Map/hooks/useTags"; +import { getValue } from "../../Utils/GetValue"; +import { MapOverlayPage } from "./MapOverlayPage"; +import { TagView } from "./TagView"; +import { useNavigate } from "react-router-dom"; + + +function groupAndCount(arr) { + const grouped = arr.reduce((acc, obj) => { + const found = acc.find(item => JSON.stringify(item.object) === JSON.stringify(obj)); + + if (found) { + found.count += 1; + } else { + acc.push({ object: obj, count: 1 }); + } + + return acc; + }, []); + + return grouped.sort((a, b) => b.count - a.count); +} + + +export const MarketView = () => { + + const [offers, setOffers] = useState>([]); + const [needs, setNeeds] = useState>([]); + const navigate = useNavigate(); + + const items = useItems(); + const tags = useTags(); + + useEffect(() => { + setOffers([]); + setNeeds([]); + items.map(i => { + i?.layer?.itemOffersField && getValue(i, i.layer.itemOffersField)?.map(o => { + const tag = tags.find(t => t.id === o.tags_id); + tag && setOffers(current => [...current, tag]) + }) + i?.layer?.itemNeedsField && getValue(i, i.layer.itemNeedsField)?.map(n => { + const tag = tags.find(t => t.id === n.tags_id); + tag && setNeeds(current => [...current, tag]) + }) + }) + console.log(offers); + + }, [items]) + + + return ( + +
+
+

Offers

+
+ {groupAndCount(offers).map(o => ( + navigate(`/?tags=${o.object.name}`)} key={o.object.id} tag={o.object} count={o.count}> + ))} +
+
+
+

Needs

+
+ {groupAndCount(needs).map(o => ( + navigate(`/?tags=${o.object.name}`)} key={o.object.id} tag={o.object} count={o.count}> + ))} +
+
+
+ +
+ ) +} diff --git a/src/Components/Templates/TagView.tsx b/src/Components/Templates/TagView.tsx index fe1f2a0f..1b2cd050 100644 --- a/src/Components/Templates/TagView.tsx +++ b/src/Components/Templates/TagView.tsx @@ -2,14 +2,19 @@ import * as React from 'react' import { decodeTag } from '../../Utils/FormatTags' import { Tag } from '../../types' -export const TagView = ({tag, heighlight, onClick} : {tag: Tag, heighlight?: boolean, onClick?: (e)=> void}) => { +export const TagView = ({ tag, heighlight, onClick, count }: { tag: Tag, heighlight?: boolean, onClick?: (e) => void, count?: number }) => { return ( - // Use your imagination to render suggestions. + // Use your imagination to render suggestions. -
-
-
{decodeTag(tag.name)} -
+
+ {decodeTag(tag.name)} + {count && ({count})} +
) }