Newer
Older
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
import React, { useCallback, useContext, useEffect, useState } from 'react'
import 'react-draft-wysiwyg/dist/react-draft-wysiwyg.css'
import { FluidType } from '../../enum/fluidTypes'
import { FrequencyInMonth } from '../../enum/frequency.enum'
import { UserContext, UserContextProps } from '../../hooks/userContext'
import { IPrice } from '../../models/price.model'
import { PricesService } from '../../services/prices.service'
import arrowDown from '../../assets/icons/down-arrow.png'
import Loader from '../Loader/Loader'
import './prices.scss'
import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
import timezone from 'dayjs/plugin/timezone'
import PriceRow from './PriceRow'
dayjs.extend(utc)
dayjs.extend(timezone)
interface PriceSectionProps {
fluid: FluidType
frequency: FrequencyInMonth
}
const PriceSection: React.FC<PriceSectionProps> = ({
fluid,
frequency,
}: PriceSectionProps) => {
const [prices, setPrices] = useState<IPrice[]>([])
const [nextPrice, setNextPrice] = useState<IPrice>()
const [isLoading, setIsLoading] = useState<boolean>(false)
const [refreshData, setRefreshData] = useState<boolean>(false)
const [showHistory, setShowHistory] = useState<boolean>(false)
const [showFullList, setShowFullList] = useState<boolean>(false)
const [priceToSave, setPriceToSave] = useState<IPrice>({
fluidType: fluid,
price: '',
startDate: '',
endDate: null,
})
const { user }: Partial<UserContextProps> = useContext(UserContext)
const maxPerList: number = 8
const handlePriceSelection = useCallback((val: string) => {
if (val === '') val = '0'
val = val.replace(/,/g, '.')
val = val.replace(/([^0-9.]+)/, '')
setPriceToSave((prev) => {
return { ...prev, price: val }
})
}, [])
const savePrice = useCallback(async () => {
if (
priceToSave &&
user &&
priceToSave.price !== '0' &&
priceToSave.price !== ''
) {
const priceService = new PricesService()
const formattedPrice = {
...priceToSave,
price: parseFloat(priceToSave.price as string),
}
await priceService.savePrice(formattedPrice, user.xsrftoken)
setRefreshData(true)
}
}, [priceToSave, user])
const toggleHistory = useCallback(() => {
setShowHistory((prev) => !prev)
}, [])
const getDate = useCallback((isoString: string): string => {
const date = new Date(isoString)
const month = date.toLocaleString('fr', { month: 'long' })
const year = date.toLocaleString('fr', { year: 'numeric' })
return `${month} ${year}`
}, [])
const toggleFullList = useCallback(() => {
setShowFullList((prev) => !prev)
}, [])
useEffect(() => {
let subscribed = true
setIsLoading(true)
async function getPrices() {
const priceService = new PricesService()
const pricesByFluid = await priceService.getPricesByFluid(fluid)
if (pricesByFluid.length) {
const nextPriceToCreate: IPrice = {
fluidType: fluid,
price: '',
startDate: '',
endDate: null,
}
// Set the correct for the next price to create
const date: string = dayjs(pricesByFluid[0].startDate)
.utc(true)
.tz('Europe/Paris')
.add(frequency, 'month')
.startOf('day')
.format('YYYY-MM-DDTHH:mm:ss[Z]')
nextPriceToCreate.startDate = date
if (subscribed) {
setPrices(pricesByFluid)
setPriceToSave(nextPriceToCreate)
setNextPrice(nextPriceToCreate)
}
setIsLoading(false)
}
}
getPrices()
return () => {
subscribed = false
setRefreshData(false)
}
}, [refreshData, frequency, fluid])
if (isLoading) return <Loader></Loader>
if (!prices.length) return <section> Aucun prix trouvé</section>
return (
<section>
<h2>
{fluid === FluidType.ELECTRICITY && 'Electricité'}
{fluid === FluidType.WATER && 'Eau'}
{fluid === FluidType.GAS && 'Gaz'}
</h2>
<hr className="price-separator" />
<div className="flex-bloc">
<p>Nouveau prix : </p>
<input
className="input-dark price-select"
type="text"
value={priceToSave.price.toString()}
onChange={(e) => handlePriceSelection(e.target.value)}
placeholder={priceToSave.price === '' ? 'Saisir le nouveau prix' : ''}
/>
<span className="euro">€</span>
<div className="flex-bloc startDate">
<p>A partir de : </p>
<p className="date">
<span className="capital">{getDate(priceToSave.startDate)}</span>
</p>
</div>
</div>
<button
className="btnValid"
onClick={savePrice}
disabled={priceToSave.price === '0' || priceToSave.price === ''}
>
Sauvegarder
</button>
<div className="history">
<button onClick={toggleHistory} className={showHistory ? 'active' : ''}>
<span>Voir l'historique</span>
<img
src={arrowDown}
className={showHistory ? 'icon-active' : ''}
alt="arrow-icon"
/>
</button>
{showHistory && (
<ul className={showHistory ? 'active' : ''}>
{nextPrice && (
<PriceRow
getDate={getDate}
priceToSave={priceToSave}
price={nextPrice}
prices={prices}
setPriceToSave={setPriceToSave}
index={0}
isNextPrice={true}
/>
)}
{prices.map((price, i) => {
return (
<div
key={i}
className={
i > maxPerList && !showFullList ? 'price-hidden' : ''
}
>
<PriceRow
getDate={getDate}
priceToSave={priceToSave}
price={price}
prices={prices}
setPriceToSave={setPriceToSave}
index={i}
/>
{i === maxPerList && !showFullList && (
<button onClick={toggleFullList} className="showButton">
En voir plus
</button>
)}
</div>
)
})}
{showFullList && (
<button onClick={toggleFullList} className="showButton">
En voir moins
</button>
)}
</ul>
)}
</div>
</section>
)
}
export default PriceSection