My goal here is to check a checkin and checkout date and see if a room is available or not
if availdata[p.roomId][date].i==0
then the room at that range of dates is not available so it will be displayed as not available
if not it will check its price with availdata[p.roomId][date].p1
and display it with a price instead
JavaScript
x
149
149
1
import React,{useState,useEffect} from 'react';
2
import HotelCards from './HotelCards';
3
import styles from '../styles/Options/Options.module.css';
4
import {Checkkout as checkkout} from "./Hero";
5
import {Checkkin as checkkin} from "./Hero";
6
7
import {format} from "date-fns";
8
let HC=[];
9
let prices =[];
10
let notavailableat="";
11
let rowss=[];
12
export default function Options({selectedGuest}) {
13
const [availdata, setavailData] = useState([]);
14
const [isLoading, setIsLoading] = useState(false);
15
const [isLoading2, setIsLoading2] = useState(false);
16
const [data, setData] = useState([]);
17
18
// request Headers
19
const requestOptions = {
20
method: 'GET',
21
headers: { 'Content-Type': 'application/json' },
22
23
};
24
const requestOptions2 = {
25
method: 'GET',
26
headers: { 'Content-Type': 'application/json' },
27
28
};
29
30
31
//Get the rooms info along with the ID
32
const fetchData = () => {
33
fetch('http://localhost:3001/api/rooms', requestOptions)
34
.then(response => response.json())
35
36
.then((result) =>{
37
console.log("roooms"+result)
38
setData(result.rooms)
39
setIsLoading2(true);
40
41
}
42
43
)
44
.catch((err) => console.log("error"));
45
};
46
47
useEffect(() => {
48
fetchData();
49
}, []);
50
51
//get the i and p variables
52
function fetchData1() {
53
fetch('http://localhost:3001/api/availability', requestOptions2)
54
.then(response => response.json())
55
56
.then((result) =>{
57
setavailData(result.availability[0])
58
59
setIsLoading(true);
60
61
62
}
63
64
)
65
.catch((err) => console.log("error"));
66
}
67
68
useEffect(() => {
69
fetchData1();
70
}, []);
71
72
prices.length=0;
73
var strToDatein = new Date(checkkin)
74
var strToDateout = new Date(checkkout)
75
76
{data.map(p=> {
77
78
if (isLoading && isLoading2){
79
80
for (var day = strToDatein; day <= strToDateout; day.setDate(day.getDate() + 1)) {
81
82
83
var diplaydate = format(day,"dd MMM ");
84
85
var date = format(day, 'yyyyMMdd');
86
87
88
if (availdata[p.roomId][date].i==0){
89
90
rowss.push(<p key={availdata[p.roomId][date]}> not available at {diplaydate} </p>);
91
notavailableat="not available at "+diplaydate;
92
prices.length=0;
93
console.log("room:"+p.roomId+"available?"+availdata[p.roomId][date].i)
94
95
HC.push(<div key={p.roomId}>
96
<HotelCards
97
idroom={p.roomId}
98
title={p.roomName.toUpperCase()}
99
status={true}
100
price={prices}
101
img={p.pictures[0].url}
102
avail={notavailableat}
103
rows={rowss}
104
guest={selectedGuest}
105
/></div>)
106
break;
107
}
108
else
109
{
110
rowss.length=0;
111
prices.push(availdata[p.roomId][date].p1);
112
console.log("room:"+p.roomId+"price?"+availdata[p.roomId][date].p1)
113
114
HC.push(<div key={p.roomId}>
115
<HotelCards
116
idroom={p.roomId}
117
title={p.roomName.toUpperCase()}
118
status={true}
119
price={prices}
120
img={p.pictures[0].url}
121
avail={notavailableat}
122
rows={rowss}
123
guest={selectedGuest}
124
/></div>)
125
126
127
128
}
129
130
}
131
}
132
133
})
134
135
return (
136
<div className={`${styles.containers}`}>
137
138
{HC}
139
140
</div>
141
);
142
143
}}
144
145
146
147
148
149
in my case it’s displaying each room 4 times with same price
Advertisement
Answer
Update : solution is
JavaScript
1
78
78
1
function format(date) {
2
return `${date.getFullYear()}${`${date.getMonth()+1}`.padStart(2, 0)}${`${date.getDate()}`.padStart(2, 0)}`;
3
}
4
HC.length=0;
5
6
data.forEach((p) =>{
7
8
if (isLoading && isLoading2 ){
9
10
for (var day = new Date(strToDatein); day < strToDateout; day.setDate(day.getDate() + 1)) {
11
12
console.log(day + "dekhel for");
13
14
var diplaydate = format(day,"dd MMM ");
15
16
var date = format(day, 'yyyyMMdd');
17
18
if (availdata[p.roomId][date].i==0){
19
20
rowss.push(<p key={availdata[p.roomId][date]}> not available at {diplaydate} </p>);
21
notavailableat="not available at "+diplaydate;
22
console.log(+p.roomId+"not available at "+diplaydate)
23
24
HC.push(<div key={p.roomId}>
25
<HotelCards
26
idroom={p.roomId}
27
title={p.roomName.toUpperCase()}
28
status={true}
29
price={0}
30
img={p.pictures[0].url}
31
avail={1111}
32
rows={rowss}
33
guest={selectedGuest}
34
/></div>)
35
36
37
break;
38
}
39
else
40
{console.log("dateeee"+ date);
41
rowss.length=0;
42
prices.length=0;
43
prices.push(availdata[p.roomId][date].p1);
44
var total_price = 0;
45
if(prices.length!==0){
46
for (var i=0;i<=prices.length-1;i++){
47
total_price+=parseFloat(prices[i]);
48
}
49
50
}
51
console.log("room:"+p.roomId+"price?"+availdata[p.roomId][date].p1)
52
53
HC.push(<div key={p.roomId}>
54
<HotelCards
55
idroom={p.roomId}
56
title={p.roomName.toUpperCase()}
57
status={true}
58
price={total_price}
59
img={p.pictures[0].url}
60
avail={1111}
61
rows={rowss}
62
guest={selectedGuest}
63
/></div>)
64
65
66
67
}
68
return (
69
<div className={`${styles.containers}`}>
70
{HC }
71
</div>
72
);
73
}
74
75
}
76
77
})
78