From 8d37c91c1762d96cba6d22e9f54de2818e63f5fe Mon Sep 17 00:00:00 2001 From: Igor <12849336+DoubleCookies@users.noreply.github.com> Date: Wed, 19 May 2021 21:41:42 +0300 Subject: [PATCH] Search weapons by sub or special (en-only) (#503) * search by sub and special weapons * check if data is null * ..forgot about console log --- components/common/WeaponSelector.tsx | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/components/common/WeaponSelector.tsx b/components/common/WeaponSelector.tsx index ce87cc0ba..3ea057684 100644 --- a/components/common/WeaponSelector.tsx +++ b/components/common/WeaponSelector.tsx @@ -8,6 +8,7 @@ import { } from "utils/lists/weaponsWithHero"; import MySelect from "./MySelect"; import WeaponImage from "./WeaponImage"; +import weaponJson from "utils/data/weaponData.json"; interface SelectorProps { autoFocus?: boolean; @@ -60,11 +61,33 @@ const Option = (props: any) => { const customFilterOption = (option: any, rawInput: string) => { const words = rawInput.split(" "); return words.reduce( - (acc, cur) => acc && option.label.toLowerCase().includes(cur.toLowerCase()), + (acc, cur) => acc && (option.label.toLowerCase().includes(cur.toLowerCase()) || + option.data?.data?.sub.toLowerCase() === rawInput.toLowerCase() || + option.data?.data?.special.toLowerCase() === rawInput.toLowerCase()), true ); }; +type WeaponData = { + name: string; + sub: string; + special: string; +}; + +function initWeaponData() { + const weaponData: Record = weaponJson; + const weaponsArray: WeaponData[] = []; + + for (const [key, value] of Object.entries(weaponData)) { + if (value.Special && value.Sub) { + weaponsArray.push({name: key, special: value.Special, sub: value.Sub}); + } + } + return weaponsArray; +} + +const weaponData = initWeaponData(); + const WeaponSelector: React.FC = ( props ) => { @@ -80,6 +103,7 @@ const WeaponSelector: React.FC = ( options: category.weapons.map((weapon) => ({ value: weapon, label: getLabel(weapon), + data: weaponData.find(obj => {return obj.name === weapon}) })), }))} value={getValue()}