From 44bfedb908d2c151f284a1aaeeb79c7a719dfca4 Mon Sep 17 00:00:00 2001 From: Egor Savkin Date: Thu, 22 Feb 2024 17:26:07 +0800 Subject: [PATCH] Prototype search bar for the backlog Signed-off-by: Egor Savkin --- static/js/shop/Backlog.jsx | 44 ++++++-------------------- static/js/shop/BacklogGroups.jsx | 44 ++++++++++++++++++++++++++ static/js/shop/BacklogSearchResult.jsx | 16 ++++++++++ static/js/shop/SearchBar.jsx | 19 +++++++++++ static/js/shop/shop_store.js | 16 ++++++++++ 5 files changed, 104 insertions(+), 35 deletions(-) create mode 100644 static/js/shop/BacklogGroups.jsx create mode 100644 static/js/shop/BacklogSearchResult.jsx create mode 100644 static/js/shop/SearchBar.jsx diff --git a/static/js/shop/Backlog.jsx b/static/js/shop/Backlog.jsx index 2b14ff7..feba9ba 100644 --- a/static/js/shop/Backlog.jsx +++ b/static/js/shop/Backlog.jsx @@ -1,9 +1,11 @@ import React from 'react'; import {Droppable} from "@hello-pangea/dnd"; -import {ProductItem} from "./ProductItem"; import {useShopStore} from "./shop_store"; // #!render_count import {useRenderCount} from "@uidotdev/usehooks"; +import {BacklogGroups} from "./BacklogGroups"; +import {SearchBar} from "./SearchBar"; +import {BacklogSearchResult} from "./BacklogSearchResult"; /** * Component that renders the backlog in the aside @@ -14,43 +16,13 @@ export function Backlog() { const data = useShopStore((state) => state.groups); const items = useShopStore((state) => state.cards); + const onClickToggleMobileSideMenu = useShopStore((state) => state.switchSideMenu); const isMobile = useShopStore((state) => state.isMobile); // #!render_count console.log("Backlog renders: ", renderCount) - const ordered_groups = data.categories.map(groupItem => ({ - name: groupItem.name, - items: groupItem.itemIds.map(itemId => items[itemId]) - })); - let item_index = -1; - const groups = ordered_groups.map((group, g_index) => { - return ( -
-

- -

-
-
- {group.items.map(item => { - item_index++; - return ( - - ) - })} -
-
-
- ); - } - ); - return ( ) : null} -
- {groups} -
+ + + + + {provided.placeholder && (
diff --git a/static/js/shop/BacklogGroups.jsx b/static/js/shop/BacklogGroups.jsx new file mode 100644 index 0000000..592d68e --- /dev/null +++ b/static/js/shop/BacklogGroups.jsx @@ -0,0 +1,44 @@ +import {ProductItem} from "./ProductItem"; +import React from "react"; +import {useShopStore} from "./shop_store"; + +export function BacklogGroups() { + const data = useShopStore((state) => state.groups); + const items = useShopStore((state) => state.cards); + + const ordered_groups = data.categories.map(groupItem => ({ + name: groupItem.name, + items: groupItem.itemIds.map(itemId => items[itemId]) + })); + let item_index = -1; + const groups = ordered_groups.map((group, g_index) => { + return ( +
+

+ +

+
+
+ {group.items.map(item => { + item_index++; + return ( + + ) + })} +
+
+
+ ); + } + ); + return ( +
+ {groups} +
+ ) +} \ No newline at end of file diff --git a/static/js/shop/BacklogSearchResult.jsx b/static/js/shop/BacklogSearchResult.jsx new file mode 100644 index 0000000..77b4dd0 --- /dev/null +++ b/static/js/shop/BacklogSearchResult.jsx @@ -0,0 +1,16 @@ +import React from 'react'; +import {useShopStore} from "./shop_store"; +import {ProductItem} from "./ProductItem"; + + +export function BacklogSearchResult() { + const cards_to_display = useShopStore((state) => state.listed_cards); + return ( <> + {cards_to_display.map((item, _) => { + return ( + + ) + })} + + ) +} \ No newline at end of file diff --git a/static/js/shop/SearchBar.jsx b/static/js/shop/SearchBar.jsx new file mode 100644 index 0000000..7285a0e --- /dev/null +++ b/static/js/shop/SearchBar.jsx @@ -0,0 +1,19 @@ +import React from 'react'; +import {useShopStore} from "./shop_store"; + +export function SearchBar() { + const search_bar_value = useShopStore((state) => state.search_bar_value); + const updateSearchBar = useShopStore((state) => state.updateSearchBar); + + return ( +
+ updateSearchBar(event.target.value)} + aria-label="Search"/> +
+ ) +} \ No newline at end of file diff --git a/static/js/shop/shop_store.js b/static/js/shop/shop_store.js index 05c5e28..b98d3a1 100644 --- a/static/js/shop/shop_store.js +++ b/static/js/shop/shop_store.js @@ -29,6 +29,21 @@ const useBacklog = ((set, get) => ({ cardIndexById: card_id => get().cards_list.findIndex((element) => (card_id === element)) })); +const useSearch = ((set, get) => ({ + search_index: Array.from(Object.values(shared_data.items) + .map((card, _) => ( + [(card.name + " " + card.name_number + " " + card.name_codename).toLowerCase(), card.id] + ))), + search_bar_value: "", + listed_cards: [], + updateSearchBar: text => set(state => ({ + search_bar_value: text, + listed_cards: text.length > 0 ? Array.from(get().search_index + .filter((card, _) => card[0].includes(text.toLowerCase())) + .map(([index, card_id], _) => get().cards_list.findIndex(elem => elem === card_id))) : [] + })) +})); + const useCrateModes = ((set, get) => ({ crate_modes: shared_data.crateModes, modes_order: shared_data.crateModeOrder, @@ -569,6 +584,7 @@ const useCart = ((set, get) => ({ export const useShopStore = createWithEqualityFn((...params) => ({ ...useBacklog(...params), + ...useSearch(...params), ...useCrateModes(...params), ...useCart(...params), ...useSubmitForm(...params),