-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddRestaurantPage.js
124 lines (111 loc) · 3.8 KB
/
AddRestaurantPage.js
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
import React from "react";
import { Container, Form, Button, Image } from "react-bootstrap";
import AlertMessage from "../../components/AlertMessage";
import RestaurantsListPage from "./RestaurantsListPage";
const AddRestaurantPage = () => {
const [name, setName] = React.useState("");
const [address, setAddress] = React.useState("");
const [imageData, setImageData] = React.useState("");
const [error, setError] = React.useState(false);
const [success, setSuccess] = React.useState("");
const handleNameChange = (e) => {
setError("");
setSuccess("");
setName(e.target.value);
};
const handleAddressChange = (e) => {
setError("");
setSuccess("");
setAddress(e.target.value);
};
const handleImageDataChange = (e) => {
setError("");
setSuccess("");
setImageData(e.target.value);
};
const addRestaurantHandler = (e) => {
e.preventDefault();
const restaurants = JSON.parse(localStorage.getItem("restaurants")) || [];
const restaurant = {
id: restaurants.length + 1,
name: name,
address: address,
image: imageData
};
restaurants.push(restaurant);
localStorage.setItem("restaurants", JSON.stringify(restaurants));
setSuccess("restaurant is added successfully");
};
const [addRestaurant, setAddRestaurant] = React.useState(true);
const handleRestaurantList = (e) => {
e.preventDefault();
setAddRestaurant(false);
};
return (
<>
{addRestaurant && (
<Container>
{error && <AlertMessage variant="danger" message={error} />}
{success && <AlertMessage variant="success" message={success} />}
{imageData && (
<div className="text-center">
<Image src={imageData} width={200} height={200} rounded></Image>
</div>
)}
<Button
variant="outline-info"
className="my-3"
onClick={(e) => handleRestaurantList(e)}
>
View restaurants
</Button>
<Form className="mb-0 rounded p-4 bg-light" border="primary" name="addRestaurantForm" id="addRestaurantForm">
<Form.Group controlId="name" className="mb-3">
<Form.Label htmlFor="restaurantName">Restaurant Name</Form.Label>
<Form.Control
type="text"
placeholder="Restaurant Name"
value={name}
id="restaurantName"
name="restaurantName"
onChange={(e) => handleNameChange(e)}
/>
</Form.Group>
<Form.Group controlId="description" className="mb-3">
<Form.Label htmlFor="restaurantAddr">Restaurant Address</Form.Label>
<Form.Control
type="textarea"
placeholder="Restaurant Address"
value={address}
id="restaurantAddr"
name="restaurantAddr"
onChange={(e) => handleAddressChange(e)}
/>
</Form.Group>
<Form.Group controlId="description" className="mb-3">
<Form.Label htmlFor="restaurantImage">Restaurant Image</Form.Label>
<Form.Control
type="url"
placeholder="Restaurant Image"
value={imageData}
id="restaurantImage"
name="restaurantImage"
onChange={(e) => handleImageDataChange(e)}
/>
</Form.Group>
<Button
type="submit"
variant="info"
className="mb-3"
onClick={addRestaurantHandler}
>
Add restaurant
</Button>
</Form>
</Container>
)}
{!addRestaurant && <RestaurantsListPage />}
</>
);
};
export default AddRestaurantPage;