Skip to content

Commit

Permalink
refactor: change deprecated place field name openingHours to `regul…
Browse files Browse the repository at this point in the history
…arOpeningHours`

PiperOrigin-RevId: 616854411
  • Loading branch information
Extended Component Library Team authored and copybara-github committed Apr 5, 2024
1 parent c2363c5 commit 27341a7
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 78 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"@rollup/plugin-node-resolve": "^15.2.1",
"@rollup/plugin-replace": "^5.0.2",
"@rollup/plugin-terser": "^0.4.3",
"@types/google.maps": "~3.53.6",
"@types/google.maps": "~3.55.6",
"@types/jasmine": "^4.3.6",
"@types/react": "^18.2.24",
"@web/test-runner": "^0.17.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export class PlaceFieldBoolean extends PlaceDataConsumer {
case 'isOpen()':
return [
'businessStatus',
'openingHours',
'regularOpeningHours',
'utcOffsetMinutes',
];
case 'hasWheelchairAccessibleEntrance':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('place field boolean test', () => {
servesWine: true,

businessStatus: 'OPERATIONAL' as google.maps.places.BusinessStatus,
openingHours: {periods: [], weekdayDescriptions: []},
regularOpeningHours: {periods: [], weekdayDescriptions: []},
utcOffsetMinutes: 0,
isOpen: async () => true,
});
Expand Down Expand Up @@ -102,7 +102,7 @@ describe('place field boolean test', () => {
servesWine: false,

businessStatus: 'OPERATIONAL' as google.maps.places.BusinessStatus,
openingHours: {periods: [], weekdayDescriptions: []},
regularOpeningHours: {periods: [], weekdayDescriptions: []},
utcOffsetMinutes: 0,
isOpen: async () => false,
});
Expand Down Expand Up @@ -137,7 +137,7 @@ describe('place field boolean test', () => {
servesWine: null,

businessStatus: null,
openingHours: null,
regularOpeningHours: null,
utcOffsetMinutes: null,
isOpen: async () => undefined,
});
Expand Down Expand Up @@ -244,7 +244,7 @@ describe('place field boolean test', () => {
const openPlace = makeFakePlace({
id: '1234567890',
businessStatus: 'OPERATIONAL' as google.maps.places.BusinessStatus,
openingHours: {periods: [], weekdayDescriptions: []},
regularOpeningHours: {periods: [], weekdayDescriptions: []},
utcOffsetMinutes: 0,
isOpen: async () => true,
});
Expand Down Expand Up @@ -274,7 +274,7 @@ describe('place field boolean test', () => {
const isOpenSpy = jasmine.createSpy('isOpen');
const noBusinessStatusPlace = makeFakePlace({
id: '1234567890',
openingHours: {periods: [], weekdayDescriptions: []},
regularOpeningHours: {periods: [], weekdayDescriptions: []},
isOpen: isOpenSpy,
});
const noOpeningHoursPlace = makeFakePlace({
Expand All @@ -286,7 +286,7 @@ describe('place field boolean test', () => {
const noUtcOffsetPlace = makeFakePlace({
id: '1234567890',
businessStatus: 'OPERATIONAL' as google.maps.places.BusinessStatus,
openingHours: {periods: [], weekdayDescriptions: []},
regularOpeningHours: {periods: [], weekdayDescriptions: []},
isOpen: isOpenSpy,
});

Expand All @@ -308,7 +308,7 @@ describe('place field boolean test', () => {
const place = makeFakePlace({
id: '1234567890',
businessStatus: 'OPERATIONAL' as google.maps.places.BusinessStatus,
openingHours: {periods: [], weekdayDescriptions: []},
regularOpeningHours: {periods: [], weekdayDescriptions: []},
utcOffsetMinutes: 0,
isOpen: isOpenSpy,
});
Expand Down Expand Up @@ -347,7 +347,7 @@ describe('place field boolean test', () => {
const place = makeFakePlace({
id: '1234567890',
businessStatus: 'OPERATIONAL' as google.maps.places.BusinessStatus,
openingHours: {periods: [], weekdayDescriptions: []},
regularOpeningHours: {periods: [], weekdayDescriptions: []},
utcOffsetMinutes: 0,
});
const [el] = await prepareState(html`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class PlaceOpeningHours extends PlaceDataConsumer {
if (!place) return nothing;

// If opening hours data is missing, try to render business status instead.
if (!place.openingHours) {
if (!place.regularOpeningHours) {
if (place.businessStatus === 'OPERATIONAL') return nothing;
return html`
<div class="closed">
Expand All @@ -125,6 +125,7 @@ export class PlaceOpeningHours extends PlaceDataConsumer {
</div>
`;
}
const {weekdayDescriptions} = place.regularOpeningHours;

// if UTC offset data is missing, omit opening status from the summary.
let summary;
Expand Down Expand Up @@ -161,7 +162,7 @@ export class PlaceOpeningHours extends PlaceDataConsumer {
role="region"
>
<ul>
${map(place.openingHours.weekdayDescriptions, (description) => html`
${map(weekdayDescriptions, (description) => html`
<li>${description}</li>
`)}
</ul>
Expand All @@ -171,14 +172,14 @@ export class PlaceOpeningHours extends PlaceDataConsumer {

/** @ignore */
getRequiredFields(): Array<keyof Place> {
return ['businessStatus', 'openingHours', 'utcOffsetMinutes'];
return ['businessStatus', 'regularOpeningHours', 'utcOffsetMinutes'];
}

protected override placeHasData(place: Place): boolean {
if (place.businessStatus === 'OPERATIONAL' && !place.openingHours) {
if (place.businessStatus === 'OPERATIONAL' && !place.regularOpeningHours) {
return false;
}
return !!(place.businessStatus || place.openingHours);
return !!(place.businessStatus || place.regularOpeningHours);
}

private getOpenSummaryContent(place: Place) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {PlaceOpeningHours} from './place_opening_hours.js';
const FAKE_PLACE_PROPS: Pick<Place, 'id'>&Partial<Place> = {
id: '1234567890',
businessStatus: 'OPERATIONAL' as google.maps.places.BusinessStatus,
openingHours: {
regularOpeningHours: {
periods: [
{
open: {day: 0, hour: 10, minute: 0},
Expand Down Expand Up @@ -83,7 +83,7 @@ describe('PlaceOpeningHours', () => {
it('renders nothing when place is operational but opening hours is missing',
async () => {
const place =
makeFakePlace({...FAKE_PLACE_PROPS, openingHours: undefined});
makeFakePlace({...FAKE_PLACE_PROPS, regularOpeningHours: undefined});
const el = await prepareState({place});

expect(el.renderRoot.textContent).toBe('');
Expand All @@ -93,7 +93,7 @@ describe('PlaceOpeningHours', () => {
const place = makeFakePlace({
...FAKE_PLACE_PROPS,
businessStatus: 'CLOSED_TEMPORARILY' as google.maps.places.BusinessStatus,
openingHours: undefined,
regularOpeningHours: undefined,
});
const el = await prepareState({place});

Expand Down Expand Up @@ -143,7 +143,7 @@ describe('PlaceOpeningHours', () => {
it('labels place as open 24 hours when close time is null', async () => {
const place = makeFakePlace({
...FAKE_PLACE_PROPS,
openingHours: {
regularOpeningHours: {
periods: [
{
open: {day: 0, hour: 0, minute: 0},
Expand All @@ -161,7 +161,7 @@ describe('PlaceOpeningHours', () => {
it('omits closing time when data is insufficient', async () => {
const place = makeFakePlace({
...FAKE_PLACE_PROPS,
openingHours: {
regularOpeningHours: {
periods: [],
weekdayDescriptions: [],
},
Expand Down Expand Up @@ -249,7 +249,8 @@ describe('PlaceOpeningHours', () => {
const details = el.renderRoot.querySelector<HTMLDivElement>('#details');
expect(details?.textContent)
.toHaveNormalizedText(
FAKE_PLACE_PROPS.openingHours!.weekdayDescriptions.join(' '));
FAKE_PLACE_PROPS.regularOpeningHours!.weekdayDescriptions.join(
' '));
});

it('polls for updates when a Place is set', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/testing/fake_place.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const SAMPLE_FAKE_PLACE = makeFakePlace({
internationalPhoneNumber: '+1 234-567-8910',
location: new FakeLatLng(1, 2),
nationalPhoneNumber: '(234) 567-8910',
openingHours: {
regularOpeningHours: {
periods: [
{
close: {day: 0, hour: 18, minute: 0},
Expand Down
18 changes: 9 additions & 9 deletions src/utils/opening_hours.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,14 @@ interface UpcomingCloseTimeResult {
*/
export function getUpcomingCloseTime(
place: Place, now = new Date()): UpcomingCloseTimeResult {
if (!place.openingHours || place.utcOffsetMinutes == null) {
if (!place.regularOpeningHours || place.utcOffsetMinutes == null) {
return {status: NextCloseTimeStatus.UNKNOWN};
} else if (isPlaceAlwaysOpen(place.openingHours)) {
} else if (isPlaceAlwaysOpen(place.regularOpeningHours)) {
return {status: NextCloseTimeStatus.ALWAYS_OPEN};
}

const currentPeriod =
getCurrentPeriod(place.openingHours, place.utcOffsetMinutes, now);
getCurrentPeriod(place.regularOpeningHours, place.utcOffsetMinutes, now);
if (!currentPeriod.period) {
return {status: NextCloseTimeStatus.NOT_OPEN_NOW};
} else if (!currentPeriod.closeDate) {
Expand Down Expand Up @@ -220,9 +220,9 @@ interface UpcomingOpenTimeResult {
*/
export function getUpcomingOpenTime(
place: Place, now = new Date()): UpcomingOpenTimeResult {
if (!place.openingHours || place.utcOffsetMinutes == null) {
if (!place.regularOpeningHours || place.utcOffsetMinutes == null) {
return {status: NextOpenTimeStatus.UNKNOWN};
} else if (isPlaceAlwaysOpen(place.openingHours)) {
} else if (isPlaceAlwaysOpen(place.regularOpeningHours)) {
return {status: NextOpenTimeStatus.ALREADY_OPEN};
}

Expand All @@ -232,7 +232,7 @@ export function getUpcomingOpenTime(
status: NextOpenTimeStatus.NEVER_OPEN,
};
let closestOpenInterval = Infinity;
for (const period of place.openingHours.periods) {
for (const period of place.regularOpeningHours.periods) {
const openDate = getPointDate(period.open, lastSundayStart);
if (!period.close) {
return {status: NextOpenTimeStatus.ALREADY_OPEN};
Expand Down Expand Up @@ -264,12 +264,12 @@ export function getUpcomingOpenTime(
* Temporary (until Place is GA) replacement for the built-in isOpen() method.
*/
export function isOpen(place: Place, now = new Date()): boolean|undefined {
if (!place.openingHours || place.utcOffsetMinutes == null) {
if (!place.regularOpeningHours || place.utcOffsetMinutes == null) {
return undefined;
} else if (isPlaceAlwaysOpen(place.openingHours)) {
} else if (isPlaceAlwaysOpen(place.regularOpeningHours)) {
return true;
}
const {period} =
getCurrentPeriod(place.openingHours, place.utcOffsetMinutes, now);
getCurrentPeriod(place.regularOpeningHours, place.utcOffsetMinutes, now);
return !!period;
}
Loading

0 comments on commit 27341a7

Please sign in to comment.