99 lines
3.3 KiB
JavaScript
99 lines
3.3 KiB
JavaScript
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';
|
|
|
|
export const loadCurrentCompany = createAsyncThunk(
|
|
'company/loadCurrent',
|
|
async () => {
|
|
const companyId = authAutomat.state.user?.selectedCompanyId;
|
|
if (!companyId) return null;
|
|
const data = await company.getById(companyId);
|
|
return normalizeCompany(data.company);
|
|
},
|
|
);
|
|
|
|
export const updateCompany = createAsyncThunk(
|
|
'company/update',
|
|
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);
|
|
return normalizeCompany(data.company, payload);
|
|
},
|
|
);
|
|
|
|
const initialState = {
|
|
current: null,
|
|
status: 'idle',
|
|
error: null,
|
|
};
|
|
|
|
const companySlice = createSlice({
|
|
name: 'company',
|
|
initialState,
|
|
reducers: {},
|
|
extraReducers: (builder) => {
|
|
builder
|
|
.addCase(loadCurrentCompany.pending, (state) => {
|
|
state.status = 'loading';
|
|
state.error = null;
|
|
})
|
|
.addCase(loadCurrentCompany.fulfilled, (state, action) => {
|
|
state.status = 'succeeded';
|
|
state.current = action.payload;
|
|
})
|
|
.addCase(loadCurrentCompany.rejected, (state, action) => {
|
|
state.status = 'failed';
|
|
state.error = action.error.message;
|
|
})
|
|
.addCase(updateCompany.pending, (state) => {
|
|
state.status = 'loading';
|
|
state.error = null;
|
|
})
|
|
.addCase(updateCompany.fulfilled, (state, action) => {
|
|
state.status = 'succeeded';
|
|
state.current = action.payload;
|
|
})
|
|
.addCase(updateCompany.rejected, (state, action) => {
|
|
state.status = 'failed';
|
|
state.error = action.error.message;
|
|
})
|
|
.addCase(AUTH_LOGGED_OUT, (state) => {
|
|
state.current = null;
|
|
state.status = 'idle';
|
|
state.error = null;
|
|
});
|
|
},
|
|
});
|
|
|
|
export const selectCurrentCompany = (state) => state.company.current;
|
|
export const selectCompanyId = (state) => state.company.current?.id ?? null;
|
|
export const selectCompanyStatus = (state) => state.company.status;
|
|
export const selectCompanyError = (state) => state.company.error;
|
|
export const selectCompanyName = (state) => state.company.current?.name;
|
|
export const selectCompanyIso639 = (state) => state.company.current?.iso639;
|
|
export const selectCompanyTaxrate = (state) => state.company.current?.taxrate ?? null;
|
|
export const selectCompanyReducedTaxrate = (state) => state.company.current?.reducedtaxrate ?? null;
|
|
export const selectCompanyRootCategoryId = (state) => state.company.current?.rootCategoryId ?? null;
|
|
|
|
export function companyLoadListener(listenerMiddleware) {
|
|
listenerMiddleware.startListening({
|
|
matcher: (action) =>
|
|
action.type === AUTH_SESSION_ESTABLISHED || action.type === AUTH_COMPANY_CHANGED,
|
|
effect: async (_action, api) => {
|
|
const companyId = authAutomat.state.user?.selectedCompanyId;
|
|
if (companyId) {
|
|
api.dispatch(loadCurrentCompany());
|
|
}
|
|
},
|
|
});
|
|
}
|
|
|
|
export default companySlice.reducer;
|