Advertisement
sentient

Untitled

Jan 26th, 2021
1,079
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {
  2.     createAsyncThunk,
  3.     createSlice,
  4.     PayloadAction,
  5.   } from "@reduxjs/toolkit";
  6. import axios from "axios";
  7. import { Result, Show } from "../../model/Search/tvmaze";
  8. import { MazeState } from "../../model/Slice";
  9.  
  10.   const initialState: MazeState = {
  11.     data : [],
  12.     loading: false,
  13.     errors: "",
  14.   }
  15.  
  16.  
  17.   const mazeSlice = createSlice({
  18.     name: "search",
  19.     initialState,
  20.     reducers: {
  21.       setLoading: (state, { payload }: PayloadAction<boolean>) => {
  22.         state.loading = payload
  23.       },
  24.  
  25.       setErrors: (state, { payload }: PayloadAction<string>) => {
  26.         state.errors = payload;
  27.       },
  28.  
  29.       setResult: (state, { payload } : PayloadAction<Result>) => {
  30.         state.data.push(payload);
  31.       },
  32.     },
  33.   });
  34.  
  35.   export const { setLoading,setErrors,setResult } = mazeSlice.actions;
  36.  
  37.   export const fetchtv = createAsyncThunk(
  38.     'film/search',
  39.     // if you type your function argument here
  40.     async (text: string,thunkAPI) => {
  41.     try {
  42.       thunkAPI.dispatch(setLoading(true));
  43.       const response = await axios.get<Result>(`http://api.tvmaze.com/search/shows?q=${text}`);
  44.       thunkAPI.dispatch(setLoading(false));
  45.       thunkAPI.dispatch(setResult(response.data));
  46.       return response;
  47.     } catch (error) {
  48.         thunkAPI.dispatch(setErrors(error));
  49.         thunkAPI.dispatch(setLoading(false));
  50.     }
  51.     }
  52.   );
  53.  
  54.   export const mazeReducer = mazeSlice.reducer;
  55.  
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement