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,18 +1,16 @@
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
import { company } from '../api';
import authAutomat, {
AUTH_COMPANY_CHANGED,
AUTH_LOGGED_OUT,
AUTH_SESSION_ESTABLISHED,
} from '../automats/authAutomat';
import { normalizeCompany } from '../utils/companyForm';
import {
bootstrapSession,
createCompany,
loginWithGoogle,
logout,
selectCompany,
} from './authSlice';
export const loadCurrentCompany = createAsyncThunk(
'company/loadCurrent',
async (_, { getState }) => {
const companyId = getState().auth.user?.selectedCompanyId;
async () => {
const companyId = authAutomat.state.user?.selectedCompanyId;
if (!companyId) return null;
const data = await company.getById(companyId);
return normalizeCompany(data.company);
@@ -21,8 +19,8 @@ export const loadCurrentCompany = createAsyncThunk(
export const updateCompany = createAsyncThunk(
'company/update',
async (payload, { getState }) => {
const companyId = getState().auth.user?.selectedCompanyId;
async (payload) => {
const companyId = authAutomat.state.user?.selectedCompanyId;
if (!companyId) throw new Error('Kein Unternehmen ausgewählt.');
await company.update(companyId, payload);
const data = await company.getById(companyId);
@@ -66,7 +64,7 @@ const companySlice = createSlice({
state.status = 'failed';
state.error = action.error.message;
})
.addCase(logout.fulfilled, (state) => {
.addCase(AUTH_LOGGED_OUT, (state) => {
state.current = null;
state.status = 'idle';
state.error = null;
@@ -87,12 +85,9 @@ export const selectCompanyRootCategoryId = (state) => state.company.current?.roo
export function companyLoadListener(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(loadCurrentCompany());
}