File size: 1,064 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import axios from "axios";
import { options } from "../utils/Fetch";
const initialState = {
  searchResults: [],
  isLoading: false,
};
const base_url = "https://youtube-v31.p.rapidapi.com";

export const searchById = createAsyncThunk("redux/searchById", async (url) => {
  try {
    const { data } = await axios.get(`${base_url}/${url}`, options);
    return data.items;
  } catch (error) {
    console.log("error in searchById thunk");
  }
});
const searchSlice = createSlice({
  name: "search",
  initialState,
  reducers: {},
  extraReducers: (builder) => {
    builder
      .addCase(searchById.pending, (state) => {
        state.isLoading = true;
      })
      .addCase(searchById.fulfilled, (state, { payload }) => {
        state.searchResults = payload;
        state.isLoading = false;
      })
      .addCase(searchById.rejected, (state) => {
        state.isLoading = false;
        console.log("Search by ID request rejected");
      });
  },
});
export default searchSlice.reducer;