mirror of
https://github.com/ershisan99/cards-front.git
synced 2025-12-16 12:32:57 +00:00
lesson 2 finished
This commit is contained in:
15
src/common/utils/create-thunk-action.ts
Normal file
15
src/common/utils/create-thunk-action.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { BaseThunkAPI } from "@reduxjs/toolkit/dist/createAsyncThunk"
|
||||
import { AppDispatch, RootState } from "@/app/store"
|
||||
import { thunkTryCatch } from "./"
|
||||
|
||||
export const createThunkAction = <A, R, T>(
|
||||
promise: (arg: A) => Promise<R>,
|
||||
transformPromise?: (arg: R) => T,
|
||||
) => {
|
||||
return (
|
||||
arg: A,
|
||||
thunkAPI: BaseThunkAPI<RootState, any, AppDispatch, unknown>,
|
||||
) => {
|
||||
return thunkTryCatch(thunkAPI, () => promise(arg).then(transformPromise))
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1,3 @@
|
||||
export * from "./create-app-async-thunk"
|
||||
export * from "./thunk-try-catch"
|
||||
export * from "./create-thunk-action"
|
||||
|
||||
@@ -6,23 +6,11 @@ import {
|
||||
RegisterResponse,
|
||||
User,
|
||||
} from "@/features/auth/auth.api"
|
||||
import { createAppAsyncThunk, thunkTryCatch } from "@/common"
|
||||
import { BaseThunkAPI } from "@reduxjs/toolkit/dist/createAsyncThunk"
|
||||
import { AppDispatch, RootState } from "@/app/store"
|
||||
import { createAppAsyncThunk, createThunkAction } from "@/common"
|
||||
|
||||
const THUNK_PREFIXES = {
|
||||
REGISTER: "auth/register",
|
||||
}
|
||||
const createThunkAction = <A, R, T>(
|
||||
promise: (arg: A) => Promise<R>,
|
||||
transformPromise: (arg: R) => T,
|
||||
) => {
|
||||
return (
|
||||
arg: A,
|
||||
thunkAPI: BaseThunkAPI<RootState, any, AppDispatch, unknown>,
|
||||
) => {
|
||||
return thunkTryCatch(thunkAPI, () => promise(arg).then(transformPromise))
|
||||
}
|
||||
LOGIN: "auth/login",
|
||||
}
|
||||
|
||||
const register = createAppAsyncThunk<{ user: RegisterResponse }, RegisterArgs>(
|
||||
@@ -31,7 +19,7 @@ const register = createAppAsyncThunk<{ user: RegisterResponse }, RegisterArgs>(
|
||||
)
|
||||
|
||||
const login = createAppAsyncThunk<{ user: User }, LoginArgs>(
|
||||
"auth/login",
|
||||
THUNK_PREFIXES.LOGIN,
|
||||
createThunkAction(AuthApi.login, (res) => ({ user: res.data })),
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user