From afd14a0e9329608a3330dde87f73a1342e00322b Mon Sep 17 00:00:00 2001 From: Aloento <11802769+Aloento@users.noreply.github.com> Date: Fri, 5 Jan 2024 11:37:28 +0100 Subject: [PATCH] Optimise transformer in ProductRadioList --- src/Pages/Product/Context.tsx | 10 +++++++++- src/Pages/Product/RadioGroup.tsx | 26 ++++++++++++-------------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/Pages/Product/Context.tsx b/src/Pages/Product/Context.tsx index ceafd0ab..a40d78d6 100644 --- a/src/Pages/Product/Context.tsx +++ b/src/Pages/Product/Context.tsx @@ -1,4 +1,5 @@ import { createContext, useContext, useState } from "react"; +import { IComboItem } from "../Admin/Product/Combo"; /** * @author Aloento @@ -6,8 +7,12 @@ import { createContext, useContext, useState } from "react"; * @version 0.1.0 */ interface Context { + /** Current Selected Combination */ Current: Record; Update: (val: Record) => void; + + All: IComboItem[]; + SetAll: (val: IComboItem[]) => void; } /** @@ -29,15 +34,18 @@ export function useRadioGroup() { /** * @author Aloento * @since 0.5.0 - * @version 0.1.0 + * @version 0.2.0 */ export function RadioGroupContext({ children }: { children: React.ReactNode }) { const [curr, setCurr] = useState({}); + const [all, setAll] = useState([]); return ( {children} diff --git a/src/Pages/Product/RadioGroup.tsx b/src/Pages/Product/RadioGroup.tsx index ff816953..92510fbc 100644 --- a/src/Pages/Product/RadioGroup.tsx +++ b/src/Pages/Product/RadioGroup.tsx @@ -34,52 +34,50 @@ const log = new Logger("Product", "RadioGroup"); /** * @author Aloento * @since 0.5.0 - * @version 0.4.0 + * @version 0.5.0 */ export function ProductRadioList({ ProdId }: { ProdId: number }) { - const { Update } = useRadioGroup(); - const [variants, setVariants] = useState>>({}); + const { Update, SetAll } = useRadioGroup(); + const [variants, setVariants] = useState<[string, string[]][]>([]); const { loading } = useRequest(() => Hub.Product.Get.Combo(ProdId, log), { onError: log.error, onSuccess(data) { const variant: Record> = {}; - const cur: Record = {}; for (const i of data) for (const [vari, type] of Object.entries(i.Combo)) if (variant.hasOwnProperty(vari)) variant[vari].add(type); - else { + else variant[vari] = new Set([type]); - cur[vari] = type; - } - Update(cur); - setVariants(variant); + SetAll(data); + Update(data[0].Combo); + setVariants(Object.entries(variant).map(([val, type]) => [val, Array.from(type)])); } }); if (loading) return ; - return Object.keys(variants).map((val, i) => ); + return variants.map(([val, type], i) => ); } /** * @author Aloento * @since 0.5.0 - * @version 0.1.0 + * @version 0.1.1 */ interface IVariRadioGroup { Variant: string; - Types: Set; + Types: string[]; } /** * @author Aloento * @since 0.5.0 - * @version 0.2.1 + * @version 0.2.2 */ function VariRadioGroup({ Variant, Types }: IVariRadioGroup) { const style = useStyle(); @@ -92,7 +90,7 @@ function VariRadioGroup({ Variant, Types }: IVariRadioGroup) {
- {Array.from(Types).map((val, i) => + {Types.map((val, i) =>