@nuvler/cli 0.5.5 → 0.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -421,12 +421,94 @@ function formatWhatsapp(value: string): string {
|
|
|
421
421
|
: `(${digits.slice(0, 2)}) ${number.slice(0, 5)}-${number.slice(5)}`;
|
|
422
422
|
}
|
|
423
423
|
|
|
424
|
+
function formatCep(value: string): string {
|
|
425
|
+
const digits = value.replace(/\D/g, "").slice(0, 8);
|
|
426
|
+
return digits.length > 5 ? `${digits.slice(0, 5)}-${digits.slice(5)}` : digits;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
const addressFieldAliases = {
|
|
430
|
+
address: ["address", "endereco", "logradouro"],
|
|
431
|
+
district: ["district", "bairro"],
|
|
432
|
+
city: ["city", "cidade", "municipio"],
|
|
433
|
+
state: ["state", "estado", "uf"],
|
|
434
|
+
} as const;
|
|
435
|
+
|
|
424
436
|
function ContactFormComponent({ section, sdk }: ThemeSectionRendererProps) {
|
|
425
437
|
const props = dataRecord(section.data, "props");
|
|
426
438
|
const configuredFields = dataRecords(props, "fields");
|
|
427
439
|
const fields = configuredFields;
|
|
428
440
|
const [status, setStatus] = useState<"idle" | "sending" | "success" | "error">("idle");
|
|
429
441
|
const [error, setError] = useState("");
|
|
442
|
+
const [cepStatus, setCepStatus] = useState<"idle" | "loading" | "success" | "error">("idle");
|
|
443
|
+
const [cepMessage, setCepMessage] = useState("");
|
|
444
|
+
const formRef = useRef<HTMLFormElement>(null);
|
|
445
|
+
const lastSearchedCep = useRef("");
|
|
446
|
+
const cepController = useRef<AbortController | null>(null);
|
|
447
|
+
|
|
448
|
+
useEffect(() => () => {
|
|
449
|
+
cepController.current?.abort();
|
|
450
|
+
cepController.current = null;
|
|
451
|
+
}, []);
|
|
452
|
+
|
|
453
|
+
const searchCep = async (value: string) => {
|
|
454
|
+
const cleanCep = value.replace(/\D/g, "");
|
|
455
|
+
if (cleanCep.length !== 8) {
|
|
456
|
+
cepController.current?.abort();
|
|
457
|
+
cepController.current = null;
|
|
458
|
+
lastSearchedCep.current = "";
|
|
459
|
+
setCepStatus("idle");
|
|
460
|
+
setCepMessage("");
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
if (cleanCep === lastSearchedCep.current) return;
|
|
464
|
+
|
|
465
|
+
cepController.current?.abort();
|
|
466
|
+
const controller = new AbortController();
|
|
467
|
+
cepController.current = controller;
|
|
468
|
+
lastSearchedCep.current = cleanCep;
|
|
469
|
+
const timeout = window.setTimeout(() => controller.abort(), 5000);
|
|
470
|
+
setCepStatus("loading");
|
|
471
|
+
setCepMessage("Buscando endereço…");
|
|
472
|
+
|
|
473
|
+
try {
|
|
474
|
+
const response = await fetch(`https://viacep.com.br/ws/${cleanCep}/json/`, {
|
|
475
|
+
signal: controller.signal,
|
|
476
|
+
});
|
|
477
|
+
if (!response.ok) throw new Error("CEP_FETCH_FAILED");
|
|
478
|
+
const data = await response.json() as { erro?: boolean; logradouro?: string; bairro?: string; localidade?: string; uf?: string };
|
|
479
|
+
if (data.erro) {
|
|
480
|
+
setCepStatus("error");
|
|
481
|
+
setCepMessage("CEP não encontrado. Confira o número ou preencha o endereço manualmente.");
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
const addressValues = {
|
|
486
|
+
address: data.logradouro || "",
|
|
487
|
+
district: data.bairro || "",
|
|
488
|
+
city: data.localidade || "",
|
|
489
|
+
state: data.uf || "",
|
|
490
|
+
};
|
|
491
|
+
const configuredKeys = new Set(fields.map((field) => dataString(field, "key")));
|
|
492
|
+
for (const [part, aliases] of Object.entries(addressFieldAliases)) {
|
|
493
|
+
const targetKey = aliases.find((alias) => configuredKeys.has(alias));
|
|
494
|
+
const nextValue = addressValues[part as keyof typeof addressValues];
|
|
495
|
+
const target = targetKey
|
|
496
|
+
? formRef.current?.elements.namedItem(targetKey)
|
|
497
|
+
: null;
|
|
498
|
+
if (nextValue && target instanceof HTMLInputElement) target.value = nextValue;
|
|
499
|
+
}
|
|
500
|
+
setCepStatus("success");
|
|
501
|
+
setCepMessage("Endereço preenchido. Confira os dados e informe o número, se necessário.");
|
|
502
|
+
} catch {
|
|
503
|
+
if (controller.signal.aborted && cepController.current !== controller) return;
|
|
504
|
+
setCepStatus("error");
|
|
505
|
+
setCepMessage(controller.signal.aborted
|
|
506
|
+
? "A consulta demorou demais. Você pode preencher o endereço manualmente."
|
|
507
|
+
: "Não foi possível consultar o CEP. Preencha o endereço manualmente.");
|
|
508
|
+
} finally {
|
|
509
|
+
window.clearTimeout(timeout);
|
|
510
|
+
}
|
|
511
|
+
};
|
|
430
512
|
|
|
431
513
|
const submit = async (event: FormEvent<HTMLFormElement>) => {
|
|
432
514
|
event.preventDefault();
|
|
@@ -457,6 +539,9 @@ function ContactFormComponent({ section, sdk }: ThemeSectionRendererProps) {
|
|
|
457
539
|
pathname: typeof window === "undefined" ? undefined : window.location.pathname,
|
|
458
540
|
});
|
|
459
541
|
event.currentTarget.reset();
|
|
542
|
+
lastSearchedCep.current = "";
|
|
543
|
+
setCepStatus("idle");
|
|
544
|
+
setCepMessage("");
|
|
460
545
|
setStatus("success");
|
|
461
546
|
} catch (cause) {
|
|
462
547
|
setStatus("error");
|
|
@@ -470,7 +555,7 @@ function ContactFormComponent({ section, sdk }: ThemeSectionRendererProps) {
|
|
|
470
555
|
<h2>{dataString(props, "title", "Fale com nossa equipe")}</h2>
|
|
471
556
|
{dataString(props, "description") && <p>{dataString(props, "description")}</p>}
|
|
472
557
|
</div>
|
|
473
|
-
<form className="contact-form" onSubmit={(event) => void submit(event)}>
|
|
558
|
+
<form className="contact-form" ref={formRef} onSubmit={(event) => void submit(event)}>
|
|
474
559
|
{fields.map((field) => {
|
|
475
560
|
const key = dataString(field, "key");
|
|
476
561
|
if (!key) return null;
|
|
@@ -489,10 +574,11 @@ function ContactFormComponent({ section, sdk }: ThemeSectionRendererProps) {
|
|
|
489
574
|
<span>{label}{required && <small aria-hidden="true"> *</small>}</span>
|
|
490
575
|
{type === "textarea"
|
|
491
576
|
? <textarea name={key} placeholder={dataString(field, "placeholder")} required={required} rows={5} />
|
|
492
|
-
: <input name={key} placeholder={dataString(field, "placeholder")} required={required} type={["email", "tel", "url", "number"].includes(type) ? type : "text"} maxLength={dataString(field, "mask") === "whatsapp-br" ? 15 : undefined} onInput={dataString(field, "mask") === "whatsapp-br" ? (event) => { event.currentTarget.value = formatWhatsapp(event.currentTarget.value); } : undefined} />}
|
|
577
|
+
: <input name={key} placeholder={dataString(field, "placeholder")} required={required} type={["email", "tel", "url", "number"].includes(type) ? type : "text"} maxLength={dataString(field, "mask") === "whatsapp-br" ? 15 : dataString(field, "mask") === "cep-br" ? 9 : undefined} inputMode={dataString(field, "mask") === "cep-br" ? "numeric" : undefined} autoComplete={dataString(field, "mask") === "cep-br" ? "postal-code" : undefined} onInput={dataString(field, "mask") === "whatsapp-br" ? (event) => { event.currentTarget.value = formatWhatsapp(event.currentTarget.value); } : dataString(field, "mask") === "cep-br" ? (event) => { event.currentTarget.value = formatCep(event.currentTarget.value); void searchCep(event.currentTarget.value); } : undefined} />}
|
|
493
578
|
</label>;
|
|
494
579
|
})}
|
|
495
580
|
<div className="contact-form-wide">
|
|
581
|
+
{cepStatus !== "idle" && <p className={`contact-form-feedback${cepStatus === "error" ? " contact-form-error" : ""}`} role={cepStatus === "error" ? "alert" : "status"}>{cepMessage}</p>}
|
|
496
582
|
<button className="button button-primary" disabled={status === "sending"} type="submit">
|
|
497
583
|
{status === "sending" ? "Enviando…" : dataString(props, "submitLabel", "Enviar contato")}
|
|
498
584
|
</button>
|