From 16c850c0be65d327b4ec776993b0ae06d636ec18 Mon Sep 17 00:00:00 2001 From: vhu-axelor <146069039+vhu-axelor@users.noreply.github.com> Date: Tue, 17 Dec 2024 11:38:39 +0100 Subject: [PATCH] feat: manage company update on AOS (#835) * RM#88098 --- changelogs/unreleased/88098.json | 5 +++++ packages/core/src/auth/api/company-api.js | 21 ++++++++++++++++-- .../molecules/UserCard/UserCard.tsx | 22 +++++++++---------- .../src/auth/features/asyncFunctionsIndex.js | 1 - packages/core/src/auth/features/userSlice.js | 14 +++--------- 5 files changed, 37 insertions(+), 26 deletions(-) create mode 100644 changelogs/unreleased/88098.json diff --git a/changelogs/unreleased/88098.json b/changelogs/unreleased/88098.json new file mode 100644 index 0000000000..02e0dd6c01 --- /dev/null +++ b/changelogs/unreleased/88098.json @@ -0,0 +1,5 @@ +{ + "title": "User active company: manage update on AOS", + "type": "feat", + "packages": "core" +} diff --git a/packages/core/src/auth/api/company-api.js b/packages/core/src/auth/api/company-api.js index a90c9a9a8f..a675e3d85a 100644 --- a/packages/core/src/auth/api/company-api.js +++ b/packages/core/src/auth/api/company-api.js @@ -18,10 +18,27 @@ import {createStandardSearch} from '../../apiProviders'; -export async function searchCompany() { +const createCompanyCriteria = ({companySet}) => { + let criteria = []; + + if (companySet != null) { + criteria.push({ + operator: 'OR', + criteria: companySet.map(({id}) => ({ + fieldName: 'id', + operator: '=', + value: id, + })), + }); + } + + return criteria; +}; + +export async function searchCompany({companySet}) { return createStandardSearch({ model: 'com.axelor.apps.base.db.Company', - criteria: [], + criteria: createCompanyCriteria({companySet}), fieldKey: 'auth_company', numberElementsByPage: null, page: 0, diff --git a/packages/core/src/auth/components/molecules/UserCard/UserCard.tsx b/packages/core/src/auth/components/molecules/UserCard/UserCard.tsx index c2e07974b3..b857b484f4 100644 --- a/packages/core/src/auth/components/molecules/UserCard/UserCard.tsx +++ b/packages/core/src/auth/components/molecules/UserCard/UserCard.tsx @@ -29,7 +29,7 @@ import { useThemeColor, } from '@axelor/aos-mobile-ui'; import {fetchCompanies} from '../../../features/companySlice'; -import {changeActiveCompany} from '../../../features/userSlice'; +import {updateActiveUser} from '../../../features/userSlice'; import { logout, useBinaryImageUri, @@ -49,8 +49,8 @@ const UserCard = ({children, style}) => { const {companyList} = useSelector((state: any) => state.company); useEffect(() => { - dispatch((fetchCompanies as any)()); - }, [dispatch]); + dispatch((fetchCompanies as any)({companySet: user.companySet})); + }, [dispatch, user.companySet]); const displayCompanyPicker = useMemo( () => baseConfig?.enableMultiCompany && canModifyCompany, @@ -58,19 +58,16 @@ const UserCard = ({children, style}) => { ); const updateActiveCompany = useCallback( - company => { + (company: any) => { dispatch( - changeActiveCompany({ - newCompany: { - $version: company?.$version, - code: company?.code, - id: company?.id, - name: company?.name, - }, + (updateActiveUser as any)({ + id: user.id, + version: user.version, + activeCompany: company == null ? null : {id: company.id}, }), ); }, - [dispatch], + [dispatch, user], ); const styles = useMemo(() => { @@ -121,6 +118,7 @@ const UserCard = ({children, style}) => { valueField="id" onValueChange={updateActiveCompany} isValueItem={true} + emptyValue={false} style={styles.picker} /> )} diff --git a/packages/core/src/auth/features/asyncFunctionsIndex.js b/packages/core/src/auth/features/asyncFunctionsIndex.js index dddbc48f1c..555cfdcc46 100644 --- a/packages/core/src/auth/features/asyncFunctionsIndex.js +++ b/packages/core/src/auth/features/asyncFunctionsIndex.js @@ -22,6 +22,5 @@ export {fetchLocalizations} from './localizationSlice'; export { fetchActiveUser, updateActiveUser, - changeActiveCompany, changeDefaultStockLocation, } from './userSlice'; diff --git a/packages/core/src/auth/features/userSlice.js b/packages/core/src/auth/features/userSlice.js index a08f94ebd2..69431cfe36 100644 --- a/packages/core/src/auth/features/userSlice.js +++ b/packages/core/src/auth/features/userSlice.js @@ -65,9 +65,6 @@ const userSlice = createSlice({ name: 'user', initialState, reducers: { - changeActiveCompany: (state, action) => { - state.user = {...state.user, activeCompany: action.payload.newCompany}; - }, changeDefaultStockLocation: (state, action) => { state.user = { ...state.user, @@ -83,9 +80,7 @@ const userSlice = createSlice({ state.loadingUser = false; state.user = action.payload ?? {}; state.isUser = action.payload != null; - if (state.user?.activeCompany == null) { - state.canModifyCompany = true; - } + state.canModifyCompany = action.payload?.companySet?.length > 1; }); builder.addCase(fetchActiveUser.rejected, (state, action) => { state.loadingUser = false; @@ -93,14 +88,11 @@ const userSlice = createSlice({ }); builder.addCase(updateActiveUser.fulfilled, (state, action) => { state.user = action.payload; - if (state.user.activeCompany == null) { - state.canModifyCompany = true; - } + state.canModifyCompany = action.payload?.companySet?.length > 1; }); }, }); -export const {changeActiveCompany, changeDefaultStockLocation} = - userSlice.actions; +export const {changeDefaultStockLocation} = userSlice.actions; export const userReducer = userSlice.reducer;