UNPKG

infinity-forge

Version:
130 lines • 6.9 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.InputGoogleAutoComplete = InputGoogleAutoComplete; var jsx_runtime_1 = require("react/jsx-runtime"); var react_1 = require("react"); var formik_1 = require("formik"); var input_1 = require("../../input/index.js"); var input_cep_1 = require("../../input-cep/index.js"); var S = __importStar(require("./styles.js")); var atena_1 = require("../../../../../ui/atena/index.js"); function InputGoogleAutoComplete(props) { var _a; var _b = (0, formik_1.useFormikContext)(), values = _b.values, setValues = _b.setValues; var hasAutocompleteValue = (_a = values === null || values === void 0 ? void 0 : values.address) === null || _a === void 0 ? void 0 : _a.lat; var jsonContent = (0, atena_1.useSiteConfig)().jsonContent; var googleMapsApiKey = jsonContent === null || jsonContent === void 0 ? void 0 : jsonContent.googleMapsApiKey; (0, react_1.useEffect)(function () { var inputId = 'address.fullAddress'; function initAutocomplete() { var tryInit = function () { var _a, _b; var inputEl = document.getElementById(inputId); if (!inputEl || !((_b = (_a = window.google) === null || _a === void 0 ? void 0 : _a.maps) === null || _b === void 0 ? void 0 : _b.places)) { setTimeout(tryInit, 300); return; } var autocomplete = new window.google.maps.places.Autocomplete(inputEl, { componentRestrictions: { country: 'br' }, fields: ['geometry', 'address_components', 'formatted_address'], types: ['address'], }); autocomplete.addListener('place_changed', function () { var _a, _b; var place = autocomplete.getPlace(); if (!((_a = place === null || place === void 0 ? void 0 : place.geometry) === null || _a === void 0 ? void 0 : _a.location)) return; var lat = place.geometry.location.lat(); var lng = place.geometry.location.lng(); var components = place.address_components || []; var get = function (type) { var _a; return ((_a = components.find(function (c) { return c.types.includes(type); })) === null || _a === void 0 ? void 0 : _a.long_name) || ''; }; var data = { complemento: get('subpremise'), zipCode: get('postal_code'), logradouro: get('route'), bairro: get('sublocality') || get('neighborhood'), numero: get('street_number'), uf: get('administrative_area_level_1'), localidade: get('administrative_area_level_2'), lat: String(lat), lng: String(lng), fullAddress: place.formatted_address || '', }; setValues(function (prev) { return (__assign(__assign({}, prev), { address: data })); }); (_b = props.onPlaceSelected) === null || _b === void 0 ? void 0 : _b.call(props, data); }); }; tryInit(); } if (!props.disabledScriptLoad) { if (!document.querySelector('#google-places-script')) { var script = document.createElement('script'); script.id = 'google-places-script'; script.src = "https://maps.googleapis.com/maps/api/js?key=".concat(googleMapsApiKey, "&libraries=places"); script.async = true; script.defer = true; script.onload = initAutocomplete; document.body.appendChild(script); } else { initAutocomplete(); } } else { initAutocomplete(); } }, []); if (typeof window !== 'undefined' && window.location.hostname === 'localhost' && !googleMapsApiKey) { var isLocalhost = window.location.hostname === 'localhost'; var href = isLocalhost ? "http://".concat(window.location.hostname).concat(window.location.port ? ":".concat(window.location.port) : '', "/configurations/site/") : "https://".concat(process.env.tenant || window.location.pathname, "/configurations/site/"); return ((0, jsx_runtime_1.jsx)("a", { href: href, className: 'font-18-bold', children: "API do google est\u00E1 faltando, clique aqui para configurar" })); } return ((0, jsx_runtime_1.jsxs)(S.Autocomplete, { children: [(0, jsx_runtime_1.jsx)(input_1.Input, __assign({}, props, { name: 'address.fullAddress', type: 'text', id: 'address.fullAddress', i18n: { disabled: true } })), hasAutocompleteValue && (props === null || props === void 0 ? void 0 : props.enableAddressFieleds) && (0, jsx_runtime_1.jsx)(input_cep_1.InputCep, { disableSearchCep: true, showAllFields: true })] })); } //# sourceMappingURL=index.js.map