{"size": 3523, "ext": "js", "max_stars_repo_path": "src/App/DFSP/Endpoints/Egress/reducers.js", "max_stars_repo_name": "kleyow/connection-manager-ui", "max_stars_repo_stars_event_min_datetime": "2020-01-02T22:08:26.000Z", "max_stars_repo_stars_event_max_datetime": "2020-01-02T22:08:26.000Z", "max_issues_repo_path": "src/App/DFSP/Endpoints/Egress/reducers.js", "max_issues_repo_name": "kleyow/connection-manager-ui", "max_issues_repo_issues_event_min_datetime": "2019-11-05T09:27:23.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-26T17:24:07.000Z", "max_forks_repo_path": "src/App/DFSP/Endpoints/Egress/reducers.js", "max_forks_repo_name": "kleyow/connection-manager-ui", "max_forks_repo_forks_event_min_datetime": "2020-01-02T22:08:29.000Z", "max_forks_repo_forks_event_max_datetime": "2021-12-21T21:04:46.000Z"}
{"max_stars_count": 1, "max_issues_count": 16, "max_forks_count": 3, "avg_line_length": 26.2910447761, "max_line_length": 93, "alphanum_fraction": 0.5455577633}
import { handleActions } from 'redux-actions';
import isEqual from 'lodash/isEqual';
import { STATES } from '../constants';
import {
  RESET_DFSP_EGRESS,
  SET_DFSP_EGRESS_IPS,
  SET_DFSP_EGRESS_ERROR,
  CHANGE_DFSP_EGRESS_ADDRESS,
  CHANGE_DFSP_EGRESS_PORT,
  ADD_DFSP_EGRESS_IP,
  REMOVE_DFSP_EGRESS_IP,
  ADD_DFSP_EGRESS_PORT,
  REMOVE_DFSP_EGRESS_PORT,
  UNDO_DFSP_EGRESS_CHANGES,
} from './actions';

const initialEgressIp = {
  id: undefined,
  state: STATES.UNSET,
  address: undefined,
  ports: [undefined],
};

const initialState = {
  previousEgressIps: [initialEgressIp],
  egressIps: [initialEgressIp],
  egressError: undefined,
};

const Egress = handleActions(
  {
    [RESET_DFSP_EGRESS]: () => initialState,
    [SET_DFSP_EGRESS_IPS]: (state, action) => {
      // we should set an empty ip structure
      // if the dfsp has no ips set
      let ips = action.payload;
      if (isEqual(ips, [])) {
        ips = [initialEgressIp];
      }
      return {
        ...state,
        previousEgressIps: ips,
        egressIps: ips,
      };
    },
    [SET_DFSP_EGRESS_ERROR]: (state, action) => ({
      ...state,
      egressError: action.payload,
    }),
    [CHANGE_DFSP_EGRESS_ADDRESS]: (state, action) => {
      const { address, index } = action.payload;
      return {
        ...state,
        egressIps: [
          ...state.egressIps.slice(0, index),
          {
            ...state.egressIps[index],
            address,
          },
          ...state.egressIps.slice(index + 1),
        ],
      };
    },
    [CHANGE_DFSP_EGRESS_PORT]: (state, action) => {
      const { port, index, portIndex } = action.payload;
      return {
        ...state,
        egressIps: [
          ...state.egressIps.slice(0, index),
          {
            ...state.egressIps[index],
            ports: [
              ...state.egressIps[index].ports.slice(0, portIndex),
              port,
              ...state.egressIps[index].ports.slice(portIndex + 1),
            ],
          },
          ...state.egressIps.slice(index + 1),
        ],
      };
    },
    [ADD_DFSP_EGRESS_IP]: state => ({
      ...state,
      egressIps: [...state.egressIps, initialEgressIp],
    }),
    [REMOVE_DFSP_EGRESS_IP]: (state, action) => {
      const index = action.payload;
      return {
        ...state,
        egressIps: [...state.egressIps.slice(0, index), ...state.egressIps.slice(index + 1)],
      };
    },
    [ADD_DFSP_EGRESS_PORT]: (state, action) => {
      const index = action.payload;
      return {
        ...state,
        egressIps: [
          ...state.egressIps.slice(0, index),
          {
            ...state.egressIps[index],
            ports: [...state.egressIps[index].ports, undefined],
          },
          ...state.egressIps.slice(index + 1),
        ],
      };
    },
    [REMOVE_DFSP_EGRESS_PORT]: (state, action) => {
      const { portIndex, index } = action.payload;
      return {
        ...state,
        egressIps: [
          ...state.egressIps.slice(0, index),
          {
            ...state.egressIps[index],
            ports: [
              ...state.egressIps[index].ports.slice(0, portIndex),
              ...state.egressIps[index].ports.slice(portIndex + 1),
            ],
          },
          ...state.egressIps.slice(index + 1),
        ],
      };
    },
    [UNDO_DFSP_EGRESS_CHANGES]: state => ({
      ...state,
      egressIps: [...state.previousEgressIps],
    }),
  },
  initialState
);

export default Egress;
export { initialState, initialEgressIp };
