From 4198b4144920e7ef58f98d1025561a02d70a8db2 Mon Sep 17 00:00:00 2001 From: Javier Aguirre Date: Mon, 28 Nov 2022 15:18:18 +0100 Subject: [PATCH] Refactor using generic item toggleMap function (#6791) --- app/screens/integration_selector/index.ts | 1 - .../integration_selector.tsx | 63 +++++++------------ 2 files changed, 23 insertions(+), 41 deletions(-) diff --git a/app/screens/integration_selector/index.ts b/app/screens/integration_selector/index.ts index 2e8ac6634..a0b2dcee8 100644 --- a/app/screens/integration_selector/index.ts +++ b/app/screens/integration_selector/index.ts @@ -9,7 +9,6 @@ import IntegrationSelector from './integration_selector'; import type {WithDatabaseArgs} from '@typings/database/database'; -// See LICENSE.txt for license information. const withTeamId = withObservables([], ({database}: WithDatabaseArgs) => ({ currentTeamId: observeCurrentTeamId(database), })); diff --git a/app/screens/integration_selector/integration_selector.tsx b/app/screens/integration_selector/integration_selector.tsx index 275cacba6..15ba2b739 100644 --- a/app/screens/integration_selector/integration_selector.tsx +++ b/app/screens/integration_selector/integration_selector.tsx @@ -66,6 +66,18 @@ const extractItemKey = (dataSource: string, item: Selection): string => { } }; +const toggleFromMap = (current: MultiselectSelectedMap, key: string, item: T): MultiselectSelectedMap => { + const newMap = {...current}; + + if (current[key]) { + delete newMap[key]; + } else { + newMap[key] = item; + } + + return newMap; +}; + const filterSearchData = (source: string, searchData: DataType, searchTerm: string) => { if (!searchData) { return []; @@ -190,56 +202,27 @@ function IntegrationSelector( switch (dataSource) { case ViewConstants.DATA_SOURCE_USERS: { - setMultiselectSelected((current) => { - const multiselectSelectedItems = {...current}; - - if (current[itemKey]) { - delete multiselectSelectedItems[itemKey]; - } else { - multiselectSelectedItems[itemKey] = item as UserProfile; - } - - return multiselectSelectedItems; - }); + setMultiselectSelected((current) => toggleFromMap(current, itemKey, item as UserProfile)); return; } case ViewConstants.DATA_SOURCE_CHANNELS: { - setMultiselectSelected((current) => { - const multiselectSelectedItems = {...current}; - - if (current[itemKey]) { - delete multiselectSelectedItems[itemKey]; - } else { - multiselectSelectedItems[itemKey] = item as Channel; - } - - return multiselectSelectedItems; - }); + setMultiselectSelected((current) => toggleFromMap(current, itemKey, item as Channel)); return; } default: { - setMultiselectSelected((current) => { - const multiselectSelectedItems = {...current}; - - if (current[itemKey]) { - delete multiselectSelectedItems[itemKey]; - } else { - multiselectSelectedItems[itemKey] = item as DialogOption; - } - - return multiselectSelectedItems; - }); + setMultiselectSelected((current) => toggleFromMap(current, itemKey, item as DialogOption)); } } }, [isMultiselect, dataSource, handleSelect]); const handleRemoveOption = useCallback((item: UserProfile | Channel | DialogOption) => { - const currentSelected: Dictionary | Dictionary | Dictionary = multiselectSelected; const itemKey = extractItemKey(dataSource, item); - const multiselectSelectedItems = {...currentSelected}; - delete multiselectSelectedItems[itemKey]; - setMultiselectSelected(multiselectSelectedItems); - }, [dataSource, multiselectSelected]); + setMultiselectSelected((current) => { + const multiselectSelectedItems = {...current}; + delete multiselectSelectedItems[itemKey]; + return multiselectSelectedItems; + }); + }, [dataSource]); const getChannels = useCallback(debounce(async () => { if (next.current && !loading && !term) { @@ -529,8 +512,8 @@ function IntegrationSelector( } }; - const renderSelectedOptions = useCallback((): React.ReactElement | null => { - const selectedItems: any = Object.values(multiselectSelected); + const renderSelectedOptions = useCallback((): React.ReactElement | null => { + const selectedItems: Channel[] | DialogOption[] | UserProfile[] = Object.values(multiselectSelected); if (!selectedItems.length) { return null;