Skip to content

Commit 0526bd4

Browse files
anton-tiptyukquarryman
authored andcommitted
chaining API requests
1 parent 165ecb7 commit 0526bd4

File tree

3 files changed

+57
-32
lines changed

3 files changed

+57
-32
lines changed

src/components/App.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class App extends Component {
1919
loading: false,
2020
serviceName: defaults.defaultServiceName
2121
};
22-
BusyAdapter.getServiceNamePromise().then((response) => {
23-
this.setState({ serviceName: response.services[0].name });
22+
BusyAdapter.getServiceInfoAsync().then((info) => {
23+
this.setState({ serviceName: info.name });
2424
});
2525
}
2626

src/components/Bookie.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class Bookie extends Component {
8989

9090
this.setParentLoading(true);
9191
this.setState({ loading: true }, () => {
92-
BusyAdapter.getServiceAvailableSlotsByIdPromise(dayToFetch)
92+
BusyAdapter.getSlotsAsync(dayToFetch)
9393
.then((slotData) => {
9494
const dayData = this.state.dayData;
9595
const parsedData = Scheduler.getDayDataFromSlots(slotData);

src/lib/BusyAdapter.js

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,54 @@ const ServicesController = Promise.promisifyAll(gonebusy.ServicesController);
88
const BookingsController = Promise.promisifyAll(gonebusy.BookingsController);
99

1010
const {
11-
service_id,
1211
clientToken: authorization,
1312
clientApiEndpoint
1413
} = gonebusyEnv;
1514

1615
gonebusy.configuration.BASEURI = clientApiEndpoint;
1716

18-
class BusyWrapper {
19-
static getServiceNamePromise() {
20-
return ServicesController.getServicesAsync({ authorization });
17+
let serviceInfo;
18+
19+
class BusyAdapter {
20+
static getServiceInfoAsync() {
21+
return new Promise((resolve) => {
22+
if (serviceInfo)
23+
resolve(serviceInfo);
24+
else {
25+
ServicesController
26+
.getServicesAsync({ authorization })
27+
.then((response) => {
28+
if (response.services && response.services.length) {
29+
const serviceEntry = response.services[0];
30+
serviceInfo = {
31+
name: serviceEntry.name,
32+
id: serviceEntry.id,
33+
max_duration: serviceEntry.max_duration,
34+
};
35+
resolve(serviceInfo);
36+
} else
37+
throw new Error('Failed to fetch service info');
38+
});
39+
}
40+
});
2141
}
2242

23-
static getServiceAvailableSlotsByIdPromise(date) {
24-
return ServicesController.getServiceAvailableSlotsByIdAsync({
25-
authorization,
26-
id: service_id,
27-
startDate: date,
28-
endDate: Scheduler.getNextDayString(date)
29-
}).then((data) => {
30-
const slotData = [];
31-
data.service.resources[0].availableSlots.forEach((x) => {
32-
slotData.push(...x.slots.split(', '));
43+
static getSlotsAsync(date) {
44+
return BusyAdapter
45+
.getServiceInfoAsync()
46+
.then(info => ServicesController.getServiceAvailableSlotsByIdAsync({
47+
authorization,
48+
id: info.id,
49+
startDate: date,
50+
endDate: Scheduler.getNextDayString(date)
51+
}))
52+
.then((data) => {
53+
const slotData = [];
54+
data.service.resources[0].availableSlots.forEach((x) => {
55+
slotData.push(...x.slots.split(', '));
56+
});
57+
return slotData;
3358
});
34-
return slotData;
35-
});
3659
}
3760

3861
static getBookingsPromise(args) {
@@ -43,23 +66,25 @@ class BusyWrapper {
4366
}
4467

4568
static createBookingPromise({ date, time, duration }) {
46-
const params = {
47-
service_id,
48-
date,
49-
time
50-
};
51-
if (undefined !== duration)
52-
params.duration = duration;
53-
54-
console.log(params);
55-
56-
const createBookingBody = new CreateBookingBody(params);
57-
return BookingsController.createBookingAsync({ authorization, createBookingBody });
69+
return BusyAdapter
70+
.getServiceInfoAsync()
71+
.then((info) => {
72+
const params = {
73+
service_id: info.id,
74+
date,
75+
time
76+
};
77+
if (undefined !== duration)
78+
params.duration = duration;
79+
console.log(params);
80+
const createBookingBody = new CreateBookingBody(params);
81+
return BookingsController.createBookingAsync({ authorization, createBookingBody });
82+
});
5883
}
5984

6085
static cancelBookingPromise(id) {
6186
return BookingsController.cancelBookingByIdAsync({ authorization, id });
6287
}
6388
}
6489

65-
export default BusyWrapper;
90+
export default BusyAdapter;

0 commit comments

Comments
 (0)