Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate values in the select #599

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions components/unitStats/dps/dpsUnitCustomizing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<WeaponMember[]>([]);

// Create weapon list, `useEffect` to listen for props changes and refresh the
// weapon data.
Expand All @@ -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]);

Expand Down Expand Up @@ -111,7 +111,7 @@ export const DpsUnitCustomizing = (props: IUnitProps) => {
components.push(
<DpsWeaponCard
weapon_member={member}
key={member.weapon_id + props.index}
key={member.weapon_id + props.index + member.num}
onDeleteMember={onDelete}
onNumberChange={onWeaponNumberChange}
></DpsWeaponCard>,
Expand Down Expand Up @@ -278,7 +278,7 @@ export const DpsUnitCustomizing = (props: IUnitProps) => {
</Grid.Col>
</Grid>

<WeaponSearch searchData={weaponList} onSelect={onAddWeapon}></WeaponSearch>
<WeaponSearch searchData={weaponList} onSelect={onAddWeapon} />

<Group gap="xs">{components}</Group>
</Stack>
Expand Down
19 changes: 14 additions & 5 deletions components/unitStats/dps/weaponSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<Select
//label="Choose a unit"
placeholder="Configure weapons and squad size"
// clearSearchOnChange={true}
// clearSearchOnBlur={true}
clearable
renderOption={renderSelectOption}
data={props.searchData}
// valueComponent={Value}
data={cleanedSearchData}
searchable
maxDropdownHeight={600}
nothingFoundMessage="Nothing here. War is over!"
Expand Down