diff --git a/src/components/GrossDepositionEnviroment/GdeSelect/GdeSelect.tsx b/src/components/GrossDepositionEnviroment/GdeSelect/GdeSelect.tsx index 2e53aca..b99b7fa 100644 --- a/src/components/GrossDepositionEnviroment/GdeSelect/GdeSelect.tsx +++ b/src/components/GrossDepositionEnviroment/GdeSelect/GdeSelect.tsx @@ -3,6 +3,7 @@ import { Autocomplete, AutocompleteChanges } from '@equinor/eds-core-react'; import { GeologicalStandardDto } from '../../../api/generated'; import { useFetchGrossDepData } from '../../../hooks/useFetchGrossDepData'; import * as StyledDialog from '../../../styles/addRowDialog/AddRowDialog.styled'; +import { sortList } from '../../../utils/SortList'; import { GdeType } from '../GrossDepositionEnviromentGroup/GrossDepositionEnviromentGroup'; export const GdeSelect = ({ @@ -38,17 +39,11 @@ export const GdeSelect = ({ (g) => g.geologyGroup === 'ArchitecturalElement', ); - const sortList = (data: any) => { - return data.sort((a: any, b: any) => - a.identifier.localeCompare(b.identifier), - ); - }; - return ( a.identifier.localeCompare(b.identifier))} + options={sortList(Gde)} optionLabel={(option) => option.identifier} onOptionsChange={(e: AutocompleteChanges) => { setGdeObject({ diff --git a/src/components/OutcropAnalogue/OutcropSelect/OutcropSelect.tsx b/src/components/OutcropAnalogue/OutcropSelect/OutcropSelect.tsx index e24bc37..adb1def 100644 --- a/src/components/OutcropAnalogue/OutcropSelect/OutcropSelect.tsx +++ b/src/components/OutcropAnalogue/OutcropSelect/OutcropSelect.tsx @@ -3,6 +3,7 @@ import { Autocomplete, AutocompleteChanges } from '@equinor/eds-core-react'; import { OutcropDto } from '../../../api/generated'; import { useFetchOutcropData } from '../../../hooks/useFetchOutcropData'; import * as StyledDialog from '../../../styles/addRowDialog/AddRowDialog.styled'; +import { sortList } from '../../../utils/SortList'; import { OutcropType } from '../OutcropAnalogueGroup/OutcropAnalogueGroup'; export const OutcropSelect = ({ @@ -29,9 +30,7 @@ export const OutcropSelect = ({ - a.name.localeCompare(b.name), - )} + options={sortList(OutcropData.data.data)} optionLabel={(option) => option.name} onOptionsChange={(e: AutocompleteChanges) => { const copyObject: OutcropType = { diff --git a/src/components/StrategraphicColumn/StratigraphicColumnSelect/StratigraphicColumnSelect.tsx b/src/components/StrategraphicColumn/StratigraphicColumnSelect/StratigraphicColumnSelect.tsx index 0512aea..6fd39d6 100644 --- a/src/components/StrategraphicColumn/StratigraphicColumnSelect/StratigraphicColumnSelect.tsx +++ b/src/components/StrategraphicColumn/StratigraphicColumnSelect/StratigraphicColumnSelect.tsx @@ -14,6 +14,7 @@ import { useFetchSmdaStratigraphicColumns, } from '../../../hooks/useFetchStratColData'; import * as StyledDialog from '../../../styles/addRowDialog/AddRowDialog.styled'; +import { sortList } from '../../../utils/SortList'; export const StratigraphicColumnSelect = ({ stratColumnObject, @@ -61,9 +62,7 @@ export const StratigraphicColumnSelect = ({ - a.identifier.localeCompare(b.identifier), - )} + options={sortList(filterCountries)} optionLabel={(option) => option.identifier} onOptionsChange={(e: AutocompleteChanges) => { setStratColumnObject({ @@ -82,11 +81,11 @@ export const StratigraphicColumnSelect = ({ field.countryId === stratColumnObject.country?.countryId, - ) - .sort((a, b) => a.identifier.localeCompare(b.identifier))} + ), + )} optionLabel={(option) => option.identifier} onOptionsChange={(e: AutocompleteChanges) => { setStratColumnObject({ @@ -103,16 +102,16 @@ export const StratigraphicColumnSelect = ({ stratColumnObject.country !== undefined && c.countries.filter( (country) => country.countryId === stratColumnObject.country?.countryId, ).length !== 0, - ) - .sort((a, b) => a.identifier.localeCompare(b.identifier))} + ), + )} optionLabel={(option) => option.identifier} onOptionsChange={(e: AutocompleteChanges) => { setStratColumnObject({ @@ -132,13 +131,15 @@ export const StratigraphicColumnSelect = ({ s.level === 1) - .filter( - (c) => - c.stratColumnId === stratColumnObject.stratColumn?.stratColumnId, - ) - .sort((a, b) => a.identifier.localeCompare(b.identifier))} + options={sortList( + stratUnitData.data.data + .filter((s) => s.level === 1) + .filter( + (c) => + c.stratColumnId === + stratColumnObject.stratColumn?.stratColumnId, + ), + )} optionLabel={(option) => option.identifier} onOptionsChange={(e: AutocompleteChanges) => setStratColumnObject({ @@ -157,17 +158,19 @@ export const StratigraphicColumnSelect = ({ s.level === 2) - .filter( - (c) => - c.stratColumnId === stratColumnObject.stratColumn?.stratColumnId, - ) - .filter( - (x) => - x.stratUnitParentId === stratColumnObject.level1?.stratUnitId, - ) - .sort((a, b) => a.identifier.localeCompare(b.identifier))} + options={sortList( + stratUnitData.data.data + .filter((s) => s.level === 2) + .filter( + (c) => + c.stratColumnId === + stratColumnObject.stratColumn?.stratColumnId, + ) + .filter( + (x) => + x.stratUnitParentId === stratColumnObject.level1?.stratUnitId, + ), + )} optionLabel={(option) => option.identifier} onOptionsChange={(e: AutocompleteChanges) => setStratColumnObject({ @@ -185,17 +188,19 @@ export const StratigraphicColumnSelect = ({ s.level === 3) - .filter( - (c) => - c.stratColumnId === stratColumnObject.stratColumn?.stratColumnId, - ) - .filter( - (x) => - x.stratUnitParentId === stratColumnObject.level2?.stratUnitId, - ) - .sort((a, b) => a.identifier.localeCompare(b.identifier))} + options={sortList( + stratUnitData.data.data + .filter((s) => s.level === 3) + .filter( + (c) => + c.stratColumnId === + stratColumnObject.stratColumn?.stratColumnId, + ) + .filter( + (x) => + x.stratUnitParentId === stratColumnObject.level2?.stratUnitId, + ), + )} optionLabel={(option) => option.identifier} onOptionsChange={(e: AutocompleteChanges) => setStratColumnObject({ diff --git a/src/utils/SortList.ts b/src/utils/SortList.ts new file mode 100644 index 0000000..de0f0cb --- /dev/null +++ b/src/utils/SortList.ts @@ -0,0 +1,15 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +export const sortList = (data: any) => { + if (data.length === 0) { + return data; + } else if (data.length !== 0 && data[0]['identifier'] !== undefined) { + return data.sort((a: any, b: any) => + a.identifier.localeCompare(b.identifier), + ); + } else if (data[0]['name'] !== undefined) { + return data.sort((a: any, b: any) => a.name.localeCompare(b.name)); + } else { + console.log('Data has no identifier or name'); + return data; + } +};