From 234ad8f22cc75219fd2f34d9655e6d1325c2d2a0 Mon Sep 17 00:00:00 2001 From: Salah Eddine Lalami <50052356+idurar@users.noreply.github.com> Date: Fri, 20 Oct 2023 15:12:47 +0100 Subject: [PATCH] update profile redux store --- frontend/src/redux/profile/actions.js | 110 +++++++----------------- frontend/src/redux/profile/reducer.js | 9 +- frontend/src/redux/profile/selectors.js | 33 +------ 3 files changed, 38 insertions(+), 114 deletions(-) diff --git a/frontend/src/redux/profile/actions.js b/frontend/src/redux/profile/actions.js index e9004e226..af502194a 100644 --- a/frontend/src/redux/profile/actions.js +++ b/frontend/src/redux/profile/actions.js @@ -1,122 +1,72 @@ import * as actionTypes from './types'; import { request } from '@/request'; -const dispatchSettingsData = (datas) => { - const settingsCategory = {}; - - datas.map((data) => { - settingsCategory[data.settingCategory] = { - ...settingsCategory[data.settingCategory], - [data.settingKey]: data.settingValue, - }; - }); - - return settingsCategory; -}; - -export const settingsAction = { +export const profileAction = { resetState: () => (dispatch) => { dispatch({ type: actionTypes.RESET_STATE, }); }, - update: - ({ entity, settingKey, jsonData }) => + currentProfile: + ({ data }) => async (dispatch) => { dispatch({ - type: actionTypes.REQUEST_LOADING, - }); - let data = await request.patch({ - entity: entity + '/updateBySettingKey/' + settingKey, - jsonData, + type: actionTypes.CURRENT_ITEM, + payload: data, }); - - if (data.success === true) { - dispatch({ - type: actionTypes.REQUEST_LOADING, - }); - - let data = await request.listAll({ entity }); - - if (data.success === true) { - const payload = dispatchSettingsData(data.result); - window.localStorage.setItem( - 'settings', - JSON.stringify(dispatchSettingsData(data.result)) - ); - dispatch({ - type: actionTypes.REQUEST_SUCCESS, - payload, - }); - } else { - dispatch({ - type: actionTypes.REQUEST_FAILED, - }); - } - } else { - dispatch({ - type: actionTypes.REQUEST_FAILED, - }); - } }, - updateMany: - ({ entity, jsonData }) => + read: + ({ entity, id }) => async (dispatch) => { dispatch({ type: actionTypes.REQUEST_LOADING, + keyState: 'read', + payload: null, }); - let data = await request.patch({ - entity: entity + '/updateManySetting', - jsonData, - }); + + let data = await request.read({ entity, id }); if (data.success === true) { dispatch({ - type: actionTypes.REQUEST_LOADING, + type: actionTypes.CURRENT_ITEM, + payload: data.result, + }); + dispatch({ + type: actionTypes.REQUEST_SUCCESS, + keyState: 'read', + payload: data.result, }); - - let data = await request.listAll({ entity }); - - if (data.success === true) { - const payload = dispatchSettingsData(data.result); - window.localStorage.setItem( - 'settings', - JSON.stringify(dispatchSettingsData(data.result)) - ); - dispatch({ - type: actionTypes.REQUEST_SUCCESS, - payload, - }); - } else { - dispatch({ - type: actionTypes.REQUEST_FAILED, - }); - } } else { dispatch({ type: actionTypes.REQUEST_FAILED, + keyState: 'read', + payload: null, }); } }, - list: - ({ entity }) => + update: + ({ entity, id, jsonData }) => async (dispatch) => { dispatch({ type: actionTypes.REQUEST_LOADING, + payload: null, }); - let data = await request.listAll({ entity }); + let data = await request.update({ entity, id, jsonData }); if (data.success === true) { - const payload = dispatchSettingsData(data.result); - window.localStorage.setItem('settings', JSON.stringify(dispatchSettingsData(data.result))); dispatch({ type: actionTypes.REQUEST_SUCCESS, - payload, + payload: data.result, + }); + dispatch({ + type: actionTypes.CURRENT_ITEM, + payload: data.result, }); } else { dispatch({ type: actionTypes.REQUEST_FAILED, + payload: null, }); } }, diff --git a/frontend/src/redux/profile/reducer.js b/frontend/src/redux/profile/reducer.js index 97527690f..142a24887 100644 --- a/frontend/src/redux/profile/reducer.js +++ b/frontend/src/redux/profile/reducer.js @@ -1,7 +1,7 @@ import * as actionTypes from './types'; const INITIAL_PROFILE_STATE = { - default_profile: { + informations: { _id: 'defaultProfile', isAdmin: true, name: 'default profile', @@ -10,7 +10,7 @@ const INITIAL_PROFILE_STATE = { role: 'default profile', photo: '', }, - default_profile_settings: {}, + settings: {}, }; const INITIAL_STATE = { @@ -19,7 +19,7 @@ const INITIAL_STATE = { isSuccess: false, }; -const settingsReducer = (state = INITIAL_STATE, action) => { +const profileReducer = (state = INITIAL_STATE, action) => { const { payload = null } = action; switch (action.type) { case actionTypes.RESET_STATE: @@ -35,7 +35,6 @@ const settingsReducer = (state = INITIAL_STATE, action) => { isLoading: false, isSuccess: false, }; - case actionTypes.REQUEST_SUCCESS: return { result: payload, @@ -47,4 +46,4 @@ const settingsReducer = (state = INITIAL_STATE, action) => { } }; -export default settingsReducer; +export default profileReducer; diff --git a/frontend/src/redux/profile/selectors.js b/frontend/src/redux/profile/selectors.js index e49139571..6dcc6b03b 100644 --- a/frontend/src/redux/profile/selectors.js +++ b/frontend/src/redux/profile/selectors.js @@ -1,33 +1,8 @@ import { createSelector } from 'reselect'; -export const selectSettings = (state) => state.settings; +export const selectProfile = (state) => state.profile; -export const selectCurrentSettings = createSelector( - [selectSettings], - (settings) => settings.result -); - -export const selectMoneyFormat = createSelector( - [selectCurrentSettings], - (settings) => settings.money_format_settings -); - -export const selectAppSettings = createSelector( - [selectCurrentSettings], - (settings) => settings.app_settings -); - -export const selectFinanceSettings = createSelector( - [selectCurrentSettings], - (settings) => settings.finance_settings -); - -export const selectCrmSettings = createSelector( - [selectCurrentSettings], - (settings) => settings.crm_settings -); - -export const selectCompanySettings = createSelector( - [selectCurrentSettings], - (settings) => settings.company_settings +export const selectCurrentProfile = createSelector( + [selectProfile], + (profile) => profile.informations );