Based on the below userApiSlice * eslint-disable no-unused-vars */ import {…
- User
Based on the below userApiSlice * eslint-disable no-unused-vars */ import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'; import axios from '../api/api';
const USERS_URL = '/auth/email';
export const login = createAsyncThunk('user/login', async (data) => { const response = await axios.post(
${USERS_URL}/login, data); return response.data; });export const register = createAsyncThunk('user/register', async (data) => { const response = await axios.post(
${USERS_URL}/register, data); return response.data; });export const updateUser = createAsyncThunk('user/update', async (data) => { const response = await axios.put('auth/me', data); return response.data; });
const initialState = { userInfo: localStorage.getItem('userInfo') ? JSON.parse(localStorage.getItem('userInfo')) : null, status: 'idle', error: null, };
const userSlice = createSlice({ name: 'user', initialState, extraReducers: (builder) => { builder .addCase(login.pending, (state) => { state.status = 'loading'; }) .addCase(login.fulfilled, (state, action) => { state.status = 'succeeded'; state.userInfo = action.payload; localStorage.setItem('userInfo', JSON.stringify(action.payload)); }) .addCase(login.rejected, (state, action) => { state.status = 'failed'; state.error = action.error.message; }) .addCase(register.pending, (state) => { state.status = 'loading'; }) .addCase(register.fulfilled, (state, action) => { state.status = 'succeeded'; state.userInfo = action.payload; localStorage.setItem('userInfo', JSON.stringify(action.payload)); }) .addCase(register.rejected, (state, action) => { state.status = 'failed'; state.error = action.error.message; }) .addCase(updateUser.pending, (state) => { state.status = 'loading'; }) .addCase(updateUser.fulfilled, (state, action) => { state.status = 'succeeded'; state.userInfo = action.payload; localStorage.setItem('userInfo', JSON.stringify(action.payload)); }) .addCase(updateUser.rejected, (state, action) => { state.status = 'failed'; state.error = action.error.message; }) .addDefaultCase((state) => {}); }, });
export default userSlice.reducer; how should i update the below LoginScreen component import { useState, useEffect } from 'react'; import { Link, useNavigate } from 'react-router-dom'; import { Form, Button, Row, Col } from 'react-bootstrap'; import FormContainer from '../components/FormContainer'; import { useDispatch, useSelector } from 'react-redux'; import { useLoginMutation } from '../slices/usersApiSlice'; import { setCredentials } from '../slices/authSlice'; import { toast } from 'react-toastify'; import Loader from '../components/Loader';
const LoginScreen = () => { const [email, setEmail] = useState(''); const [password, setPassword] = useState('');
const dispatch = useDispatch(); const navigate = useNavigate();
const [login, { isLoading }] = useLoginMutation();
const { userInfo } = useSelector((state) => state.auth);
- Assistant
Since the backend API has been refactored and the
useLoginMutationhook fromusersApiSliceis no longer used in the updateduserSlice, theLoginScreencomponent should be updated to dispatch theloginaction creator from theuserSliceinstead of using theuseLoginMutationhook.To update the
LoginScreencomponent:- Remove the following imports:
codeimport { useLoginMutation } from '../slices/usersApiSlice'; import { setCredentials } from '../slices/authSlice';- Import the
loginaction creator fromuserSlice:
codeimport { login } from '../slices/userSlice'; - User
Give me the complete updated LoginScreen componenet
- Assistant
- User
- Assistant