export const getUsers = state => state.users.list;
export const getIsUsersLoaded = state => state.users.list.length > 0
// users.selector.ts
export const getUsers = state => state.users.list;
export const getIsLoaded = state => state.users.isLoaded
export const getIsEmpty = createSelector(
getUsers,
users => users.length === 0,
);
export const getIsLoadedAndEmpty = createSelector(
getIsLoaded,
getIsEmpty,
(isLoaded, isEmpty) => isLoaded && isEmpty,
);
// users.selector.spec.ts
it(‘getIsLoadedAndEmpty should return true if users are loaded and not empty’, () => {
const loadedState = {
...globalStateMock,
users: {
...globalStateMock.users,
isLoaded: true,
users: [...usersMock],
}
}
const actual = getIsLoadedAndEmpty(loadedState);
expect(actual).toBeTruthy();
});
export const checkUsersReady = (isLoaded, isEmpty) => isLoaded
&& isEmpty;
export const getIsLoadedAndEmpty = createSelector(
getIsLoaded,
getIsEmpty,
checkUsersReady,
);
if (token) {
showMain();
} else {
showLogin();
}
// checkout.ts
export interface CheckoutFlow {
shipmentAddressId: number,
shipmentMethodId: number,
paymentMethodId: number,
billingAddressId: number,
warnings: any[], // here should be a type
}
export enum CheckoutFlowStep {
shipment = 'shipment',
shipmentMethod = 'shipmentMethod',
paymentMethod = 'paymentMethod',
billingAddress = 'billingAddress',
}
// checkout.actions.ts
// We need will run this action after every checkout step change to recalculate current checkout step.
export class InvalidateCheckoutStep implements Action {
readonly type = CheckoutAction.InvalidateCheckoutStep;
constructor(public payload: CheckoutFlow) {}
}
// checkout.selector.ts
export function getCurrentCheckoutStep(checkout: CheckoutFlow): CheckoutFlowStep {
...implementation
}
// checkout.selector.spec.ts
describe('getCurrentCheckoutStep', () => {
it('Should return shipment if all fields are empty', () => {
const actual = getCurrentCheckoutStep(mockWithEmptyCheckout);
expect(actual).toBe(CheckoutFlowStep.shipment)
});
it('Should return shipment method if shipment filled and method not', () => {
const actual = getCurrentCheckoutStep(mockWithoutShipmentMethod);
expect(actual).toBe(CheckoutFlowStep.shipment)
});
// Do the same for every possible state;
});
// checkout.effects.ts
@Effect({dispatch: false})
invalidateCheckoutStep$ = this.actions$
.ofType(CheckoutAction.InvalidateCheckoutStep)
.do((action) => {
const step = getCurrentCheckoutStep(action.payload);
const route = mapStepToRoute(step);
this.router.navigate(route);
});