@@ -18,7 +18,6 @@ import { ROUTES } from '@Common/Constants'
1818import { get , getUrlWithSearchParams , patch , showError } from '@Common/index'
1919import { THEME_PREFERENCE_MAP } from '@Shared/Providers/ThemeProvider/types'
2020import { BaseAppMetaData } from '@Shared/Services'
21- import { ResourceKindType } from '@Shared/types'
2221
2322import { USER_PREFERENCES_ATTRIBUTE_KEY } from './constants'
2423import {
@@ -39,11 +38,7 @@ import { getUserPreferenceResourcesMetadata } from './utils'
3938 * @description This function fetches the user preferences from the server. It uses the `get` method to make a request to the server and retrieves the user preferences based on the `USER_PREFERENCES_ATTRIBUTE_KEY`. The result is parsed and returned as a `UserPreferencesType` object.
4039 * @throws Will throw an error if the request fails or if the result is not in the expected format.
4140 */
42- export const getUserPreferences = async ( {
43- resourceKindType,
44- } : {
45- resourceKindType ?: ResourceKindType
46- } ) : Promise < UserPreferencesType > => {
41+ export const getUserPreferences = async ( ) : Promise < UserPreferencesType > => {
4742 const queryParamsPayload : Pick < GetUserPreferencesQueryParamsType , 'key' > = {
4843 key : USER_PREFERENCES_ATTRIBUTE_KEY ,
4944 }
@@ -62,32 +57,13 @@ export const getUserPreferences = async ({
6257 ? ViewIsPipelineRBACConfiguredRadioTabs . ACCESS_ONLY
6358 : ViewIsPipelineRBACConfiguredRadioTabs . ALL_ENVIRONMENTS
6459
65- const getPreferredResourcesData = ( ) => {
66- let resources
67- switch ( resourceKindType ) {
68- case ResourceKindType . devtronApplication :
69- resources = {
70- [ UserPreferenceResourceActions . RECENTLY_VISITED ] :
71- parsedResult . resources ?. [ ResourceKindType . devtronApplication ] ?. [
72- UserPreferenceResourceActions . RECENTLY_VISITED
73- ] || ( [ ] as BaseAppMetaData [ ] ) ,
74- }
75- break
76- default :
77- resources = { }
78- }
79- return resources
80- }
81-
8260 return {
8361 pipelineRBACViewSelectedTab,
8462 themePreference :
8563 parsedResult . computedAppTheme === 'system-dark' || parsedResult . computedAppTheme === 'system-light'
8664 ? THEME_PREFERENCE_MAP . auto
8765 : parsedResult . computedAppTheme ,
88- resources : {
89- [ resourceKindType ] : resourceKindType ? getPreferredResourcesData ( ) : { } ,
90- } ,
66+ resources : parsedResult . resources ,
9167 }
9268}
9369/**
@@ -103,6 +79,7 @@ const getUserPreferencePayload = async ({
10379 path,
10480 value,
10581 resourceKind,
82+ userPreferencesResponse,
10683} : UserPathValueMapType ) : Promise < Partial < UserPreferencesPayloadValueType > > => {
10784 switch ( path ) {
10885 case 'themePreference' :
@@ -116,10 +93,24 @@ const getUserPreferencePayload = async ({
11693 value . pipelineRBACViewSelectedTab === ViewIsPipelineRBACConfiguredRadioTabs . ACCESS_ONLY ,
11794 }
11895
119- case 'resources' :
96+ case 'resources' : {
97+ const existingResources = userPreferencesResponse ?. resources || { }
98+
99+ const updatedResources = {
100+ ...existingResources ,
101+ [ resourceKind ] : {
102+ ...existingResources [ resourceKind ] ,
103+ [ UserPreferenceResourceActions . RECENTLY_VISITED ] :
104+ getUserPreferenceResourcesMetadata ( value as BaseAppMetaData [ ] , resourceKind ) [ resourceKind ] ?. [
105+ UserPreferenceResourceActions . RECENTLY_VISITED
106+ ] || [ ] ,
107+ } ,
108+ }
109+
120110 return {
121- resources : getUserPreferenceResourcesMetadata ( value as BaseAppMetaData [ ] , resourceKind ) ,
111+ resources : updatedResources ,
122112 }
113+ }
123114 default :
124115 return { }
125116 }
@@ -130,12 +121,18 @@ export const updateUserPreferences = async ({
130121 value,
131122 resourceKind,
132123 shouldThrowError = false ,
124+ userPreferencesResponse,
133125} : UserPreferenceResourceProps ) : Promise < boolean > => {
134126 try {
135127 const payload : UpdateUserPreferencesPayloadType = {
136128 key : USER_PREFERENCES_ATTRIBUTE_KEY ,
137129 value : JSON . stringify (
138- await getUserPreferencePayload ( { path, value, resourceKind } as UserPathValueMapType ) ,
130+ await getUserPreferencePayload ( {
131+ path,
132+ value,
133+ resourceKind,
134+ userPreferencesResponse,
135+ } as UserPathValueMapType ) ,
139136 ) ,
140137 }
141138
0 commit comments