Skip to content

Commit

Permalink
feat: manage company update on AOS (#835)
Browse files Browse the repository at this point in the history
* RM#88098
  • Loading branch information
vhu-axelor authored Dec 17, 2024
1 parent 8d175aa commit 16c850c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 26 deletions.
5 changes: 5 additions & 0 deletions changelogs/unreleased/88098.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"title": "User active company: manage update on AOS",
"type": "feat",
"packages": "core"
}
21 changes: 19 additions & 2 deletions packages/core/src/auth/api/company-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 10 additions & 12 deletions packages/core/src/auth/components/molecules/UserCard/UserCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -49,28 +49,25 @@ 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,
[baseConfig?.enableMultiCompany, canModifyCompany],
);

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(() => {
Expand Down Expand Up @@ -121,6 +118,7 @@ const UserCard = ({children, style}) => {
valueField="id"
onValueChange={updateActiveCompany}
isValueItem={true}
emptyValue={false}
style={styles.picker}
/>
)}
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/auth/features/asyncFunctionsIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,5 @@ export {fetchLocalizations} from './localizationSlice';
export {
fetchActiveUser,
updateActiveUser,
changeActiveCompany,
changeDefaultStockLocation,
} from './userSlice';
14 changes: 3 additions & 11 deletions packages/core/src/auth/features/userSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -83,24 +80,19 @@ 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;
state.isUser = false;
});
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;

0 comments on commit 16c850c

Please sign in to comment.