File size: 1,791 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import axios from "axios";
import { options } from "../utils/Fetch";
const base_url = "https://youtube-v31.p.rapidapi.com";
const initialState = {
  channelVideos: [],
  isLoading: false,
  channelDetails: "",
};
export const getChannelVideos = createAsyncThunk(
  "redux/getChannelDetails",
  async (url) => {
    try {
      const { data } = await axios.get(`${base_url}/${url}`, options);
      return data.items;
    } catch (error) {
      console.log(error);
    }
  }
);
export const getChannelDetails = createAsyncThunk(
  "redux/getChannelVideos",
  async (url) => {
    try {
      const { data } = await axios.get(`${base_url}/${url}`, options);
      return data.items[0];
    } catch (error) {
      console.log("error in getChannelVideos thunk");
    }
  }
);
export const channelSlice = createSlice({
  name: "channel",
  initialState,
  reducers: {},
  extraReducers:(builder)=> {
    builder
      .addCase(getChannelVideos.pending, (state) => {
        state.isLoading = true;
      })
      .addCase(getChannelVideos.fulfilled, (state, { payload }) => {
        state.channelVideos = payload;
        state.isLoading = false;
      })
      .addCase(getChannelVideos.rejected, (state) => {
        console.log("Channel videos request rejected");
      })
      .addCase(getChannelDetails.pending, (state) => {
        state.isLoading = true;
      })
      .addCase(getChannelDetails.fulfilled, (state, { payload }) => {
        state.channelDetails = payload;
        state.isLoading = false;
      })
      .addCase(getChannelDetails.rejected, (state) => {
        console.log("Channel details request rejected");
        state.isLoading = false;
      });
  },
});

export default channelSlice.reducer;