| /******************************************************************************** |
| * Copyright (c) 2020 Contributors to the Eclipse Foundation |
| * |
| * See the NOTICE file(s) distributed with this work for additional |
| * information regarding copyright ownership. |
| * |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v. 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0. |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| ********************************************************************************/ |
| /** |
| * Every reducer module's default export is the reducer function itself. In |
| * addition, each module should export a type or interface that describes |
| * the state of the reducer plus any selector functions. The `* as` |
| * notation packages up all of the exports into a single object. |
| */ |
| import * as fromSettings from '@shared/store/reducers/settings.reducer'; |
| import * as fromContactsPage from '@shared/store/reducers/contacts/contacts-page.reducer'; |
| import * as fromExternalPersonForm from '@shared/store/reducers/persons/external-person/external-person-details-form.reducer'; |
| import * as fromInternalPersonForm from '@shared/store/reducers/persons/internal-person/internal-person-details-form.reducer'; |
| import { CommunicationType, ExternalPerson, InternalPerson, Company, Salutation, PersonType } from '@shared/models'; |
| import { FormGroupState } from 'ngrx-forms'; |
| import { createFeatureSelector } from '@ngrx/store'; |
| import { createSelector } from 'reselect'; |
| /* admin imports */ |
| import * as fromSalutations from '@app/shared/store/reducers/admin/salutations.reducer'; |
| import * as fromSalutationsDetailForm from '@app/shared/store/reducers/admin/salutations-details-form.reducer'; |
| import * as fromCommunicationTypes from '@app/shared/store/reducers/admin/communication-types.reducer'; |
| import * as fromCommunicationTypesDetailForm from '@app/shared/store/reducers/admin/communication-types-details-form.reducer'; |
| import * as fromCompanyDetailForm from '@shared/store/reducers/company/company-details-form.reducer'; |
| import * as fromPersonTypes from '@shared/store/reducers/admin/person-types.reducer'; |
| import * as fromPersonTypesDetailForm from '@shared/store/reducers/admin/person-types-details-form.reducer'; |
| |
| /** |
| * We treat each reducer like a table in a database. This means |
| * our top level state interface is just a map of keys to inner state types. |
| */ |
| export interface State { |
| settings: fromSettings.State; |
| contactsPage: fromContactsPage.State; |
| } |
| |
| export interface PersonState { |
| externalPersonDetailsForm: FormGroupState<ExternalPerson>; |
| internalPersonDetailsForm: FormGroupState<InternalPerson>; |
| } |
| |
| export interface AdminState { |
| salutations: fromSalutations.State; |
| salutationsDetailForm: FormGroupState<Salutation>; |
| communicationTypes: fromCommunicationTypes.State; |
| communicationTypesDetailForm: FormGroupState<CommunicationType>; |
| personTypes: fromPersonTypes.State; |
| personTypesDetailForm: FormGroupState<PersonType>; |
| } |
| |
| export interface CompanyState { |
| companyDetailsForm: FormGroupState<Company>; |
| } |
| |
| /** |
| * Because metareducers take a reducer function and return a new reducer, |
| * we can use our compose helper to chain them together. Here we are |
| * using combineReducers to make our top level reducer, and then |
| * wrapping that in storeLogger. Remember that compose applies |
| * the result from right to left. |
| */ |
| export const reducers = { |
| settings: fromSettings.reducer, |
| contactsPage: fromContactsPage.reducer, |
| }; |
| |
| export const adminReducers = { |
| salutations: fromSalutations.reducer, |
| salutationsDetailForm: fromSalutationsDetailForm.reducer, |
| communicationTypes: fromCommunicationTypes.reducer, |
| communicationTypesDetailForm: fromCommunicationTypesDetailForm.reducer, |
| personTypes: fromPersonTypes.reducer, |
| personTypesDetailForm: fromPersonTypesDetailForm.reducer |
| }; |
| |
| export const personsReducers = { |
| externalPersonDetailsForm: fromExternalPersonForm.reducer, |
| internalPersonDetailsForm: fromInternalPersonForm.reducer, |
| } |
| |
| export const companyReducers = { |
| companyDetailsForm: fromCompanyDetailForm.reducer, |
| } |
| |
| /** |
| * Every reducer module exports selector functions, however child reducers |
| * have no knowledge of the overall state tree. To make them useable, we |
| * need to make new selectors that wrap them. |
| */ |
| |
| /** |
| * Settings store functions |
| */ |
| export const getSettingsState = (state: State) => state.settings; |
| |
| export const getSelectedLanguage = createSelector(getSettingsState, fromSettings.getSelectedLanguage); |
| export const getSelectedCulture = createSelector(getSettingsState, fromSettings.getSelectedCulture); |
| export const getAvailableLanguages = createSelector(getSettingsState, fromSettings.getAvailableLanguages); |
| |
| /** |
| * Contacts Page store functions |
| */ |
| export const getContactsPageState = (state: State) => state.contactsPage; |
| export const getContactsPageLoaded = createSelector(getContactsPageState, fromContactsPage.getLoaded); |
| export const getContactsPageLoading = createSelector(getContactsPageState, fromContactsPage.getLoading); |
| export const getContactsPageFailed = createSelector(getContactsPageState, fromContactsPage.getFailed); |
| export const getContactsPageData = createSelector(getContactsPageState, fromContactsPage.getData); |
| /** |
| * Settings store functions |
| */ |
| export const getUser = createSelector(getSettingsState, fromSettings.getUser); |
| /** |
| * Persons store functions |
| */ |
| export const selectPersonState = createFeatureSelector<PersonState>('personsData'); |
| |
| export const selectExternalPersonDetails = createSelector( |
| selectPersonState, |
| (state: PersonState) => state.externalPersonDetailsForm |
| ); |
| export const getExternalPersonDetails = createSelector( |
| selectExternalPersonDetails, |
| fromExternalPersonForm.getFormState |
| ); |
| |
| export const selectInternalPersonDetails = createSelector( |
| selectPersonState, |
| (state: PersonState) => state.internalPersonDetailsForm |
| ); |
| export const getInternalPersonDetails = createSelector( |
| selectInternalPersonDetails, |
| fromInternalPersonForm.getFormState |
| ); |
| |
| /** |
| * Company store functions |
| */ |
| export const selectCompanyState = createFeatureSelector<CompanyState>('companyData'); |
| |
| export const selectCompanyDetails = createSelector( |
| selectCompanyState, |
| (state: CompanyState) => state.companyDetailsForm |
| ); |
| export const getCompanyDetails = createSelector( |
| selectCompanyDetails, |
| fromCompanyDetailForm.getFormState |
| ); |
| |
| /** |
| * Admin store functions |
| */ |
| export const selectAdminState = createFeatureSelector<AdminState>('admin'); |
| |
| // salutations list |
| export const selectSalutations = createSelector( |
| selectAdminState, |
| (state: AdminState) => state.salutations |
| ); |
| export const getSalutationsLoaded = createSelector(selectSalutations, fromSalutations.getLoaded); |
| export const getSalutationsLoading = createSelector(selectSalutations, fromSalutations.getLoading); |
| export const getSalutationsFailed = createSelector(selectSalutations, fromSalutations.getFailed); |
| export const getSalutationsData = createSelector(selectSalutations, fromSalutations.getData); |
| // salutations details |
| export const selectSalutationDetails = createSelector( |
| selectAdminState, |
| (state: AdminState) => state.salutationsDetailForm |
| ); |
| export const getSalutationDetails = createSelector( |
| selectSalutationDetails, |
| fromSalutationsDetailForm.getFormState |
| ); |
| // communication types list |
| export const selectCommunicationTypes = createSelector( |
| selectAdminState, |
| (state: AdminState) => state.communicationTypes |
| ); |
| |
| export const getCommunicationTypesLoaded = createSelector(selectCommunicationTypes, fromCommunicationTypes.getLoaded); |
| export const getCommunicationTypesLoading = createSelector(selectCommunicationTypes, fromCommunicationTypes.getLoading); |
| export const getCommunicationTypesFailed = createSelector(selectCommunicationTypes, fromCommunicationTypes.getFailed); |
| export const getCommunicationTypesData = createSelector(selectCommunicationTypes, fromCommunicationTypes.getData); |
| // communication types details |
| export const selectCommunicationTypesDetails = createSelector( |
| selectAdminState, |
| (state: AdminState) => state.communicationTypesDetailForm |
| ); |
| export const getCommunicationTypesDetails = createSelector( |
| selectCommunicationTypesDetails, |
| fromCommunicationTypesDetailForm.getFormState |
| ); |
| // person types list |
| export const selectPersonTypes = createSelector( |
| selectAdminState, |
| (state: AdminState) => state.personTypes |
| ); |
| export const getPersonTypesLoaded = createSelector(selectPersonTypes, fromPersonTypes.getLoaded); |
| export const getPersonTypesLoading = createSelector(selectPersonTypes, fromPersonTypes.getLoading); |
| export const getPersonTypesFailed = createSelector(selectPersonTypes, fromPersonTypes.getFailed); |
| export const getPersonTypesData = createSelector(selectPersonTypes, fromPersonTypes.getData); |
| // person types details |
| export const selectPersonTypesDetails = createSelector( |
| selectAdminState, |
| (state: AdminState) => state.personTypesDetailForm |
| ); |
| export const getPersonTypesDetails = createSelector( |
| selectPersonTypesDetails, |
| fromPersonTypesDetailForm.getFormState |
| ); |