Newer
Older
import dayjs from 'dayjs'
import timezone from 'dayjs/plugin/timezone'
import utc from 'dayjs/plugin/utc'
import React, { useCallback, useContext, useEffect, useState } from 'react'
import 'react-draft-wysiwyg/dist/react-draft-wysiwyg.css'
import arrowDown from '../../assets/icons/down-arrow.png'
import { getAxiosXSRFHeader } from '../../axios.config'
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 Loader from '../Loader/Loader'
import PriceRow from './PriceRow'
dayjs.extend(utc)
dayjs.extend(timezone)
interface PriceSectionProps {
fluid: FluidType
frequency: FrequencyInMonth
}
const PriceSection: React.FC<PriceSectionProps> = ({ fluid, frequency }) => {
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 handlePriceSelection = useCallback((val: string) => {
if (val === '') val = '0'
val = val.replace(/,/g, '.')
val = val.replace(/([^0-9.]+)/, '')
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,
getAxiosXSRFHeader(user.xsrftoken)
)
setRefreshData(true)
}
}, [priceToSave, user])
const toggleHistory = useCallback(() => {
}, [])
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(() => {
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
}, [])
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 (!prices.length) return <section> Aucun prix trouvé</section>
return (
<section>
<h2>
{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()}
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' : ''}>
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
<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