Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion public/app.js

Large diffs are not rendered by default.

19 changes: 18 additions & 1 deletion src/helpers/load-meeting-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export function loadMeetingData(
strings: Translation,
timezone?: string
): {
bounds?: { north: string; south: string; east: string; west: string };
capabilities: Data['capabilities'];
indexes: Data['indexes'];
meetings: Data['meetings'];
Expand All @@ -36,6 +37,9 @@ export function loadMeetingData(
distance: [],
};

const latitudes: number[] = [];
const longitudes: number[] = [];

// loop through each entry
flattenDays(data).forEach(meeting => {
const {
Expand Down Expand Up @@ -219,6 +223,8 @@ export function loadMeetingData(
typeof meeting.longitude === 'string'
? parseFloat(meeting.longitude)
: meeting.longitude;
latitudes.push(latitude);
longitudes.push(longitude);
}
}

Expand Down Expand Up @@ -561,7 +567,18 @@ export function loadMeetingData(
// determine sharing
capabilities.sharing = typeof navigator.canShare === 'function';

return { meetings, indexes, capabilities, slugs };
// bounds to bias the geocoder
const bounds =
latitudes.length && longitudes.length
? {
north: Math.max(...latitudes).toString(),
south: Math.min(...latitudes).toString(),
east: Math.max(...longitudes).toString(),
west: Math.min(...longitudes).toString(),
}
: undefined;

return { bounds, capabilities, indexes, meetings, slugs };
}

// look for data with multiple days and make them all single
Expand Down
15 changes: 7 additions & 8 deletions src/hooks/data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export const DataProvider = ({
}: PropsWithChildren<{ google?: string; src?: string; timezone?: string }>) => {
const [data, setData] = useState<Data>(defaultData);
const { setError } = useError();
const { input, latitude, longitude } = useInput();
const { input, latitude, longitude, setBounds } = useInput();
const { settings, strings } = useSettings();

useEffect(() => {
Expand Down Expand Up @@ -148,18 +148,17 @@ export const DataProvider = ({
throw new Error('data is not in the correct format');
}

const { meetings, indexes, capabilities, slugs } = loadMeetingData(
json,
data.capabilities,
settings,
strings,
timezone
);
const { bounds, capabilities, indexes, meetings, slugs } =
loadMeetingData(json, data.capabilities, settings, strings, timezone);

if (!timezone && !slugs.length) {
throw new Error('time zone is not set');
}

if (bounds) {
setBounds(bounds);
}

setData({
capabilities,
indexes,
Expand Down
17 changes: 15 additions & 2 deletions src/hooks/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,25 @@ const InputContext = createContext<
input: TSMLReactConfig['defaults'];
latitude?: number;
longitude?: number;
setBounds: (bounds: {
north: string;
south: string;
east: string;
west: string;
}) => void;
} & Coordinates
>({ input: defaults.defaults, waitingForInput: false });
>({ input: defaults.defaults, waitingForInput: false, setBounds: () => {} });

export const InputProvider = ({ children }: PropsWithChildren) => {
const { setError } = useError();
const [searchParams] = useSearchParams();
const { settings, strings } = useSettings();
const [bounds, setBounds] = useState({
north: '',
south: '',
east: '',
west: '',
});

const [input, setInput] = useState<TSMLReactConfig['defaults']>(
defaults.defaults
Expand Down Expand Up @@ -96,6 +108,7 @@ export const InputProvider = ({ children }: PropsWithChildren) => {
language: settings.language,
referrer: window.location.href,
search: input.search,
...bounds,
})}`
)
.then(result => result.json())
Expand Down Expand Up @@ -145,7 +158,7 @@ export const InputProvider = ({ children }: PropsWithChildren) => {
}, [input.mode, input.search]);

return (
<InputContext.Provider value={{ input, ...coordinates }}>
<InputContext.Provider value={{ input, setBounds, ...coordinates }}>
{children}
</InputContext.Provider>
);
Expand Down
80 changes: 53 additions & 27 deletions test/__tests__/load-meeting-data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,32 @@ describe('loadMeetingData', () => {
city: 'Anytown',
state: 'OK',
country: 'USA',
latitude: 37,
longitude: -122,
day: '0',
},
{
name: 'Other Meeting',
slug: 'other-meeting',
time: '09:00',
end_time: '10:00',
address: '123 Main St',
city: 'Anytown',
state: 'OK',
country: 'USA',
latitude: 38,
longitude: -121,
day: '0',
},
{
name: 'Inactive Meeting',
slug: 'inactive-meeting',
formatted_address: 'Anytown, OK, USA',
latitude: 38,
longitude: -121,
},
];
const { meetings, indexes, capabilities, slugs } = loadMeetingData(
const { bounds, meetings, indexes, capabilities, slugs } = loadMeetingData(
data,
{
coordinates: false,
Expand All @@ -43,8 +61,8 @@ describe('loadMeetingData', () => {
defaults.strings[defaults.language],
'America/Los_Angeles'
);
expect(meetings).toStrictEqual({
'test-meeting': {
expect(meetings['test-meeting']).toEqual(
expect.objectContaining({
address: '123 Main St',
approximate: false,
formatted_address: '123 Main St, Anytown, OK, USA',
Expand All @@ -57,70 +75,78 @@ describe('loadMeetingData', () => {
search: '123 main st, anytown, ok, usa\ttest meeting\tanytown',
slug: 'test-meeting',
types: ['in-person', 'active'],
},
'inactive-meeting': {
approximate: true,
formatted_address: 'Anytown, OK, USA',
isActive: false,
isInPerson: false,
isOnline: false,
isTempClosed: false,
name: 'Inactive Meeting',
regions: [],
search: 'anytown, ok, usa\tinactive meeting',
slug: 'inactive-meeting',
types: ['inactive'],
},
});
})
);
expect(indexes).toStrictEqual({
distance: [],
region: [
{
children: [],
key: 'anytown',
name: 'Anytown',
slugs: ['test-meeting'],
slugs: ['test-meeting', 'other-meeting'],
},
],
time: [
{
key: 'morning',
name: 'Morning',
slugs: ['test-meeting', 'other-meeting'],
},
{
key: 'appointment',
name: 'Appointment',
slugs: ['test-meeting', 'inactive-meeting'],
slugs: ['inactive-meeting'],
},
],
type: [
{
key: 'active',
name: 'Active',
slugs: ['test-meeting'],
slugs: ['test-meeting', 'other-meeting'],
},
{
key: 'in-person',
name: 'In-person',
slugs: ['test-meeting'],
slugs: ['test-meeting', 'other-meeting'],
},
{
key: 'inactive',
name: 'Inactive',
slugs: ['inactive-meeting'],
},
],
weekday: [],
weekday: [
{
key: 'sunday',
name: 'Sunday',
slugs: ['test-meeting', 'other-meeting'],
},
],
});
expect(capabilities).toStrictEqual({
coordinates: false,
coordinates: true,
distance: false,
geolocation: undefined,
inactive: true,
location: false,
region: true,
sharing: false,
time: false,
time: true,
type: true,
weekday: false,
weekday: true,
});
expect(slugs).toStrictEqual([
'test-meeting',
'other-meeting',
'inactive-meeting',
]);
expect(bounds).toStrictEqual({
north: '38',
south: '37',
east: '-121',
west: '-122',
});
expect(slugs).toStrictEqual(['test-meeting', 'inactive-meeting']);
});
});

Expand Down