Skip to content

Commit 0228217

Browse files
committed
Adds user info in modal
1 parent b6c9606 commit 0228217

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

src/components/UserEditModal/UserEditModal.jsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { useRef } from "react";
22
import { useDispatch } from "react-redux";
3+
import { useSelector } from "react-redux";
4+
import { selectUser } from "../../redux/auth/selectror";
35
import { Field, Form, Formik } from "formik";
46
import { ErrorMessage } from "formik";
57
import PasswordField from "../PasswordField/PasswordField";
@@ -21,6 +23,7 @@ const ValidationSchema = Yup.object().shape({
2123
});
2224

2325
export default function UserEditModal({ onClose }) {
26+
const user = useSelector(selectUser);
2427
const dispatch = useDispatch();
2528

2629
const fileInputRef = useRef(null);
@@ -84,8 +87,8 @@ export default function UserEditModal({ onClose }) {
8487
<div>
8588
<Formik
8689
initialValues={{
87-
name: "",
88-
email: "",
90+
name: user.name || "",
91+
email: user.email || "",
8992
password: "",
9093
}}
9194
onSubmit={handleSubmit}

src/redux/auth/operations.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export const register = createAsyncThunk(
1515
"/auth/register",
1616
async (userInfo, thunkAPI) => {
1717
try {
18-
1918
const response = await axios.post("/auth/register", userInfo);
2019
setAuthHeader(response.data.token);
2120
return response.data;
@@ -31,6 +30,7 @@ export const logIn = createAsyncThunk(
3130
try {
3231
const response = await axios.post("/auth/login", userInfo);
3332
setAuthHeader(response.data.token);
33+
await thunkAPI.dispatch(getUserInfo());
3434
return response.data;
3535
} catch (error) {
3636
return thunkAPI.rejectWithValue(error.message);

src/redux/auth/slice.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ const authSlice = createSlice({
7171
})
7272
.addCase(getUserInfo.fulfilled, (state, action) => {
7373
state.loading = false;
74-
state.user = action.payload;
74+
// state.user = action.payload;
75+
state.user.name = action.payload.name;
76+
state.user.email = action.payload.email;
77+
state.user.theme = action.payload.theme;
78+
state.user.avatarURL = action.payload.avatarURL;
7579
state.isLoggedIn = true;
7680
})
7781
.addCase(getUserInfo.rejected, (state, action) => {

0 commit comments

Comments
 (0)