From 79bc77f70322b484672e8d5f97be7d4a84406ecf Mon Sep 17 00:00:00 2001 From: Petr Vecera Date: Mon, 30 Sep 2024 12:40:27 +0200 Subject: [PATCH] Fix duplicated values in the select --- .../unitStats/dps/dpsUnitCustomizing.tsx | 10 +++++----- components/unitStats/dps/weaponSearch.tsx | 19 ++++++++++++++----- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/components/unitStats/dps/dpsUnitCustomizing.tsx b/components/unitStats/dps/dpsUnitCustomizing.tsx index 0d50aed0..0123c16e 100644 --- a/components/unitStats/dps/dpsUnitCustomizing.tsx +++ b/components/unitStats/dps/dpsUnitCustomizing.tsx @@ -42,9 +42,7 @@ interface IUnitProps { } export const DpsUnitCustomizing = (props: IUnitProps) => { - const weaponListInit: WeaponMember[] = []; - //const [activeData] = useState(props.unit); - const [weaponList, setWeaponList] = useState(weaponListInit); + const [weaponList, setWeaponList] = useState([]); // Create weapon list, `useEffect` to listen for props changes and refresh the // weapon data. @@ -53,11 +51,13 @@ export const DpsUnitCustomizing = (props: IUnitProps) => { const weaponUpgrades = props.allowAllWeapons ? props.weapons.map((weapon) => mapWeaponMember(props.unit.sbps, props.ebps[0], weapon)) : getSbpsUpgrades(props.unit.sbps, props.ebps, props.weapons); + for (const weaponUpgrade of weaponUpgrades) { // check if weapon is already available if (sbpsWeapons.find((member) => member.weapon_id == weaponUpgrade.weapon_id)) continue; sbpsWeapons.push(weaponUpgrade); } + setWeaponList(sbpsWeapons); }, [props.ebps, props.unit.sbps, props.weapons]); @@ -111,7 +111,7 @@ export const DpsUnitCustomizing = (props: IUnitProps) => { components.push( , @@ -278,7 +278,7 @@ export const DpsUnitCustomizing = (props: IUnitProps) => { - + {components} diff --git a/components/unitStats/dps/weaponSearch.tsx b/components/unitStats/dps/weaponSearch.tsx index 678df67e..bfe3228e 100644 --- a/components/unitStats/dps/weaponSearch.tsx +++ b/components/unitStats/dps/weaponSearch.tsx @@ -49,16 +49,25 @@ export const WeaponSearch = (props: ISearchProps) => { //}); } + const cleanedSearchData = []; + const cleanedSearchDataMapWithValue: { [key: string]: boolean } = {}; + + // We need to avoid duplicates in the select based on value (hopefully there will not be more than 1 duplicate) + for (const item of props.searchData) { + if (cleanedSearchDataMapWithValue[item.value]) { + cleanedSearchData.push({ ...item, value: `${item.value}_2` }); + } else { + cleanedSearchData.push(item); + cleanedSearchDataMapWithValue[item.value] = true; + } + } + return (