2021-05-20 08:19:37 +00:00
|
|
|
import * as chrono from 'chrono-node';
|
2021-05-30 16:56:48 +00:00
|
|
|
import { getEndpoint } from 'utils/common/apiUtil';
|
|
|
|
import { getToken } from 'utils/common/key';
|
|
|
|
import { DateValue, Suggestion, SuggestionType } from 'components/SearchBar';
|
2021-05-29 06:27:52 +00:00
|
|
|
import HTTPService from './HTTPService';
|
2021-05-26 10:18:13 +00:00
|
|
|
|
|
|
|
const ENDPOINT = getEndpoint();
|
2021-05-28 17:27:08 +00:00
|
|
|
const DIGITS = new Set(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']);
|
2021-05-21 09:47:28 +00:00
|
|
|
export type Bbox = [number, number, number, number];
|
|
|
|
export interface LocationSearchResponse {
|
2021-05-26 10:18:13 +00:00
|
|
|
place: string;
|
2021-05-21 09:47:28 +00:00
|
|
|
bbox: Bbox;
|
|
|
|
}
|
2021-05-29 06:27:52 +00:00
|
|
|
export const getMapboxToken = () => process.env.NEXT_PUBLIC_MAPBOX_TOKEN;
|
2021-05-20 07:50:20 +00:00
|
|
|
|
2021-05-28 16:25:19 +00:00
|
|
|
export function parseHumanDate(humanDate: string): DateValue[] {
|
|
|
|
const date = chrono.parseDate(humanDate);
|
2021-05-29 06:27:52 +00:00
|
|
|
const date1 = chrono.parseDate(`${humanDate} 1`);
|
|
|
|
if (date !== null) {
|
2021-05-28 17:27:08 +00:00
|
|
|
const dates = [
|
2021-05-30 16:56:48 +00:00
|
|
|
{ month: date.getMonth() },
|
|
|
|
{ date: date.getDate(), month: date.getMonth() },
|
2021-05-28 16:25:19 +00:00
|
|
|
];
|
2021-05-28 17:27:08 +00:00
|
|
|
let reverse = false;
|
|
|
|
humanDate.split('').forEach((c) => {
|
|
|
|
if (DIGITS.has(c)) {
|
|
|
|
reverse = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (reverse) {
|
|
|
|
return dates.reverse();
|
|
|
|
}
|
2021-05-29 06:27:52 +00:00
|
|
|
return dates;
|
|
|
|
} if (date1) {
|
2021-05-30 16:56:48 +00:00
|
|
|
return [{ month: date1.getMonth() }];
|
2021-05-28 16:25:19 +00:00
|
|
|
}
|
2021-05-29 06:27:52 +00:00
|
|
|
return [];
|
2021-05-20 08:19:37 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 07:50:20 +00:00
|
|
|
export async function searchLocation(
|
2021-05-29 06:27:52 +00:00
|
|
|
searchPhrase: string,
|
2021-05-21 09:47:28 +00:00
|
|
|
): Promise<LocationSearchResponse[]> {
|
2021-05-26 11:49:28 +00:00
|
|
|
const resp = await HTTPService.get(
|
|
|
|
`${ENDPOINT}/search/location`,
|
|
|
|
{
|
|
|
|
query: searchPhrase,
|
|
|
|
limit: 4,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'X-Auth-Token': getToken(),
|
2021-05-29 06:27:52 +00:00
|
|
|
},
|
2021-05-26 11:49:28 +00:00
|
|
|
);
|
|
|
|
return resp.data.results;
|
2021-05-20 07:50:20 +00:00
|
|
|
}
|
2021-05-28 16:25:19 +00:00
|
|
|
|
|
|
|
export function getHolidaySuggestion(searchPhrase: string): Suggestion[] {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
label: 'Christmas',
|
2021-05-30 16:56:48 +00:00
|
|
|
value: { month: 11, date: 25 },
|
2021-05-28 16:25:19 +00:00
|
|
|
type: SuggestionType.DATE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Christmas Eve',
|
2021-05-30 16:56:48 +00:00
|
|
|
value: { month: 11, date: 24 },
|
2021-05-28 16:25:19 +00:00
|
|
|
type: SuggestionType.DATE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'New Year',
|
2021-05-30 16:56:48 +00:00
|
|
|
value: { month: 0, date: 1 },
|
2021-05-28 16:25:19 +00:00
|
|
|
type: SuggestionType.DATE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'New Year Eve',
|
2021-05-30 16:56:48 +00:00
|
|
|
value: { month: 11, date: 31 },
|
2021-05-28 16:25:19 +00:00
|
|
|
type: SuggestionType.DATE,
|
|
|
|
},
|
2021-05-29 06:27:52 +00:00
|
|
|
].filter((suggestion) => suggestion.label.toLowerCase().includes(searchPhrase.toLowerCase()));
|
2021-05-28 16:25:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getYearSuggestion(searchPhrase: string): Suggestion[] {
|
2021-05-29 06:27:52 +00:00
|
|
|
if (searchPhrase.length === 4) {
|
2021-05-28 16:25:19 +00:00
|
|
|
try {
|
|
|
|
const year = parseInt(searchPhrase);
|
|
|
|
if (year >= 1970 && year <= new Date().getFullYear()) {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
label: searchPhrase,
|
2021-05-30 16:56:48 +00:00
|
|
|
value: { year },
|
2021-05-28 16:25:19 +00:00
|
|
|
type: SuggestionType.DATE,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2021-05-29 06:27:52 +00:00
|
|
|
// ignore
|
2021-05-28 16:25:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|