This commit is contained in:
seb
2026-09-21 03:19:32 +02:00
parent 5594c59d88
commit 366d2517d6
17 changed files with 467 additions and 269 deletions

View File

@@ -1,13 +1,11 @@
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
import { unit as unitApi } from '../api';
import authAutomat, {
AUTH_COMPANY_CHANGED,
AUTH_LOGGED_OUT,
AUTH_SESSION_ESTABLISHED,
} from '../automats/authAutomat';
import { requireArray, requireObject } from '../utils/apiShape';
import {
bootstrapSession,
createCompany,
loginWithGoogle,
logout,
selectCompany,
} from './authSlice';
function sortUnits(units) {
return [...units].sort((a, b) => {
@@ -69,9 +67,8 @@ const unitSlice = createSlice({
.addCase(deleteUnit.fulfilled, (state, action) => {
state.units = state.units.filter((u) => u.si !== action.payload);
})
.addCase(selectCompany.fulfilled, () => initialState)
.addCase(createCompany.fulfilled, () => initialState)
.addCase(logout.fulfilled, () => initialState);
.addCase(AUTH_COMPANY_CHANGED, () => initialState)
.addCase(AUTH_LOGGED_OUT, () => initialState);
},
});
@@ -82,12 +79,9 @@ export const selectUnitsError = (state) => state.unit.error;
export function unitLoadListener(listenerMiddleware) {
listenerMiddleware.startListening({
matcher: (action) =>
bootstrapSession.fulfilled.match(action) ||
loginWithGoogle.fulfilled.match(action) ||
selectCompany.fulfilled.match(action) ||
createCompany.fulfilled.match(action),
action.type === AUTH_SESSION_ESTABLISHED || action.type === AUTH_COMPANY_CHANGED,
effect: async (_action, api) => {
const companyId = api.getState().auth.user?.selectedCompanyId;
const companyId = authAutomat.state.user?.selectedCompanyId;
if (companyId) {
api.dispatch(loadUnits());
}