Skip to content

Commit

Permalink
Merge pull request #1415 from Arnei/createAppAsyncThunk
Browse files Browse the repository at this point in the history
Add typing to async thunks
  • Loading branch information
geichelberger authored Dec 17, 2024
2 parents 00bc540 + b618831 commit 2916425
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
14 changes: 14 additions & 0 deletions src/redux/createAsyncThunkWithTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { createAsyncThunk } from "@reduxjs/toolkit";
import { AppDispatch, RootState } from "./store";

/**
* Use instead of createAsyncThunk to provide basic typing to all async thunks
*
* Thematically this belongs in `store.ts`. However, this causes
* "Cannot access 'createAsyncThunk' before initialization",
* so this has to be moved into it's own file.
*/
export const createAppAsyncThunk = createAsyncThunk.withTypes<{
state: RootState
dispatch: AppDispatch
}>();
5 changes: 3 additions & 2 deletions src/redux/metadataSlice.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { createSlice, createAsyncThunk, PayloadAction } from "@reduxjs/toolkit";
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { client } from "../util/client";

import { httpRequestState } from "../types";
import { settings } from "../config";
import { createAppAsyncThunk } from "./createAsyncThunkWithTypes";

export interface Catalog {
fields: MetadataField[],
Expand Down Expand Up @@ -36,7 +37,7 @@ const initialState: metadata & httpRequestState = {
errorReason: "unknown",
};

export const fetchMetadata = createAsyncThunk("metadata/fetchMetadata", async () => {
export const fetchMetadata = createAppAsyncThunk("metadata/fetchMetadata", async () => {
if (!settings.id) {
throw new Error("Missing media package identifier");
}
Expand Down
8 changes: 4 additions & 4 deletions src/redux/subtitleSlice.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Segment, SubtitleCue, SubtitlesInEditor } from "./../types";
import { createAsyncThunk, createSlice, nanoid, PayloadAction } from "@reduxjs/toolkit";
import { createSlice, nanoid, PayloadAction } from "@reduxjs/toolkit";
import { roundToDecimalPlace } from "../util/utilityFunctions";
import { video } from "./videoSlice";
import { createAppAsyncThunk } from "./createAsyncThunkWithTypes";

export interface subtitle {
isDisplayEditView: boolean; // Should the edit view be displayed
Expand Down Expand Up @@ -253,15 +253,15 @@ export const {
* Will grab the state from videoState to skip past deleted segment if preview
* mode is active.
*/
export const setCurrentlyAtAndTriggerPreview = createAsyncThunk("subtitleState/setCurrentlyAtAndTriggerPreview",
export const setCurrentlyAtAndTriggerPreview = createAppAsyncThunk("subtitleState/setCurrentlyAtAndTriggerPreview",
async (milliseconds: number, { getState, dispatch }) => {
milliseconds = roundToDecimalPlace(milliseconds, 0);

if (milliseconds < 0) {
milliseconds = 0;
}

const allStates = getState() as { videoState: video, subtitleState: subtitle; };
const allStates = getState();
const segments: Segment[] = allStates.videoState.segments;
let triggered = false;

Expand Down
5 changes: 3 additions & 2 deletions src/redux/videoSlice.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { clamp } from "lodash";
import { createSlice, nanoid, createAsyncThunk, PayloadAction, createSelector } from "@reduxjs/toolkit";
import { createSlice, nanoid, PayloadAction, createSelector } from "@reduxjs/toolkit";
import { client } from "../util/client";

import { Segment, httpRequestState, Track, Workflow, SubtitlesFromOpencast } from "../types";
import { roundToDecimalPlace } from "../util/utilityFunctions";
import { settings } from "../config";
import { createAppAsyncThunk } from "./createAsyncThunkWithTypes";

export interface video {
isPlaying: boolean, // Are videos currently playing?
Expand Down Expand Up @@ -81,7 +82,7 @@ export const initialState: video & httpRequestState = {
errorReason: "unknown",
};

export const fetchVideoInformation = createAsyncThunk("video/fetchVideoInformation", async () => {
export const fetchVideoInformation = createAppAsyncThunk("video/fetchVideoInformation", async () => {
if (!settings.id) {
throw new Error("Missing media package identifier");
}
Expand Down
5 changes: 3 additions & 2 deletions src/redux/workflowPostSlice.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import { createSlice } from "@reduxjs/toolkit";
import { client } from "../util/client";
import { Segment, PostEditArgument, httpRequestState } from "../types";
import { settings } from "../config";
import { createAppAsyncThunk } from "./createAsyncThunkWithTypes";

const initialState: httpRequestState = {
status: "idle",
Expand All @@ -10,7 +11,7 @@ const initialState: httpRequestState = {
};

export const postVideoInformation =
createAsyncThunk("video/postVideoInformation", async (argument: PostEditArgument) => {
createAppAsyncThunk("video/postVideoInformation", async (argument: PostEditArgument) => {
if (!settings.id) {
throw new Error("Missing media package id");
}
Expand Down

0 comments on commit 2916425

Please sign in to comment.