Skip to content

Commit 24f31e4

Browse files
authored
Если у пользователя не заполнены поля имя и почта, вылезет модальное … (#292)
* Если у пользователя не заполнены поля имя и почта, вылезет модальное окно, с просьбой заполнить их * Доработал модальное окно и изменил стили для него * Убрал бессмыленную очисту полей ввода данных пользователя
1 parent 480eb66 commit 24f31e4

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed

src/app/App.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { useAuth } from 'common/context/Auth/useAuth';
55
import { Header } from 'components/layout/header/Header';
66
import { fetchProfile } from 'features/profile/profileSlice';
77
import { Footer } from 'components/layout/Footer';
8+
import PatchUserModal from 'components/PatchUserModal';
89
import { LazyPlaceholder } from './LazyPlaceholder';
910

1011
const QuestionPage = lazy(() => import('features/questions/question-page/QuestionPage'));
@@ -56,6 +57,7 @@ export const App = () => {
5657

5758
return (
5859
<>
60+
<PatchUserModal />
5961
<Header />
6062
<Suspense fallback={<LazyPlaceholder />}>
6163
<Switch>

src/components/PatchUserModal.jsx

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { Button, Input, Modal } from 'antd';
2+
import React, { useState } from 'react';
3+
import { useDispatch, useSelector } from 'react-redux';
4+
import { selectProfile, updateProfile } from 'features/profile/profileSlice';
5+
import styled from 'styled-components';
6+
7+
const StyledModalHeading = styled.h2`
8+
text-align: center;
9+
`;
10+
const StyledButtonBlock = styled.div`
11+
width: 110px;
12+
margin: auto;
13+
`;
14+
15+
const PatchUserModal = () => {
16+
const profile = useSelector(selectProfile);
17+
18+
const dispatch = useDispatch();
19+
20+
const [userFullName, setUserFullName] = useState('');
21+
const [userEmail, setUserEmail] = useState('');
22+
23+
const id = profile._id;
24+
25+
const handleSubmit = () => {
26+
dispatch(updateProfile({ id, userFullName, userEmail }));
27+
};
28+
29+
const isModalOpened = !profile.fullName || !profile.email;
30+
31+
return (
32+
<div>
33+
<Modal open={isModalOpened} footer={null} destroyOnClose>
34+
<div>
35+
<StyledModalHeading>Введите полное имя и почту, что бы продолжить</StyledModalHeading>
36+
<div className="mt-3">
37+
{profile.fullName && (
38+
<Input
39+
value={userFullName}
40+
onChange={(e) => setUserFullName(e.target.value)}
41+
type="text"
42+
placeholder="Введите имя и фамилию"
43+
/>
44+
)}
45+
{profile.email && (
46+
<Input
47+
value={userEmail}
48+
onChange={(e) => setUserEmail(e.target.value)}
49+
type="text"
50+
placeholder="Введите Email"
51+
className="mt-2"
52+
/>
53+
)}
54+
<StyledButtonBlock>
55+
<Button
56+
className="mt-3"
57+
disabled={!userFullName}
58+
onClick={handleSubmit}
59+
type="button"
60+
>
61+
Сохранить
62+
</Button>
63+
</StyledButtonBlock>
64+
</div>
65+
</div>
66+
</Modal>
67+
</div>
68+
);
69+
};
70+
71+
export default PatchUserModal;

src/features/profile/ProfileUser.jsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ const ProfileUser = () => {
6060
<div className="registration-date">
6161
Зарегистрирован {dayjs(profile.createdAt).fromNow()}
6262
</div>
63-
<div className="userName">Атамазов Насырбек</div>
64-
<div className="userEmail">atamazov00@mail.ru</div>
63+
<div className="userName">
64+
{profile.fullName ? profile.fullName : `User ${profile._id}`}
65+
</div>
66+
<div className="userEmail">{profile.email}</div>
6567
</div>
6668
</Paper>
6769
</div>

src/features/profile/profileSlice.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ export const fetchProfile = createAsyncThunk('profile/fetch', async (_, thunkAPI
1515
}
1616
});
1717

18+
export const updateProfile = createAsyncThunk(
19+
'profile/patch',
20+
async ({ id, userFullName, userEmail }, thunkAPI) => {
21+
try {
22+
const response = await axios.patch(`http://localhost:3030/user/profile/${id}`, {
23+
fullName: userFullName,
24+
email: userEmail,
25+
});
26+
return response.data;
27+
} catch (e) {
28+
return thunkAPI.rejectWithValue(e.message);
29+
}
30+
}
31+
);
32+
1833
const profileSlice = createSlice({
1934
name: 'profile',
2035
initialState: {
@@ -26,6 +41,7 @@ const profileSlice = createSlice({
2641
questionIdsThatUserFavorite: [],
2742
isAdmin: false,
2843
createdAt: null,
44+
email: null,
2945
// добавить остальные поля по мере необходимости
3046
},
3147

@@ -43,6 +59,7 @@ const profileSlice = createSlice({
4359
state.questionIdsThatUserFavorite = payload.questionIdsThatUserFavorite;
4460
state.isAdmin = payload.isAdmin;
4561
state.createdAt = payload.createdAt;
62+
state.email = payload.email;
4663
},
4764

4865
[addQuestionToFavorites.pending]: (state, action) => {
@@ -54,6 +71,11 @@ const profileSlice = createSlice({
5471
(id) => id !== action.meta.arg.questionId
5572
);
5673
},
74+
75+
[updateProfile.fulfilled]: (state, action) => {
76+
state.fullName = action.payload.fullName;
77+
state.email = action.payload.email;
78+
},
5779
},
5880
reducers: {
5981
resetProfile: (state) => {

0 commit comments

Comments
 (0)