Skip to content

Commit

Permalink
Merge pull request #728 from Arnei/default-typing-for-createAsyncThunk
Browse files Browse the repository at this point in the history
Default typing for createAsyncThunk
  • Loading branch information
Arnei authored Jul 1, 2024
2 parents 4c8b2d0 + a0609c1 commit ca662a8
Show file tree
Hide file tree
Showing 25 changed files with 200 additions and 178 deletions.
14 changes: 14 additions & 0 deletions src/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
}>()
7 changes: 4 additions & 3 deletions src/slices/aclDetailsSlice.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { PayloadAction, SerializedError, createAsyncThunk, createSlice } from '@reduxjs/toolkit'
import { PayloadAction, SerializedError, createSlice } from '@reduxjs/toolkit'
import axios from 'axios';
import { prepareAccessPolicyRulesForPost } from '../utils/resourceUtils';
import { addNotification } from './notificationSlice';
import { createAppAsyncThunk } from '../createAsyncThunkWithTypes';

/**
* This file contains redux reducer for actions affecting the state of details of an ACL
Expand Down Expand Up @@ -43,7 +44,7 @@ const initialState: AclDetailsState = {
};

// fetch details about a certain acl from server
export const fetchAclDetails = createAsyncThunk('aclDetails/fetchAclDetails', async (aclId: number) => {
export const fetchAclDetails = createAppAsyncThunk('aclDetails/fetchAclDetails', async (aclId: number) => {
const res = await axios.get(`/admin-ng/acl/${aclId}`);

let aclDetails = res.data;
Expand Down Expand Up @@ -128,7 +129,7 @@ export const fetchAclDetails = createAsyncThunk('aclDetails/fetchAclDetails', as
});

// update details of a certain acl
export const updateAclDetails = createAsyncThunk('aclDetails/updateAclDetails', async (params: {
export const updateAclDetails = createAppAsyncThunk('aclDetails/updateAclDetails', async (params: {
values: {
name: string,
acls: TransformedAcls,
Expand Down
8 changes: 4 additions & 4 deletions src/slices/aclSlice.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { PayloadAction, SerializedError, createAsyncThunk, createSlice } from '@reduxjs/toolkit'
import { PayloadAction, SerializedError, createSlice } from '@reduxjs/toolkit'
import { TableConfig, aclsTableConfig } from "../configs/tableConfigs/aclsTableConfig";
import axios from 'axios';
import { getURLParams, prepareAccessPolicyRulesForPost, transformAclTemplatesResponse } from '../utils/resourceUtils';
import { transformToIdValueArray } from '../utils/utils';
import { NOTIFICATION_CONTEXT_ACCESS } from '../configs/modalConfig';
import { addNotification, removeNotificationWizardAccess } from './notificationSlice';
import { AppDispatch } from '../store';
import { RootState } from '../store';
import { createAppAsyncThunk } from '../createAsyncThunkWithTypes';

/**
* This file contains redux reducer for actions affecting the state of acls
Expand Down Expand Up @@ -64,9 +64,9 @@ const initialState: AclsState = {
limit: 0,
};

export const fetchAcls = createAsyncThunk('acls/fetchAcls', async (_, { getState }) => {
export const fetchAcls = createAppAsyncThunk('acls/fetchAcls', async (_, { getState }) => {
const state = getState();
let params = getURLParams(state as RootState);
let params = getURLParams(state);
// Just make the async request here, and return the response.
// This will automatically dispatch a `pending` action first,
// and then `fulfilled` or `rejected` actions based on the promise.
Expand Down
Loading

0 comments on commit ca662a8

Please sign in to comment.