-
Notifications
You must be signed in to change notification settings - Fork 267
/
ProfileScreen.jsx
162 lines (148 loc) · 4.99 KB
/
ProfileScreen.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import React, { useEffect, useState } from 'react';
import { Table, Form, Button, Row, Col } from 'react-bootstrap';
import { LinkContainer } from 'react-router-bootstrap';
import { useDispatch, useSelector } from 'react-redux';
import { FaTimes } from 'react-icons/fa';
import { toast } from 'react-toastify';
import Message from '../components/Message';
import Loader from '../components/Loader';
import { useProfileMutation } from '../slices/usersApiSlice';
import { useGetMyOrdersQuery } from '../slices/ordersApiSlice';
import { setCredentials } from '../slices/authSlice';
const ProfileScreen = () => {
const [name, setName] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [confirmPassword, setConfirmPassword] = useState('');
const { userInfo } = useSelector((state) => state.auth);
const { data: orders, isLoading, error } = useGetMyOrdersQuery();
const [updateProfile, { isLoading: loadingUpdateProfile }] =
useProfileMutation();
useEffect(() => {
setName(userInfo.name);
setEmail(userInfo.email);
}, [userInfo.email, userInfo.name]);
const dispatch = useDispatch();
const submitHandler = async (e) => {
e.preventDefault();
if (password !== confirmPassword) {
toast.error('Passwords do not match');
} else {
try {
const res = await updateProfile({
_id: userInfo._id,
name,
email,
password,
}).unwrap();
dispatch(setCredentials({ ...res }));
toast.success('Profile updated successfully');
} catch (err) {
toast.error(err?.data?.message || err.error);
}
}
};
return (
<Row>
<Col md={3}>
<h2>User Profile</h2>
<Form onSubmit={submitHandler}>
<Form.Group className='my-2' controlId='name'>
<Form.Label>Name</Form.Label>
<Form.Control
type='name'
placeholder='Enter name'
value={name}
onChange={(e) => setName(e.target.value)}
></Form.Control>
</Form.Group>
<Form.Group className='my-2' controlId='email'>
<Form.Label>Email Address</Form.Label>
<Form.Control
type='email'
placeholder='Enter email'
value={email}
onChange={(e) => setEmail(e.target.value)}
></Form.Control>
</Form.Group>
<Form.Group className='my-2' controlId='password'>
<Form.Label>Password</Form.Label>
<Form.Control
type='password'
placeholder='Enter password'
value={password}
onChange={(e) => setPassword(e.target.value)}
></Form.Control>
</Form.Group>
<Form.Group className='my-2' controlId='confirmPassword'>
<Form.Label>Confirm Password</Form.Label>
<Form.Control
type='password'
placeholder='Confirm password'
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
></Form.Control>
</Form.Group>
<Button type='submit' variant='primary'>
Update
</Button>
{loadingUpdateProfile && <Loader />}
</Form>
</Col>
<Col md={9}>
<h2>My Orders</h2>
{isLoading ? (
<Loader />
) : error ? (
<Message variant='danger'>
{error?.data?.message || error.error}
</Message>
) : (
<Table striped table hover responsive className='table-sm'>
<thead>
<tr>
<th>ID</th>
<th>DATE</th>
<th>TOTAL</th>
<th>PAID</th>
<th>DELIVERED</th>
<th></th>
</tr>
</thead>
<tbody>
{orders.map((order) => (
<tr key={order._id}>
<tD>{order._id}</tD>
<td>{order.createdAt.substring(0, 10)}</td>
<td>{order.totalPrice}</td>
<td>
{order.isPaid ? (
order.paidAt.substring(0, 10)
) : (
<FaTimes style={{ color: 'red' }} />
)}
</td>
<td>
{order.isDelivered ? (
order.deliveredAt.substring(0, 10)
) : (
<FaTimes style={{ color: 'red' }} />
)}
</td>
<td>
<LinkContainer to={`/order/${order._id}`}>
<Button className='btn-sm' variant='light'>
Details
</Button>
</LinkContainer>
</td>
</tr>
))}
</tbody>
</Table>
)}
</Col>
</Row>
);
};
export default ProfileScreen;