0% found this document useful (0 votes)
15 views6 pages

Slices

The document defines Redux slices for authentication, courses, viewing courses, cart, and profile. Each slice initializes state, defines reducer functions to update state, and exports actions and the reducer. The auth slice manages login data. The course slice manages steps, course data, and editing. The view course slice tracks course sections and lectures. The cart slice handles adding/removing items and totals. And the profile slice stores user profile information.

Uploaded by

Sameer Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

Slices

The document defines Redux slices for authentication, courses, viewing courses, cart, and profile. Each slice initializes state, defines reducer functions to update state, and exports actions and the reducer. The auth slice manages login data. The course slice manages steps, course data, and editing. The view course slice tracks course sections and lectures. The cart slice handles adding/removing items and totals. And the profile slice stores user profile information.

Uploaded by

Sameer Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

StudyByte\src\slices\authSlice.

jsx

1 import { createSlice } from "@reduxjs/toolkit";


2
3 const initialState = {
4 signupData: null,
5 loading: false,
6 token: localStorage.getItem("token") ? JSON.parse(localStorage.getItem("token")) : null,
7 };
8
9 const authSlice = createSlice({
10 name: "auth",
11 initialState: initialState,
12 reducers: {
13 setSignupData(state, value) {
14 state.signupData = value.payload;
15 },
16 setLoading(state, value) {
17 state.loading = value.payload;
18 },
19 setToken(state, value) {
20 state.token = value.payload;
21 },
22 },
23 });
24
25 export const { setSignupData, setLoading, setToken } = authSlice.actions;
26
27 export default authSlice.reducer;
StudyByte\src\slices\courseSlice.jsx

1 import { createSlice } from "@reduxjs/toolkit"


2
3 const initialState = {
4 step: 1,
5 course: null,
6 editCourse: false,
7 paymentLoading: false,
8 }
9
10 const courseSlice = createSlice({
11 name: "course",
12 initialState,
13 reducers: {
14 setStep: (state, action) => {
15 state.step = action.payload
16 },
17 setCourse: (state, action) => {
18 state.course = action.payload
19 },
20 setEditCourse: (state, action) => {
21 state.editCourse = action.payload
22 },
23 setPaymentLoading: (state, action) => {
24 state.paymentLoading = action.payload
25 },
26 resetCourseState: (state) => {
27 state.step = 1
28 state.course = null
29 state.editCourse = false
30 },
31 },
32 })
33
34 export const {
35 setStep,
36 setCourse,
37 setEditCourse,
38 setPaymentLoading,
39 resetCourseState,
40 } = courseSlice.actions
41
42 export default courseSlice.reducer
StudyByte\src\slices\viewCourseSlice.js

1 import { createSlice } from "@reduxjs/toolkit"


2
3 const initialState = {
4 courseSectionData: [],
5 courseEntireData: [],
6 completedLectures: [],
7 totalNoOfLectures: 0,
8 }
9
10 const viewCourseSlice = createSlice({
11 name: "viewCourse",
12 initialState,
13 reducers: {
14 setCourseSectionData: (state, action) => {
15 state.courseSectionData = action.payload
16 },
17 setEntireCourseData: (state, action) => {
18 state.courseEntireData = action.payload
19 },
20 setTotalNoOfLectures: (state, action) => {
21 state.totalNoOfLectures = action.payload
22 },
23 setCompletedLectures: (state, action) => {
24 state.completedLectures = action.payload
25 },
26 updateCompletedLectures: (state, action) => {
27 state.completedLectures = [...state.completedLectures, action.payload]
28 },
29 },
30 })
31
32 export const {
33 setCourseSectionData,
34 setEntireCourseData,
35 setTotalNoOfLectures,
36 setCompletedLectures,
37 updateCompletedLectures,
38 } = viewCourseSlice.actions
39
40 export default viewCourseSlice.reducer
StudyByte\src\slices\cartSlice.jsx

1 import { createSlice } from "@reduxjs/toolkit"


2 import { toast } from "react-hot-toast"
3
4 const initialState = {
5 cart: localStorage.getItem("cart")
6 ? JSON.parse(localStorage.getItem("cart"))
7 : [],
8 total: localStorage.getItem("total")
9 ? JSON.parse(localStorage.getItem("total"))
10 : 0,
11 totalItems: localStorage.getItem("totalItems")
12 ? JSON.parse(localStorage.getItem("totalItems"))
13 : 0,
14 }
15
16 const cartSlice = createSlice({
17 name: "cart",
18 initialState,
19 reducers: {
20 addToCart: (state, action) => {
21 const course = action.payload
22 console.log("courrse payloed",course)
23 const index = state.cart.findIndex((item) => item._id === course._id)
24
25 if (index >= 0) {
26 // If the course is already in the cart, do not modify the quantity
27 toast.error("Course already in cart")
28 return
29 }
30 // If the course is not in the cart, add it to the cart
31 state.cart.push(course)
32 // Update the total quantity and price
33 state.totalItems++
34 state.total += course.price
35 // Update to localstorage
36 localStorage.setItem("cart", JSON.stringify(state.cart))
37 localStorage.setItem("total", JSON.stringify(state.total))
38 localStorage.setItem("totalItems", JSON.stringify(state.totalItems))
39 // show toast
40 toast.success("Course added to cart")
41 },
42 removeFromCart: (state, action) => {
43 const courseId = action.payload
44 const index = state.cart.findIndex((item) => item._id === courseId)
45
46 if (index >= 0) {
47 // If the course is found in the cart, remove it
48 state.totalItems--
49 state.total -= state.cart[index].price
50 state.cart.splice(index, 1)
51 // Update to localstorage
52 localStorage.setItem("cart", JSON.stringify(state.cart))
53 localStorage.setItem("total", JSON.stringify(state.total))
54 localStorage.setItem("totalItems", JSON.stringify(state.totalItems))
55 // show toast
56 toast.success("Course removed from cart")
57 }
58 },
59 resetCart: (state) => {
60 state.cart = []
61 state.total = 0
62 state.totalItems = 0
63 // Update to localstorage
64 localStorage.removeItem("cart")
65 localStorage.removeItem("total")
66 localStorage.removeItem("totalItems")
67 },
68 },
69 })
70
71 export const { addToCart, removeFromCart, resetCart } = cartSlice.actions
72
73 export default cartSlice.reducer
74  
StudyByte\src\slices\profileSlice.js

1 import {createSlice} from "@reduxjs/toolkit"


2
3 const initialState = {
4 user:localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : null,
5 loading:false
6 };
7
8 const profileSlice = createSlice({
9 name:"profile",
10 initialState: initialState,
11 reducers: {
12 setUser(state, value) {
13 state.user = value.payload;
14 },
15 setLoading(state, value) {
16 state.loading = value.payload;
17 },
18 },
19 });
20
21 export const {setUser,setLoading} = profileSlice.actions;
22 export default profileSlice.reducer;

You might also like