File size: 1,071 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
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { callAPI } from "../utills/callApi";
import config from "../config";

export const fetchUserData = createAsyncThunk("user/fetchUserData", () =>
  callAPI(config.BASE_URL)
);

const userStore = createSlice({
  name: "user",
  initialState: {},
  reducers: {
    setUser: (state, action) => {
      state.token = action.payload.token;
    },
    getUser: (state) => {
      return state.user;
    },
  },
  extraReducers: (builder) => {
    builder.addCase(fetchUserData.pending, (state, action) => {
      state.isLoading = true;
    });
    builder.addCase(fetchUserData.fulfilled, (state, action) => {
      console.log(action.payload);
      state.userUrl = action.payload.external_urls?.spotify || "";
      state.name = action.payload.display_name;
      state.isLoading = false;
    });
    builder.addCase(fetchUserData.rejected, (state, action) => {
      state = {};
      state.isLoading = false;
    });
  },
});

export const userActions = userStore.actions;

export default userStore;