File size: 1,712 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 |
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 = {
videoDetails: {},
isLoading: false,
relatedVideos: [],
};
export const getVideoDetails = createAsyncThunk(
"redux/getVideoDetails",
async (url) => {
try {
const { data } = await axios.get(`${base_url}/${url}`, options);
return data.items[0];
} catch (error) {
console.log("error in getVideoDetails thunk");
}
}
);
export const getRelatedVideos = createAsyncThunk(
"redux/getRelatedVideos",
async (url) => {
try {
const { data } = await axios.get(`${base_url}/${url}`, options);
return data.items;
} catch (error) {
console.log("error in getRelatedVideos thunk");
}
}
);
const videoSlice = createSlice({
name: "video",
initialState,
reducers: {},
extraReducers: (builder) => {
builder
.addCase(getVideoDetails.pending, (state) => {
state.isLoading = true;
})
.addCase(getVideoDetails.fulfilled, (state, { payload }) => {
state.videoDetails = payload;
state.isLoading = false;
})
.addCase(getVideoDetails.rejected, (state) => {
state.isLoading = false;
})
.addCase(getRelatedVideos.pending, (state) => {
state.isLoading = true;
})
.addCase(getRelatedVideos.fulfilled, (state, { payload }) => {
state.relatedVideos = payload;
state.isLoading = false;
})
.addCase(getRelatedVideos.rejected, (state) => {
state.isLoading = false;
});
},
});
export default videoSlice.reducer;
|