@reduxjs/toolkit#ThunkAction TypeScript Examples

The following examples show how to use @reduxjs/toolkit#ThunkAction. 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: game.ts    From minesweeper with MIT License 6 votes vote down vote up
recursiveUpdate =
  (prevGameField: Field): ThunkAction<void, RootState, unknown, AnyAction> =>
  (dispatch, getState) =>
    setTimeout(() => {
      const { isGameStarted, isTimerRunning, gameField } = getState().game;
      const isTheSameGame = gameField === prevGameField;

      if (isGameStarted && isTimerRunning && isTheSameGame) {
        dispatch(actions.updateTime());
        dispatch(recursiveUpdate(gameField));
      }
    }, 1000)
Example #2
Source File: game.ts    From minesweeper with MIT License 6 votes vote down vote up
runTimer =
  (): ThunkAction<void, RootState, unknown, AnyAction> =>
  (dispatch, getState) => {
    const { isGameStarted, isTimerRunning, gameField } = getState().game;
    if (isGameStarted && !isTimerRunning) {
      dispatch(actions.setTimerActive());
      dispatch(recursiveUpdate(gameField));
    }
  }
Example #3
Source File: homeSlice.ts    From server with MIT License 6 votes vote down vote up
loadMorePosts = (): ThunkAction<void, RootState, unknown, AnyAction> => {
  return async (dispatch, getState) => {
    if (getState().home.loadingMore) {
      return
    }
    dispatch(setLoadingMore())
    const homePosts = getState().home.posts
    const lastPost = homePosts[homePosts.length - 1]
    const newPosts = await api.getHome(lastPost['id'])
    if (newPosts.length !== 0) {
      dispatch(setPosts(homePosts.concat(newPosts)))
    } else {
      dispatch(setNoMore())
    }
    dispatch(unsetLoadingMore())
  }
}
Example #4
Source File: homeSlice.ts    From server with MIT License 6 votes vote down vote up
pollPosts = (): ThunkAction<void, RootState, unknown, AnyAction> => {
  return async (dispatch, getState) => {
    const homePosts = getState().home.posts
    if (homePosts.length === 0 || getState().home.loading || getState().home.polling) {
      return
    }
    dispatch(setPolling())
    const newPosts = await api.pollHome(homePosts[0].id)
    if (newPosts.length !== 0) {
      dispatch(setPosts([...newPosts, ...homePosts]))
    }
    dispatch(unsetPolling())
  }
}
Example #5
Source File: notificationsSlice.ts    From server with MIT License 6 votes vote down vote up
loadMoreNotifications = (): ThunkAction<void, RootState, unknown, AnyAction> => {
  return async (dispatch, getState) => {
    if (getState().notifications.loadingMore) {
      return
    }
    dispatch(setLoadingMore())
    const notifications = getState().notifications.notifications
    const lastNotification = notifications[notifications.length - 1]
    const newNotifications = await api.getNotifications(lastNotification['id'])
    if (newNotifications.length !== 0) {
      dispatch(setNotifications(notifications.concat(newNotifications)))
    }
    dispatch(unsetLoadingMore())
  }
}
Example #6
Source File: notificationsSlice.ts    From server with MIT License 6 votes vote down vote up
pollNotifications = (): ThunkAction<void, RootState, unknown, AnyAction> => {
  return async (dispatch, getState) => {
    const notifications = getState().notifications.notifications
    if (notifications.length === 0 || getState().notifications.loadingMore) {
      return
    }
    dispatch(setPolling())
    const newNotifications = await api.pollNotifications(notifications[0].id)
    if (newNotifications.length !== 0) {
      dispatch(setNotifications([...newNotifications, ...notifications]))
    }
    dispatch(unsetPolling())
  }
}
Example #7
Source File: notificationsSlice.ts    From server with MIT License 6 votes vote down vote up
markNotificationAsRead = (notificationId: string): ThunkAction<void, RootState, unknown, AnyAction> => {
  return async (dispatch, getState) => {
    await api.markNotificationAsRead(notificationId)
    dispatch(setNotifications(getState().notifications.notifications.map(n => {
      if (n.id === notificationId) {
        return {
          ...n, unread: false
        }
      }
      return n
    })))  }
}
Example #8
Source File: notificationsSlice.ts    From server with MIT License 6 votes vote down vote up
markAllNotificationsAsRead= (): ThunkAction<void, RootState, unknown, AnyAction> => {
  return async (dispatch, getState) => {
    await api.markAllNotificationsAsRead()
    dispatch(setNotifications(getState().notifications.notifications.map(n => {
      return {
        ...n,
        unread: false
      }
    })))
  }
}
Example #9
Source File: homeSlice.ts    From server with MIT License 5 votes vote down vote up
loadPosts = (): ThunkAction<void, RootState, unknown, AnyAction> => {
  return async (dispatch) => {
    dispatch(setPosts(await api.getHome()))
    dispatch(unsetLoading())
  }
}
Example #10
Source File: meSlice.ts    From server with MIT License 5 votes vote down vote up
loadMe = (): ThunkAction<void, RootState, unknown, AnyAction> => {
  return async (dispatch) => {
    dispatch(setLoading())
    dispatch(setMe(await api.getMe()))
    dispatch(unsetLoading())
  }
}
Example #11
Source File: notificationsSlice.ts    From server with MIT License 5 votes vote down vote up
loadNotifications = (): ThunkAction<void, RootState, unknown, AnyAction> => {
  return async (dispatch) => {
    dispatch(setNotifications(await api.getNotifications()))
    dispatch(unsetLoading())
  }
}