cronofy-elements
Advanced tools
Comparing version 1.14.0 to 1.15.0
{ | ||
"name": "cronofy-elements", | ||
"version": "1.14.0", | ||
"version": "1.15.0", | ||
"description": "Fast track scheduling with Cronofy's embeddable UI Elements", | ||
@@ -5,0 +5,0 @@ "main": "build/npm/CronofyElements.js", |
@@ -107,2 +107,5 @@ import React, { useContext } from "react"; | ||
} | ||
&:focus { | ||
outline: none; | ||
} | ||
`} | ||
@@ -109,0 +112,0 @@ className={`${theme.prefix}__slot-button`} |
@@ -92,3 +92,3 @@ import React, { useState, useEffect } from "react"; | ||
const [rawData, setRawData] = useState([]); | ||
const [rawData, setRawData] = useState(false); | ||
@@ -119,2 +119,127 @@ const [weeks, setWeeks] = useState({ | ||
// This is a wrapper for our `connections.getAvailability()` call. | ||
// We always want to handle errors the same way, but need to handle the response | ||
// differently depending on the scenario (checking visible period and updating | ||
// the grid, vs. checking all weeks to test if *anything* is available). | ||
const handleGetAvailability = ({ query, callback }) => { | ||
connections | ||
.getAvailability( | ||
options.token, | ||
options.domains.apiDomain, | ||
query, | ||
`AvailabilityViewer`, | ||
status.tzid, | ||
options.demo | ||
) | ||
.then(res => { | ||
if ( | ||
typeof res.available_slots === "undefined" && | ||
typeof res.available_periods === "undefined" | ||
) { | ||
throw { | ||
type: 400, | ||
message: | ||
"Check your `token`, `target`, and `query` params are correct. Full details can be found on the Cronofy Elements documention page: https://docs.cronofy.com/developers/ui-elements/" | ||
}; | ||
} | ||
const response = | ||
query.response_format === "overlapping_slots" | ||
? res.available_slots | ||
: res.available_periods; | ||
callback(response, query); | ||
}) | ||
.catch(error => { | ||
setStatus({ | ||
...status, | ||
error, | ||
loading: false, | ||
preloading: false | ||
}); | ||
const errorMessage = error.message ? error.message : error; | ||
const errorOutput = {}; | ||
if (error.docsSlug) { | ||
errorOutput.docsSlug = error.docsSlug; | ||
} | ||
if (error.body) { | ||
errorOutput.errorObject = error.body; | ||
} | ||
log.error(errorMessage, errorOutput); | ||
return error; | ||
}); | ||
}; | ||
const handleNormalAvailability = response => { | ||
if (response.length < 1) { | ||
// There isn't any availability for the selected | ||
// week. Check if *anything* is available... | ||
checkAllAvailability(options.query); | ||
} | ||
// Happy path. | ||
// We've checked availability, there *is* availability, and | ||
// now we need to show that availability in the grid. | ||
const slotsWithKeys = addKeysToSlots(response); | ||
setRawData({ ...rawData, ...slotsWithKeys }); | ||
const weeksLoaded = uniqueItems([...status.weeksLoaded, weeks.current]); | ||
setStatus({ | ||
...status, | ||
error: false, | ||
loading: false, | ||
preloading: false, | ||
weeksLoaded | ||
}); | ||
}; | ||
const handleEmptyAvailability = (res, query) => { | ||
let notification; | ||
if (res.length < 1) { | ||
// There is no availability for any of the | ||
// weeks shown in the viewer. | ||
notification = { | ||
notification: { | ||
type: "no_slots_found", | ||
query: query | ||
} | ||
}; | ||
} else { | ||
// There is no availability for the week that | ||
// is currently visible, but there is at least | ||
// one free slot in one of the offscreen weeks. | ||
notification = { | ||
notification: { | ||
type: "no_visible_slots" | ||
} | ||
}; | ||
} | ||
status.notificationCallback(notification); | ||
}; | ||
const checkAllAvailability = query => { | ||
// At this point we're only interested in the binary | ||
// question of has-availability/has-no-availability. | ||
// `max_results: 1` ensures we're making the most | ||
// light-weight request we can. | ||
const croppedQuery = cropQuery( | ||
{ ...query, max_results: 1 }, | ||
{ | ||
currentWeek: weeks.current, | ||
days: weeks.days, | ||
startTime: limits.start, | ||
endTime: limits.end, | ||
tzid: status.tzid | ||
}, | ||
true // this is where we ignore week divisions | ||
); | ||
handleGetAvailability({ | ||
query: croppedQuery, | ||
callback: res => handleEmptyAvailability(res, croppedQuery) | ||
}); | ||
}; | ||
useEffect(() => { | ||
@@ -148,76 +273,9 @@ setWeeks( | ||
if (croppedQuery.available_periods.length > 0) { | ||
connections | ||
.getAvailability( | ||
options.token, | ||
options.domains.apiDomain, | ||
croppedQuery, | ||
`AvailabilityViewer`, | ||
status.tzid, | ||
options.demo | ||
) | ||
.then(res => { | ||
if ( | ||
typeof res.available_slots === "undefined" && | ||
typeof res.available_periods === "undefined" | ||
) { | ||
throw { | ||
type: 400, | ||
message: | ||
"Check your `token`, `target`, and `query` params are correct. Full details can be found on the Cronofy Elements documention page: https://docs.cronofy.com/developers/ui-elements/" | ||
}; | ||
} | ||
const response = | ||
query.response_format === "overlapping_slots" | ||
? res.available_slots | ||
: res.available_periods; | ||
if (response.length < 1) { | ||
const notification = { | ||
notification: { | ||
type: "no_slots_found", | ||
query: croppedQuery | ||
} | ||
}; | ||
status.notificationCallback(notification); | ||
} | ||
const slotsWithKeys = addKeysToSlots(response); | ||
setRawData({ ...rawData, ...slotsWithKeys }); | ||
const weeksLoaded = uniqueItems([ | ||
...status.weeksLoaded, | ||
weeks.current | ||
]); | ||
setStatus({ | ||
...status, | ||
error: false, | ||
loading: false, | ||
preloading: false, | ||
weeksLoaded | ||
}); | ||
}) | ||
.catch(error => { | ||
setStatus({ | ||
...status, | ||
error, | ||
loading: false, | ||
preloading: false | ||
}); | ||
const errorMessage = error.message | ||
? error.message | ||
: error; | ||
const errorOutput = {}; | ||
if (error.docsSlug) { | ||
errorOutput.docsSlug = error.docsSlug; | ||
} | ||
if (error.body) { | ||
errorOutput.errorObject = error.body; | ||
} | ||
log.error(errorMessage, errorOutput); | ||
return error; | ||
}); | ||
// Happy Path. | ||
// Check the visible week's availability, and pass | ||
// the result into `handleNormalAvailability()` | ||
handleGetAvailability({ | ||
query: croppedQuery, | ||
callback: handleNormalAvailability | ||
}); | ||
} else { | ||
@@ -266,3 +324,3 @@ // If we get here, the available_periods are not within the visible window | ||
useEffect(() => { | ||
if (!objectIsEmpty(rawData)) { | ||
if (rawData) { | ||
const slots = generateWeeklySlots({ | ||
@@ -269,0 +327,0 @@ availablePeriods: rawData, |
@@ -208,2 +208,5 @@ import React, { useState, useContext, useEffect } from "react"; | ||
: "pointer"}; | ||
&:focus { | ||
outline: none; | ||
} | ||
`} | ||
@@ -210,0 +213,0 @@ tabIndex="-1" |
@@ -33,6 +33,3 @@ import moment from "moment-timezone"; | ||
min: moment.utc().format("YYYY-MM-DD"), | ||
max: moment | ||
.utc() | ||
.add(1, "days") | ||
.format("YYYY-MM-DD") | ||
max: moment.utc().add(1, "days").format("YYYY-MM-DD") | ||
}; | ||
@@ -430,4 +427,9 @@ } | ||
export const cropQuery = (query, extent) => { | ||
const days = divideDaysIntoWeeks(extent.days, extent.currentWeek); | ||
export const cropQuery = (query, extent, ignoreWeeks = false) => { | ||
let days; | ||
if (ignoreWeeks) { | ||
days = extent.days; | ||
} else { | ||
days = divideDaysIntoWeeks(extent.days, extent.currentWeek); | ||
} | ||
@@ -434,0 +436,0 @@ // These are the bounds within which our query should live: |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2482891
22008