Merge pull request 'Feat/api logic' (#1) from feat/api-logic into dev
Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
commit
6ef1e381e4
4 files changed
+83
-2
No files matched your search
@@ -3,3 +3,8 @@ export interface Request{
|
|||||||
text: string;
|
text: string;
|
||||||
files: File[];
|
files: File[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface Response{
|
||||||
|
test: string;
|
||||||
|
files: File[];
|
||||||
|
}
|
||||||
@@ -1 +1,16 @@
|
|||||||
|
import apiClient from "../config/client";
|
||||||
|
import { Request, Response } from "../model/common";
|
||||||
|
|
||||||
|
export async function createRequest(data: Request): Promise<Response> {
|
||||||
|
const formData = new FormData();
|
||||||
|
|
||||||
|
formData.append("id", data.id.toString());
|
||||||
|
formData.append("text", data.text);
|
||||||
|
|
||||||
|
data.files.forEach((file) => {
|
||||||
|
formData.append("files", file);
|
||||||
|
});
|
||||||
|
|
||||||
|
const response = await apiClient.post<Response>("/request", formData);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
@@ -1 +1,62 @@
|
|||||||
|
import {Request, Response} from '../../model/common';
|
||||||
|
import {createAsyncThunk, createSlice, PayloadAction} from '@reduxjs/toolkit';
|
||||||
|
import {createRequest} from '../../services/service';
|
||||||
|
|
||||||
|
interface RequestState {
|
||||||
|
files: File[];
|
||||||
|
text: string;
|
||||||
|
loading: boolean;
|
||||||
|
error: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const initialState: RequestState = {
|
||||||
|
files: [],
|
||||||
|
text: '',
|
||||||
|
loading: false,
|
||||||
|
error: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
export const request = createAsyncThunk(
|
||||||
|
'request/create',
|
||||||
|
async (data: Request, {rejectWithValue}) => {
|
||||||
|
try{
|
||||||
|
const response = await createRequest(data);
|
||||||
|
return response;
|
||||||
|
} catch(error: any){
|
||||||
|
if(error.response?.data?.detail){
|
||||||
|
return rejectWithValue(error.response.data.detail);
|
||||||
|
} else {
|
||||||
|
return rejectWithValue(error instanceof Error ? error.message : "Ошибка создания запроса");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
export const requestSlice = createSlice({
|
||||||
|
name:'requests',
|
||||||
|
initialState,
|
||||||
|
reducers: {
|
||||||
|
clearRequestError(state){
|
||||||
|
state.error = null;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
extraReducers: (builder) => {
|
||||||
|
builder.addCase(request.pending, (state) => {
|
||||||
|
state.loading = true;
|
||||||
|
state.error = null;
|
||||||
|
})
|
||||||
|
builder.addCase(request.fulfilled, (state, action: PayloadAction<Response>) => {
|
||||||
|
state.loading = false;
|
||||||
|
state.error = null;
|
||||||
|
state.files = action.payload.files;
|
||||||
|
})
|
||||||
|
builder.addCase(request.rejected, (state, action) => {
|
||||||
|
state.loading = false;
|
||||||
|
state.error = (action.payload as string) || action.error.message || "Неизвестная ошибка";
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
export const {clearRequestError} = requestSlice.actions;
|
||||||
|
|
||||||
|
export default requestSlice.reducer;
|
||||||
+2
-2
@@ -1,9 +1,9 @@
|
|||||||
import { configureStore } from '@reduxjs/toolkit'
|
import { configureStore } from '@reduxjs/toolkit'
|
||||||
//import requestReducer from './slices/slice'
|
import requestReducer from './slices/slice'
|
||||||
|
|
||||||
export const store = configureStore({
|
export const store = configureStore({
|
||||||
reducer: {
|
reducer: {
|
||||||
//request: requestReducer
|
request: requestReducer
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user