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 following ProfileScreen Component /* eslint-disable no-unused-vars */ import { useState, useEffect } from 'react'; import { Link, useNavigate } from 'react-router-dom'; import { Form, Button } from 'react-bootstrap'; import { useDispatch, useSelector } from 'react-redux'; import FormContainer from '../components/FormContainer'; import { toast } from 'react-toastify'; import Loader from '../components/Loader'; import { useUpdateUserMutation } from '../slices/usersApiSlice'; import { setCredentials } from '../slices/authSlice';
const ProfileScreen = () => { const [email, setEmail] = useState(''); const [firstName, setFirstName] = useState(''); const [lastName, setLastName] = useState(''); const [password, setPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState('');
const dispatch = useDispatch();
const { userInfo } = useSelector((state) => state.auth);
- Assistant
- User
Show the changed code by steps
- Assistant
- User
- Assistant