-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pricing.tsx
261 lines (226 loc) · 7.92 KB
/
Pricing.tsx
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import React, { useState } from 'react';
import styled from 'styled-components';
interface Pricing {
id: number;
roomType: string;
price: number;
discount: number;
seasonalRate: string;
}
const PricingContainer = styled.div`
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #ffffff; /* White background */
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
`;
const Title = styled.h1`
text-align: center;
color: #28a745; /* Green color */
margin-bottom: 20px;
font-size: 2rem;
font-weight: bold;
text-transform: uppercase;
`;
const Form = styled.form`
display: grid;
gap: 10px;
margin-bottom: 20px;
background-color: #f7f7f7; /* Light grey background for form */
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
`;
const FormGroup = styled.div`
display: flex;
flex-direction: column;
`;
const Label = styled.label`
font-size: 14px;
color: #333; /* Darker grey for text */
`;
const Input = styled.input`
padding: 10px;
font-size: 14px;
border-radius: 4px;
border: 1px solid #ddd;
margin-top: 5px;
`;
const Button = styled.button<{ variant?: string }>`
padding: 10px 15px;
font-size: 14px;
border: none;
border-radius: 4px;
cursor: pointer;
background-color: ${({ variant }) => (variant === 'delete' ? '#dc3545' : variant === 'edit' ? '#28a745' : '#007BFF')}; /* Green and red colors */
color: white;
transition: background-color 0.3s ease;
&:hover {
background-color: ${({ variant }) => (variant === 'delete' ? '#c82333' : variant === 'edit' ? '#218838' : '#0056b3')}; /* Darker green and red */
}
`;
const Table = styled.table`
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
`;
const TableHeader = styled.th`
padding: 10px;
background-color: #28a745; /* Green color */
color: white;
border: 1px solid #ddd;
`;
const TableData = styled.td`
padding: 10px;
border: 1px solid #ddd;
text-align: center;
`;
const ActionButtons = styled.div`
display: flex;
gap: 10px;
justify-content: center;
`;
const ErrorMessage = styled.p`
color: #dc3545; /* Red color for errors */
font-size: 12px;
`;
// Function to format numbers as dollars
const formatCurrency = (amount: number) => {
return new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
}).format(amount);
};
const Pricing: React.FC = () => {
const [pricingData, setPricingData] = useState<Pricing[]>([]);
const [roomType, setRoomType] = useState('');
const [price, setPrice] = useState<number | ''>('');
const [discount, setDiscount] = useState<number | ''>('');
const [seasonalRate, setSeasonalRate] = useState('');
const [selectedPricing, setSelectedPricing] = useState<Pricing | null>(null);
const [error, setError] = useState('');
const validateForm = (): boolean => {
if (!roomType || price === '' || discount === '' || !seasonalRate) {
setError('All fields are required.');
return false;
}
if (price <= 0 || discount < 0) {
setError('Price must be greater than 0 and discount must be non-negative.');
return false;
}
setError('');
return true;
};
const handleAddPricing = (e: React.FormEvent) => {
e.preventDefault();
if (!validateForm()) return;
const newPricing: Pricing = {
id: Date.now(),
roomType,
price: Number(price),
discount: Number(discount),
seasonalRate,
};
setPricingData([...pricingData, newPricing]);
resetForm();
};
const handleEditPricing = (e: React.FormEvent) => {
e.preventDefault();
if (!validateForm()) return;
if (!selectedPricing) return;
const updatedPricing: Pricing = {
...selectedPricing,
roomType,
price: Number(price),
discount: Number(discount),
seasonalRate,
};
setPricingData(
pricingData.map(pricing =>
pricing.id === updatedPricing.id ? updatedPricing : pricing
)
);
resetForm();
};
const handleDeletePricing = (id: number) => {
setPricingData(pricingData.filter(pricing => pricing.id !== id));
};
const handleSelectPricing = (pricing: Pricing) => {
setSelectedPricing(pricing);
setRoomType(pricing.roomType);
setPrice(pricing.price);
setDiscount(pricing.discount);
setSeasonalRate(pricing.seasonalRate);
};
const resetForm = () => {
setRoomType('');
setPrice('');
setDiscount('');
setSeasonalRate('');
setSelectedPricing(null);
};
return (
<PricingContainer>
<Title>Pricing Management</Title>
<Form onSubmit={selectedPricing ? handleEditPricing : handleAddPricing}>
<FormGroup>
<Label>Room Type</Label>
<Input type="text" value={roomType} onChange={(e) => setRoomType(e.target.value)} />
</FormGroup>
<FormGroup>
<Label>Price</Label>
<Input
type="number"
value={price}
onChange={(e) => setPrice(parseFloat(e.target.value))}
placeholder="$0.00"
/>
</FormGroup>
<FormGroup>
<Label>Discount</Label>
<Input
type="number"
value={discount}
onChange={(e) => setDiscount(parseFloat(e.target.value))}
placeholder="$0.00"
/>
</FormGroup>
<FormGroup>
<Label>Seasonal Rate</Label>
<Input type="text" value={seasonalRate} onChange={(e) => setSeasonalRate(e.target.value)} />
</FormGroup>
{error && <ErrorMessage>{error}</ErrorMessage>}
<Button type="submit">{selectedPricing ? 'Save Changes' : 'Add Pricing'}</Button>
</Form>
<Table>
<thead>
<tr>
<TableHeader>Room Type</TableHeader>
<TableHeader>Price</TableHeader>
<TableHeader>Discount</TableHeader>
<TableHeader>Seasonal Rate</TableHeader>
<TableHeader>Actions</TableHeader>
</tr>
</thead>
<tbody>
{pricingData.map(pricing => (
<tr key={pricing.id}>
<TableData>{pricing.roomType}</TableData>
<TableData>{formatCurrency(pricing.price)}</TableData>
<TableData>{formatCurrency(pricing.discount)}</TableData>
<TableData>{pricing.seasonalRate}</TableData>
<TableData>
<ActionButtons>
<Button variant="edit" onClick={() => handleSelectPricing(pricing)}>Edit</Button>
<Button variant="delete" onClick={() => handleDeletePricing(pricing.id)}>Delete</Button>
</ActionButtons>
</TableData>
</tr>
))}
</tbody>
</Table>
</PricingContainer>
);
};
export default Pricing;