Skip to content

Commit 5f88c58

Browse files
committed
feat: add getUpdatedPluginStore util to abstract traversal of new plugin store response to append to data store
1 parent 13bf47c commit 5f88c58

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

src/Shared/Components/Plugin/PluginListContainer.tsx

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
} from './types'
1717
import { DEFAULT_PLUGIN_LIST_FILTERS } from './constants'
1818
import './pluginListContainer.scss'
19+
import { getUpdatedPluginStore } from './utils'
1920

2021
const PluginListContainer = ({
2122
availableTags,
@@ -98,25 +99,12 @@ const PluginListContainer = ({
9899
pluginResponse,
99100
appendResponse = false,
100101
) => {
101-
const clonedPluginDataStore = structuredClone(pluginDataStore)
102102
const {
103103
pluginStore: { parentPluginStore, pluginVersionStore },
104104
totalCount: responseTotalCount,
105105
} = pluginResponse
106106

107-
Object.keys(parentPluginStore).forEach((key) => {
108-
if (!clonedPluginDataStore.parentPluginStore[key]) {
109-
clonedPluginDataStore.parentPluginStore[key] = parentPluginStore[key]
110-
}
111-
})
112-
113-
Object.keys(pluginVersionStore).forEach((key) => {
114-
if (!clonedPluginDataStore.pluginVersionStore[key]) {
115-
clonedPluginDataStore.pluginVersionStore[key] = pluginVersionStore[key]
116-
}
117-
})
118-
119-
handlePluginDataStoreUpdate(clonedPluginDataStore)
107+
handlePluginDataStoreUpdate(getUpdatedPluginStore(pluginDataStore, parentPluginStore, pluginVersionStore))
120108
handleUpdateTotalCount(responseTotalCount)
121109

122110
const newPluginList: typeof pluginList = appendResponse ? structuredClone(pluginList) : []
@@ -142,7 +130,6 @@ const PluginListContainer = ({
142130

143131
useEffect(() => {
144132
const isLoading = isLoadingPluginData || getIsRequestAborted(pluginDataError)
145-
// This will be reusable for load more so convert it to a function
146133
if (!isLoading && !pluginDataError && pluginData) {
147134
handleDataUpdateForPluginResponse(pluginData)
148135
}

src/Shared/Components/Plugin/utils.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,28 @@ export const pluginTagSelectStyles = {
9797
},
9898
}),
9999
}
100+
101+
/**
102+
* @description This method takes the initial plugin data store and updates only keys that are not present in the initial store with the values from the target store.
103+
*/
104+
export const getUpdatedPluginStore = (
105+
initialPluginDataStore: PluginDataStoreType,
106+
targetParentPluginStore: PluginDataStoreType['parentPluginStore'],
107+
targetPluginVersionStore: PluginDataStoreType['pluginVersionStore'],
108+
): PluginDataStoreType => {
109+
const clonedPluginDataStore = structuredClone(initialPluginDataStore)
110+
111+
Object.keys(targetParentPluginStore).forEach((key) => {
112+
if (!clonedPluginDataStore.parentPluginStore[key]) {
113+
clonedPluginDataStore.parentPluginStore[key] = targetParentPluginStore[key]
114+
}
115+
})
116+
117+
Object.keys(targetPluginVersionStore).forEach((key) => {
118+
if (!clonedPluginDataStore.pluginVersionStore[key]) {
119+
clonedPluginDataStore.pluginVersionStore[key] = targetPluginVersionStore[key]
120+
}
121+
})
122+
123+
return clonedPluginDataStore
124+
}

0 commit comments

Comments
 (0)