Changing Basket Logic Frontend
In this lesson, we're going to change the basket logic in frontend
Changing basket logic frontend#
We just changed the basket logic in the backend, so let's now apply the changes in the frontend. Let's start by going to the user model. Here, we can add basket property which will be optional, so I will add a question mark. It will be of type Basket so let's import it. It's optional because the user will not always have a basket.
Now we can go to the userSlice file. We need to make some changes to the signInUser function because from now onwards, we will receive a basket along with the user token. We can change the variable to userData because it contains basket. We can use destructuring to extract user and the basket from the userData. Let's use curly brackets and inside, we can destructure basket, and the rest of the data will be the user so we can use spread operator with the user. The user part will then be stored inside the local storage. Since we are returning the user from here, it will be stored inside redux state. We will then check if the basket exists. If it does, we will use thunkAPI to dispatch the setBasket function which we created inside basketSlice and pass the basket.