wongjiahau/ttap-web

View on GitHub
src/ts/redux/actions/showAlternateSlot.ts

Summary

Maintainability
A
1 hr
Test Coverage
import { ISlotViewModel } from "../../model/slotViewModel";
import { TinySlot } from "../../permutator/tinySlot";
import { getSlotContent } from "../../react/slotView";
import { GetInitial } from "../../util/getInitial";
import { RawSlot } from "./../../model/rawSlot";
import { IMasterState, MasterStateAction } from "./../reducers/masterState";

export class ShowAlternateSlot extends MasterStateAction {
  public constructor(private slot: ISlotViewModel) {
    super();
  }
  public TypeName(): string {
    return "show alternate slot";
  }
  protected GenerateNewState(state: IMasterState): IMasterState {
    if (
      state.TimetableListState.ShowingAlternateSlotOf &&
      this.slot.Uid !== state.TimetableListState.ShowingAlternateSlotOf.Uid
    ) {
      // Why?
      // Refer to https://github.com/wongjiahau/ttap-web/issues/193#issuecomment-763494226
      return state;
    }
    if (
      state.TimetableListState.ShowingAlternateSlotOf &&
      this.slot.Uid === state.TimetableListState.ShowingAlternateSlotOf.Uid
    ) {
      return {
        ...state,
        SnackbarState: {
          ...state.SnackbarState,
          IsOpen: false,
        },
        TimetableListState: {
          ...state.TimetableListState,
          ShowingAlternateSlotOf: null,
        },
      };
    }

    const alternativeSlots = this.slot.AlternativeSlots;

    return {
      ...state,
      SnackbarState: {
        ...state.SnackbarState,
        Message:
          alternativeSlots.length > 0
            ? `Showing alternative slots for ${getSlotContent(this.slot)}`
            : `No alternative slots are available for ${getSlotContent(
                this.slot
              )}`,
        IsOpen: true,
      },
      TimetableListState: {
        ...state.TimetableListState,
        ShowingAlternateSlotOf: this.slot,
      },
    };
  }
}