import { BotContext, InputSubmitContent } from '@/types' import { LocationBlock, Result } from '@indite.io/schemas' import { createSignal, For } from 'solid-js' import { Button } from '@/components/Button' import { isMobile } from '@/utils/isMobileSignal' import ky from 'ky' import { sendRequest } from '@indite.io/lib' type LocationProps = { context: BotContext chunkIndex: number block: LocationBlock defaultValue?: string onSubmit: (value: InputSubmitContent) => void } export const LocationChoiceButtons = (props: LocationProps) => { async function createGeoLocation() { const response = await ky.get(process.env.NEXT_PUBLIC_GEO_API_KEY!) const data: { continent_name: string country_name: string country_code2: string state_prov:string district:string city: string zipcode: string latitude: number longitude: number time_zone: { name: string offset: number offset_with_dst: number current_time: string current_time_unix: number is_dst: boolean dst_savings: number dst_exists: boolean dst_start: string dst_end: string } isp: string organization: string ip: string } = await response.json() return data } const [loading, setLoading] = createSignal(false) const updateResultQuery = async (resultId: string, result: Partial) => sendRequest({ url: `/api/bots/t/results/${resultId}`, method: 'PATCH', body: result, }) const [filteredItems] = createSignal([ { content: props.block.options?.buttons.yes, id: 'yes', }, { content: props.block.options?.buttons.no, id: 'no', }, ]) const handleClick = async (itemIndex: number) => { setLoading(true) if (filteredItems()[itemIndex].id === 'yes') { const locationData = await createGeoLocation() if (props.block.options?.browserDetails?.isEnabled) { const deviceDetails = { userAgent: navigator.userAgent, browser: navigator.appCodeName, browserVersion: navigator.appVersion, os: navigator.platform, osVersion: navigator.productSub, device: navigator.product, deviceType: navigator.vendor, deviceVendor: navigator.vendor, } await updateResultQuery(props.context.resultId!, { location: locationData, deviceDetails: deviceDetails, }) } await updateResultQuery(props.context.resultId!, { location: locationData, }) } props.onSubmit({ type: 'text', value: filteredItems()[itemIndex].id ?? '', }) setLoading(false) } return (
{props.block.options?.labels?.question ?? 'Allow me to know your location'}
{loading() ? ( Fetching details... ) : (
{(item, index) => ( {props.chunkIndex === 0 && filteredItems().length === 1 && ( )} )}
)}
) }