import { State } from '../../../reducers'; import { createFeatureSelector, MemoizedSelector } from '@ngrx/store'; import * as petaDestinationsActions from '../actions/peta-destinations.actions'; import { PETADestinationsInterface } from '../models/peta-destinations.interface'; import * as petaActions from '../actions/peta.actions'; import { PETAInterface } from '../models/peta.interface'; export interface PETADestinationsRequestState extends PETADestinationsInterface { loading: boolean; } export const initialStatePETADestinations: PETADestinationsRequestState = { destinations: [{ fulfillmentType: null, postalCode: null }], loading: null }; export interface PETAListRequestState { petaList: PETAInterface[]; loading: boolean; } export const initialPETAListRequestStateState: PETAListRequestState = { petaList: null, loading: null }; export interface PETARequestState extends PETAInterface { loading: boolean; error: boolean; } export const initialPetaState: PETARequestState = { SOURCING_RESULT: { SOURCING_RESULTS: [{ SOURCING_STRATEGY_ID: null, BUSINESS_OBJECTIVE_ID: null, CART_ITEMS: null, ADDITIONAL_INFORMATION: null }], MESSAGES: null, RESERVATION_SUCCESSFUL: null }, loading: null, error: null }; export function petaDestinationsRequestReducer(state = initialStatePETADestinations, action: petaDestinationsActions.PetaDestinationActions): PETADestinationsRequestState { switch (action.type) { case petaDestinationsActions.LOAD_PETA_DESTINATIONS_SUCCESS_ACTION: { return { ...state, ...action.payload, loading: false }; } case petaDestinationsActions.LOAD_PETA_DESTINATIONS_ACTION: { return { ...state, loading: true }; } case petaDestinationsActions.PETA_DESTINATIONS_SET_TO_DEFAULT_ACTION: { return { ...state, ...initialStatePETADestinations, loading: false }; } default: { return state; } } } export function petaListRequestReducer(state = initialPETAListRequestStateState, action: petaActions.PetaActions): PETAListRequestState { switch (action.type) { case petaActions.LOAD_PETA_LIST_LOADING: { return { ...state, loading: action.loading }; } case petaActions.LOAD_PETA_LIST_SUCCESS_ACTION: { return { ...state, petaList: action.payload }; } default: { return state; } } } export function petaRequestReducer(state = initialPetaState, action: petaActions.PetaActions): PETARequestState { switch (action.type) { case petaActions.LOAD_PETA_LOADING: { return { ...state, loading: action.loading }; } case petaActions.LOAD_PETA_SUCCESS_ACTION: { return { ...state, ...action.payload, error: false }; } case petaActions.LOAD_PETA_ACTION: { return { ...state, loading: true }; } case petaActions.LOAD_PETA_ERROR: { return { ...state, error: action.error }; } default: { return state; } } } export interface PetaState extends State { getPETADestinationsRequest: PETADestinationsRequestState; getPETARequest: PETARequestState; getPETAListRequest: PETAListRequestState; } export const petaReducers = { getPETADestinationsRequest: petaDestinationsRequestReducer, getPETARequest: petaRequestReducer, getPETAListRequest: petaListRequestReducer }; export const selectPetaState: MemoizedSelector = createFeatureSelector('peta');