Skip to content

Commit

Permalink
Merge pull request #54 from AnWhiteM/reload
Browse files Browse the repository at this point in the history
reload
  • Loading branch information
AnWhiteM authored Jun 13, 2024
2 parents 6cf1869 + a6500ad commit e1f4a5e
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 46 deletions.
20 changes: 9 additions & 11 deletions src/components/App/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import { lazy, Suspense, useEffect } from "react";
import { Route, Routes } from "react-router-dom";
import { Toaster } from "react-hot-toast";
import { useDispatch, useSelector } from "react-redux";
import { refreshUser, getUserInfo } from "../../redux/auth/operations";
import {
selectIsRefreshing,
selectIsLoggedIn,
} from "../../redux/auth/selectors";
import { refreshUser } from "../../redux/auth/operations";
import { selectIsRefreshing } from "../../redux/auth/selectror";


import RestrictedRoute from "../RestrictedRoute/RestrictedRoute";
import PrivateRoute from "../PrivateRoute/PrivateRoute";
Expand All @@ -19,18 +17,18 @@ const NotFoundPage = lazy(() => import("../../pages/NotFoundPage"));

export const App = () => {
const isRefreshing = useSelector(selectIsRefreshing);
const isLoggedIn = useSelector(selectIsLoggedIn);
// const isLoggedIn = useSelector(selectIsLoggedIn);
const dispatch = useDispatch();

useEffect(() => {
dispatch(refreshUser());
}, [dispatch]);

useEffect(() => {
if (isLoggedIn) {
dispatch(getUserInfo());
}
}, [dispatch, isLoggedIn]);
// useEffect(() => {
// if (isLoggedIn) {
// dispatch(getUserInfo());
// }
// }, [dispatch, isLoggedIn]);
return (
<>
{isRefreshing ? (
Expand Down
7 changes: 6 additions & 1 deletion src/components/Header/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ export default function Header({ openSideBar, sideBarOpen }) {
<div className={css.layout}>
<p className={css.text}>{user.name}</p>
<button className={css.button} onClick={() => openModal()}>
<span className={`${css.avatarSmall} ${css.avatar}`} />
<span
className={`${css.avatarSmall} ${css.avatar}`}
// style={{
// backgroundImage: `url(${user.avatarURL})`,
// }}
/>
</button>
</div>
{isModalOpen && <UserEditModal onClose={closeModal} />}
Expand Down
32 changes: 27 additions & 5 deletions src/components/UserEditModal/UserEditModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,20 @@ export default function UserEditModal({ onClose }) {

const fileInputRef = useRef(null);

const handleSubmit = (values, actions) => {
// отправляем операцию aпдейтюзера и передаем ей обьект с данными имя мыло пароль
dispatch(updateUserInfo(values));
actions.resetForm();
const handleSubmit = async (values) => {
console.log(values);
try {
const sendInfo = {
avatarURL: values.avatarURL,
name: values.name,
email: values.email,
password: values.password,
};
await dispatch(updateUserInfo(sendInfo)).unwrap();
// отправляем операцию aпдейтюзера и передаем ей обьект с данными имя мыло пароль
} catch (error) {
console.log(error);
}
};

const handleMenuClick = (ev) => {
Expand Down Expand Up @@ -66,7 +76,12 @@ export default function UserEditModal({ onClose }) {
</div>
<p className={css.txt}>Edit Profile</p>
<div className={css.avatarContainer}>
<span className={`${css.avatarBig} ${css.avatar}`} />
<span
className={`${css.avatarBig} ${css.avatar}`}
// style={{
// backgroundImage: `url(${user.avatarURL})`,
// }}
/>

<button
type="button"
Expand All @@ -87,6 +102,7 @@ export default function UserEditModal({ onClose }) {
<div>
<Formik
initialValues={{
// avatarURL: user.avatarURL || null,
name: user.name || "",
email: user.email || "",
password: "",
Expand All @@ -95,6 +111,12 @@ export default function UserEditModal({ onClose }) {
validationSchema={ValidationSchema}
>
<Form className={css.forma} autoComplete="off">
{/* <Field
type="text"
name="avatarURL"
className={css.formInput}
placeholder="avatar"
/> */}
<div className={css.formGroup}>
<label htmlFor="name" className={css.formLabel} />
<Field
Expand Down
32 changes: 21 additions & 11 deletions src/redux/auth/operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,17 @@ export const refreshUser = createAsyncThunk(
* headers: Authorization: Bearer token
*/

export const getUserInfo = createAsyncThunk(
"user/getUserInfo",
async (_, thunkAPI) => {
try {
const response = await axios.get("/current");
return response.data;
} catch (error) {
return thunkAPI.rejectWithValue(error.message);
}
}
);
// export const getUserInfo = createAsyncThunk(
// "user/getUserInfo",
// async (_, thunkAPI) => {
// try {
// const response = await axios.get("/current");
// return response.data;
// } catch (error) {
// return thunkAPI.rejectWithValue(error.message);
// }
// }
// );

/*
* Put @ /
Expand All @@ -98,10 +98,20 @@ export const updateUserInfo = createAsyncThunk(
async (userData, thunkAPI) => {
try {
console.log(userData);
const reduxState = thunkAPI.getState();
const savedToken = reduxState.auth.token;
setAuthHeader(savedToken);
const response = await axios.put("/current", userData);
return response.data;
} catch (error) {
return thunkAPI.rejectWithValue(error.message);
}
},
{
condition: (_, { getState }) => {
const reduxState = getState();
const savedToken = reduxState.auth.token;
return savedToken !== null;
},
}
);
42 changes: 24 additions & 18 deletions src/redux/auth/slice.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
logOut,
register,
updateUserInfo,
getUserInfo,
// getUserInfo,
refreshUser,
} from "./operations";

Expand Down Expand Up @@ -67,30 +67,34 @@ const authSlice = createSlice({
state.loading = false;
state.error = true;
})
.addCase(getUserInfo.pending, (state) => {
// .addCase(getUserInfo.pending, (state) => {
// state.loading = true;
// state.error = false;
// })
// .addCase(getUserInfo.fulfilled, (state, action) => {
// state.loading = false;
// // state.user = action.payload;
// state.user.name = action.payload.name;
// state.user.email = action.payload.email;
// state.user.theme = action.payload.theme;
// state.user.avatarURL = action.payload.avatarURL;
// state.isLoggedIn = true;
// })
// .addCase(getUserInfo.rejected, (state, action) => {
// state.loading = false;
// state.error = action.payload;
// })
.addCase(updateUserInfo.pending, (state) => {
state.loading = true;
state.error = false;
})
.addCase(getUserInfo.fulfilled, (state, action) => {
.addCase(updateUserInfo.fulfilled, (state, action) => {
state.isLoggedIn = true;
state.loading = false;
// state.user = action.payload;
state.user.name = action.payload.name;
state.user.email = action.payload.email;
state.user.theme = action.payload.theme;
state.user.avatarURL = action.payload.avatarURL;
state.isLoggedIn = true;
})
.addCase(getUserInfo.rejected, (state, action) => {
state.loading = false;
state.error = action.payload;
})
.addCase(updateUserInfo.pending, (state) => {
state.loading = true;
state.error = false;
})
.addCase(updateUserInfo.fulfilled, (state, action) => {
state.loading = false;
state.user = { ...state.user, ...action.payload }; // here we update userinfo
state.user.theme = action.payload.theme;
state.error = false;
})
.addCase(updateUserInfo.rejected, (state, action) => {
Expand All @@ -104,6 +108,8 @@ const authSlice = createSlice({
state.error = true;
})
.addCase(refreshUser.fulfilled, (state, action) => {
// action.payload.avatarURL =
// "https://cdn.britannica.com/26/162626-050-3534626F/Koala.jpg";
state.user = action.payload;
state.isLoggedIn = true;
state.isRefreshing = false;
Expand Down

0 comments on commit e1f4a5e

Please sign in to comment.