-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfileScreen.jsx
More file actions
169 lines (156 loc) · 5.22 KB
/
ProfileScreen.jsx
File metadata and controls
169 lines (156 loc) · 5.22 KB
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
163
164
165
166
167
168
169
import { useState, useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import { Table, Form, Button, Row, Col } from "react-bootstrap";
import { LinkContainer } from "react-router-bootstrap";
import { toast } from "react-toastify";
import { FcBiohazard } from "react-icons/fc";
import Message from "../components/Message";
import Loader from "../components/Loader";
import { useProfileMutation } from "../slices/usersApiSlice";
import { setCredentials } from "../slices/authSlice";
import { useGetMyOrdersQuery } from "../slices/ordersApiSlice";
const ProfileScreen = () => {
const [email, setEmail] = useState("");
const [name, setName] = useState("");
const [password, setPassword] = useState("");
const [confirmPassword, setConfirmPassword] = useState("");
const dispatch = useDispatch();
const { userInfo } = useSelector((state) => state.auth);
const [updateProfile, { isLoading: loadingUpdateProfile }] =
useProfileMutation();
const { data: orders, isLoading, error } = useGetMyOrdersQuery();
useEffect(() => {
if (userInfo) {
setName(userInfo.name);
setEmail(userInfo.email);
}
}, [userInfo, userInfo.name, userInfo.email]);
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");
} catch (error) {
toast.error(error?.data?.message || error.error);
}
}
};
return (
<Row>
<Col md={3}>
<h2>User Profile</h2>
<Form onSubmit={submitHandler}>
<Form.Group controlId="name" className="my-2">
<Form.Label>Name</Form.Label>
<Form.Control
type="text"
placeholder="Enter name"
value={name}
onChange={(e) => setName(e.target.value)}
></Form.Control>
</Form.Group>
<Form.Group controlId="email" className="my-2">
<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 controlId="password" className="my-2">
<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 controlId="confirmPassword" className="my-2">
<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="dark" className="my-2">
Update Profile
</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 bordered 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)
) : (
<FcBiohazard style={{ color: "red" }} />
)}
</td>
<td>
{order.isDelivered ? (
order.deliveredAt.substring(0, 10)
) : (
<FcBiohazard style={{ color: "red" }} />
)}
</td>
<td>
<LinkContainer to={`/order/${order._id}`}>
<Button className="btn-sm" variant="info">
Details
</Button>
</LinkContainer>
</td>
</tr>
))}
</tbody>
<tfoot>
<td>
<FcBiohazard style={{ color: "red", fontSize: "24px" }} /> = Not
complete
</td>
</tfoot>
</Table>
)}
</Col>
</Row>
);
};
export default ProfileScreen;