Skip to content

Commit

Permalink
update profile redux store
Browse files Browse the repository at this point in the history
  • Loading branch information
Salah Eddine Lalami committed Oct 20, 2023
1 parent 770e4cb commit 234ad8f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 114 deletions.
110 changes: 30 additions & 80 deletions frontend/src/redux/profile/actions.js
Original file line number Diff line number Diff line change
@@ -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,
});
}
},
Expand Down
9 changes: 4 additions & 5 deletions frontend/src/redux/profile/reducer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as actionTypes from './types';

const INITIAL_PROFILE_STATE = {
default_profile: {
informations: {
_id: 'defaultProfile',
isAdmin: true,
name: 'default profile',
Expand All @@ -10,7 +10,7 @@ const INITIAL_PROFILE_STATE = {
role: 'default profile',
photo: '<PHOTO>',
},
default_profile_settings: {},
settings: {},
};

const INITIAL_STATE = {
Expand All @@ -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:
Expand All @@ -35,7 +35,6 @@ const settingsReducer = (state = INITIAL_STATE, action) => {
isLoading: false,
isSuccess: false,
};

case actionTypes.REQUEST_SUCCESS:
return {
result: payload,
Expand All @@ -47,4 +46,4 @@ const settingsReducer = (state = INITIAL_STATE, action) => {
}
};

export default settingsReducer;
export default profileReducer;
33 changes: 4 additions & 29 deletions frontend/src/redux/profile/selectors.js
Original file line number Diff line number Diff line change
@@ -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
);

0 comments on commit 234ad8f

Please sign in to comment.