@reduxjs/toolkit#current TypeScript Examples

The following examples show how to use @reduxjs/toolkit#current. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: reducer.ts    From OpenBA-NextGenTV with MIT License 4 votes vote down vote up
alertSlice = createSlice({
  name: 'alert',
  initialState,
  reducers: {
    selectAlert: (state, action) => {
      if (action.payload.alert) {
        state.value = {
          ...state.value,
          selectedAlertId: action.payload.alert?.id,
        };
      }
    },
    openAlertModal: (state) => {
      const selectAlert = state.value.alerts.find(
        (alert) => alert.id === state.value.selectedAlertId
      );

      const openedAlerts = { ...state.value.openedAlerts };

      if (selectAlert && state.value.selectedAlertId) {
        openedAlerts[state.value.selectedAlertId] = selectAlert.latestPublishTime;
      }

      if (selectAlert?.status === AlertState.NEW || selectAlert?.status === AlertState.UPDATE) {
        selectAlert.status = AlertState.ALERT;
      }

      state.value = {
        ...state.value,
        modalAlertId: state.value.selectedAlertId,
        openedAlerts,
      };
    },
    closeAlertModal: (state) => {
      state.value = { ...state.value, modalAlertId: undefined };
    },
    updateAlertsFromAEAT: (state, action) => {
      const alerts = transformAEATtoAlerts(
        action.payload.alertList,
        action.payload.preferAEATMessagesDemo
      );

      const {
        alerts: oldVersionOfAlerts,
        modalAlertId,
        selectedAlertId,
        openedAlerts,
      } = current(state).value;

      const newAlerts = supplementAlerts(oldVersionOfAlerts, alerts);
      const relevantOpenAlerts = actualizeSelectedAlerts(openedAlerts, newAlerts);
      const newAlertsWithNewState = updateStatusToNew(relevantOpenAlerts, alerts);

      const newAlertsWithUpdateState = removeUpdatedStatus(
        relevantOpenAlerts,
        newAlertsWithNewState
      );

      if (selectedAlertId && !newAlerts.some((a) => a.id === selectedAlertId)) {
        state.value.selectedAlertId = newAlerts[0]?.id;
      }

      if (modalAlertId && !newAlerts.some((a) => a.id === modalAlertId)) {
        state.value.modalAlertId = newAlerts[0]?.id;
      }

      if (!selectedAlertId && oldVersionOfAlerts.length === 0) {
        state.value.selectedAlertId = newAlerts[0]?.id;
      }

      state.value.openedAlerts = relevantOpenAlerts;
      state.value.alerts = newAlertsWithUpdateState;
    },
    removeExpiredAlerts: (state) => {
      const now = Date.now();
      const { modalAlertId, selectedAlertId, alerts } = current(state).value;
      const newAlerts = alerts.filter((a) => a.expire > now);

      state.value.alerts = newAlerts;

      if (selectedAlertId && !newAlerts.some((a) => a.id === selectedAlertId)) {
        state.value.selectedAlertId = newAlerts[0]?.id;
      }

      if (modalAlertId && !newAlerts.some((a) => a.id === modalAlertId)) {
        state.value.modalAlertId = newAlerts[0]?.id;
      }
    },
  },

  extraReducers: {
    [getAlertsAsync.fulfilled.toString()]: (state, action) => {
      const { alertFeeds, alerts } = getData(action.payload.alerts);
      const { baseURI } = action.payload;
      const pathToAlert = baseURI ? `${baseURI}/` : '';
      const newAlerts = mapAlerts(alerts, alertFeeds, pathToAlert);
      const oldAlerts = [...state.value.alerts];

      newAlerts.forEach((alert) => {
        const alertStatus = oldAlerts.some(
          ({ id, latestPublishTime, status }) =>
            alert.id === id &&
            (status === AlertState.UPDATE || alert.latestPublishTime !== latestPublishTime)
        )
          ? AlertState.UPDATE
          : AlertState.ALERT;

        return alertStatus;
      });

      state.value.alerts = newAlerts;
      state.value.selectedAlertId = newAlerts[0]?.id;
    },
    [getAlertsAsync.rejected.toString()]: (state, action) => {
      state.value.error = action.payload;
    },
    [getAEATAsync.fulfilled.toString()]: (state, action) => {
      const { alerts } = action.payload;
      const { openedAlerts } = state.value;

      state.value.selectedAlertId = alerts[0]?.id;

      const relevantOpenAlerts = actualizeSelectedAlerts(openedAlerts, alerts);
      const alertsWithNewState = updateStatusToNew(relevantOpenAlerts, alerts);
      const newAlertsWithUpdateState = removeUpdatedStatus(relevantOpenAlerts, alertsWithNewState);

      state.value.openedAlerts = relevantOpenAlerts;
      state.value.alerts = newAlertsWithUpdateState;
    },
    [getAlertNRT.fulfilled.toString()]: (state, action) => {
      const { nrt, alertId, baseURI } = action.payload;
      const { alertFeeds, alerts } = getData(nrt);
      const alert = state.value.alerts.find((item) => item.id === alertId);
      const newVersion = patchAlertsWithData(alert, alertFeeds[0], alerts[0], baseURI);

      state.value.alerts = state.value.alerts.map((item) =>
        item.id === alertId && newVersion ? newVersion : item
      );
    },
  },
})