|
| 1 | +import { get, patch } from '@Common/Api' |
| 2 | +import { ROUTES } from '@Common/Constants' |
| 3 | +import { getUrlWithSearchParams, showError } from '@Common/index' |
| 4 | +import { ResourceKindType, BaseAppMetaData } from '@Shared/index' |
| 5 | +import { THEME_PREFERENCE_MAP, ThemeConfigType, ThemePreferenceType } from '@Shared/Providers/ThemeProvider/types' |
| 6 | +import { USER_PREFERENCES_ATTRIBUTE_KEY } from './constants' |
| 7 | +import { |
| 8 | + UserPreferencesType, |
| 9 | + GetUserPreferencesQueryParamsType, |
| 10 | + GetUserPreferencesParsedDTO, |
| 11 | + ViewIsPipelineRBACConfiguredRadioTabs, |
| 12 | + UserPreferenceResourceType, |
| 13 | + UserPreferenceResourceActions, |
| 14 | + UpdatedUserPreferencesType, |
| 15 | + UserPreferencesPayloadValueType, |
| 16 | + UpdateUserPreferencesPayloadType, |
| 17 | +} from './types' |
| 18 | + |
| 19 | +/** |
| 20 | + * @returns UserPreferencesType |
| 21 | + * @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. |
| 22 | + * @throws Will throw an error if the request fails or if the result is not in the expected format. |
| 23 | + */ |
| 24 | +export const getUserPreferences = async (): Promise<UserPreferencesType> => { |
| 25 | + const queryParamsPayload: Pick<GetUserPreferencesQueryParamsType, 'key'> = { |
| 26 | + key: USER_PREFERENCES_ATTRIBUTE_KEY, |
| 27 | + } |
| 28 | + |
| 29 | + const { result } = await get<{ value: string }>( |
| 30 | + getUrlWithSearchParams(`${ROUTES.ATTRIBUTES_USER}/${ROUTES.GET}`, queryParamsPayload), |
| 31 | + ) |
| 32 | + |
| 33 | + if (!result?.value) { |
| 34 | + return null |
| 35 | + } |
| 36 | + |
| 37 | + const parsedResult: GetUserPreferencesParsedDTO = JSON.parse(result.value) |
| 38 | + |
| 39 | + const pipelineRBACViewSelectedTab = parsedResult.viewPermittedEnvOnly |
| 40 | + ? ViewIsPipelineRBACConfiguredRadioTabs.ACCESS_ONLY |
| 41 | + : ViewIsPipelineRBACConfiguredRadioTabs.ALL_ENVIRONMENTS |
| 42 | + |
| 43 | + return { |
| 44 | + pipelineRBACViewSelectedTab, |
| 45 | + themePreference: |
| 46 | + parsedResult.computedAppTheme === 'system-dark' || parsedResult.computedAppTheme === 'system-light' |
| 47 | + ? THEME_PREFERENCE_MAP.auto |
| 48 | + : parsedResult.computedAppTheme, |
| 49 | + resources: parsedResult.resources, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +export const resourceTypes: ResourceKindType[] = [ResourceKindType.devtronApplication] |
| 54 | + |
| 55 | +const resourcesObj = (recentlyVisited: BaseAppMetaData[]): UserPreferenceResourceType => |
| 56 | + resourceTypes.reduce((acc, resource) => { |
| 57 | + acc[resource] = { |
| 58 | + [UserPreferenceResourceActions.RECENTLY_VISITED]: recentlyVisited, |
| 59 | + } |
| 60 | + return acc |
| 61 | + }, {} as UserPreferenceResourceType) |
| 62 | + |
| 63 | +/** |
| 64 | + * @description This function updates the user preferences in the server. It constructs a payload with the updated user preferences and sends a PATCH request to the server. If the request is successful, it returns true. If an error occurs, it shows an error message and returns false. |
| 65 | + * @param updatedUserPreferences - The updated user preferences to be sent to the server. |
| 66 | + * @param recentlyVisitedDevtronApps - The recently visited Devtron apps to be sent to the server. |
| 67 | + * @param shouldThrowError - A boolean indicating whether to throw an error if the request fails. Default is false. |
| 68 | + * @returns A promise that resolves to true if the request is successful, or false if an error occurs. |
| 69 | + * @throws Will throw an error if `shouldThrowError` is true and the request fails. |
| 70 | + */ |
| 71 | + |
| 72 | +export const updateUserPreferences = async ( |
| 73 | + updatedUserPreferences?: UpdatedUserPreferencesType, |
| 74 | + recentlyVisitedDevtronApps?: BaseAppMetaData[], |
| 75 | + shouldThrowError: boolean = false, |
| 76 | +): Promise<boolean> => { |
| 77 | + try { |
| 78 | + let value: UserPreferencesPayloadValueType = null |
| 79 | + if (updatedUserPreferences) { |
| 80 | + const { themePreference, appTheme, pipelineRBACViewSelectedTab } = updatedUserPreferences |
| 81 | + |
| 82 | + value = { |
| 83 | + viewPermittedEnvOnly: pipelineRBACViewSelectedTab === ViewIsPipelineRBACConfiguredRadioTabs.ACCESS_ONLY, |
| 84 | + computedAppTheme: themePreference === THEME_PREFERENCE_MAP.auto ? `system-${appTheme}` : appTheme, |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if (recentlyVisitedDevtronApps?.length) { |
| 89 | + value = { |
| 90 | + resources: resourcesObj(recentlyVisitedDevtronApps), |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + const payload: UpdateUserPreferencesPayloadType = { |
| 95 | + key: USER_PREFERENCES_ATTRIBUTE_KEY, |
| 96 | + value: JSON.stringify(value), |
| 97 | + } |
| 98 | + |
| 99 | + await patch(`${ROUTES.ATTRIBUTES_USER}/${ROUTES.PATCH}`, payload) |
| 100 | + return true |
| 101 | + } catch (error) { |
| 102 | + if (shouldThrowError) { |
| 103 | + throw error |
| 104 | + } |
| 105 | + |
| 106 | + showError(error) |
| 107 | + return false |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +export type UserPreferencesUpdateType = |
| 112 | + | { type: 'updateTheme'; value: ThemePreferenceType | null; appTheme: ThemeConfigType['appTheme'] } |
| 113 | + | { type: 'updatePipelineRBACView'; value: ViewIsPipelineRBACConfiguredRadioTabs } |
| 114 | + | { type: 'updateRecentlyVisitedApps'; value: BaseAppMetaData[] } |
0 commit comments