import { createReducer, on, Action, createFeatureSelector, createSelector, MemoizedSelector, DefaultProjectorFn, } from '@ngrx/store'; import * as companyActions from './company.actions'; import * as userActions from './user.actions'; import { MwCorePreferenceModel } from '../models/preference.model'; import { NamingTypeModel } from '../models/naming-type.model'; export interface State { preference: MwCorePreferenceModel | null; preferenceReady: boolean; useDecimalFormatForHours: boolean; namingType: NamingTypeModel; featureFlags: { [key: string]: boolean }; featureFlagsStates: { [key: string]: boolean }; } export const initialState: State = { preference: null, preferenceReady: false, useDecimalFormatForHours: false, namingType: new NamingTypeModel(), featureFlags: {}, featureFlagsStates: {}, }; const companyReducers = createReducer( initialState, on(userActions.setActiveCompany, (state, action) => ({ ...state, preference: null, preferenceReady: false, featureFlags: {}, featureFlagsStates: {}, })), on(companyActions.companySettingsReceived, (state, action) => ({ ...state, useDecimalFormatForHours: action.settings.useDecimalFormatForHours, namingType: new NamingTypeModel({ project: action.settings.projectNamingType, order: action.settings.orderNamingType, customer: action.settings.customerNamingType, }), })), on(companyActions.preferenceReceived, (state, action) => ({ ...state, preference: new MwCorePreferenceModel(action.data), preferenceReady: true, })), on(companyActions.preferenceSaved, (state, action) => ({ ...state, preference: new MwCorePreferenceModel(action.data), })), on(companyActions.requestFeatureFlagState, (state, action) => ({ ...state, featureFlagsStates: Object.assign({}, state.featureFlagsStates, { [action.key]: false, }), })), on(companyActions.featureFlagReceived, (state, action) => ({ ...state, featureFlagsStates: Object.assign({}, state.featureFlagsStates, { [action.key]: true, }), featureFlags: Object.assign({}, state.featureFlags, { [action.key]: action.isEnabled, }), })) ); export function reducer(state: State | undefined, action: Action): State { return companyReducers(state, action); } export const companyReducer = reducer; export const companyFeatureKey = 'general-company'; export const companyState = createFeatureSelector(companyFeatureKey); export const useDecimalFormatForHours = createSelector( companyState, (state) => state.useDecimalFormatForHours ); export const namingType = createSelector( companyState, (state) => state.namingType ); export const preference = createSelector( companyState, (state) => state.preference ); export const isPreferenceReady = createSelector( companyState, (state) => state.preferenceReady ); export function getFeatureFlagState( key: string ): MemoizedSelector> { return createSelector(companyState, (state) => state.featureFlags[key]); } export const getFeatureFlags = createSelector( companyState, (state) => state.featureFlags ); export const areFeatureFlagsReady = createSelector(companyState, (state) => { return !Object.values(state.featureFlagsStates).includes(false); });