mitre-form-component 0.0.44 → 0.0.45

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/dist/index.js CHANGED
@@ -1079,139 +1079,8 @@ var PhoneInputBase = ({
1079
1079
  };
1080
1080
  var PhoneInput2 = forwardRef2(PhoneInputBase);
1081
1081
 
1082
- // src/components/ProductSelect/index.tsx
1083
- import {
1084
- forwardRef as forwardRef3
1085
- } from "react";
1086
-
1087
- // src/components/ProductSelect/styles.ts
1088
- import styled6, { css as css5 } from "styled-components";
1089
- var FormLabel3 = styled6.label`
1090
- font-family: "Montserrat", sans-serif;
1091
- font-style: normal;
1092
- font-weight: 500;
1093
- font-size: 1rem;
1094
- color: ${(props) => props.$textColor || "var(--black)"};
1095
- display: block;
1096
- margin-bottom: 0.5rem;
1097
- text-align: left;
1098
- `;
1099
- var Select = styled6.select`
1100
- font-family: "Montserrat", sans-serif;
1101
- font-style: normal;
1102
- font-weight: 500;
1103
- font-size: 1rem;
1104
- line-height: 1.5rem;
1105
- background: ${(props) => props.$backgroundColor || "var(--white)"};
1106
- color: ${(props) => props.$textColor || "var(--black)"};
1107
- padding: 0.5rem 3rem 0.5rem 0.5rem; /* Aumentado padding-right para 3rem */
1108
- border-radius: 0.125rem;
1109
- border: 1px solid ${(props) => props.$borderColor || "transparent"};
1110
- display: block;
1111
- height: 3.125rem;
1112
- width: 100%;
1113
- cursor: pointer;
1114
-
1115
- /* Remover a seta padrão do navegador */
1116
- -webkit-appearance: none;
1117
- -moz-appearance: none;
1118
- appearance: none;
1119
-
1120
- /* Adicionar seta personalizada maior */
1121
- background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6,9 12,15 18,9'%3e%3c/polyline%3e%3c/svg%3e");
1122
- background-repeat: no-repeat;
1123
- background-position: right 1rem center; /* Posicionado a 1rem da direita */
1124
- background-size: 1.25rem; /* Tamanho da seta aumentado para 1.25rem */
1125
-
1126
- &:focus {
1127
- border-radius: 0.125rem;
1128
- border: 2px solid ${(props) => props.$focusBorderColor || "var(--yellow-500)"};
1129
- outline: none;
1130
- }
1131
-
1132
- /* Estilo para option disabled (placeholder) */
1133
- option:disabled {
1134
- color: var(--gray-100);
1135
- font-weight: 800;
1136
- }
1137
-
1138
- /* Estilo para options normais */
1139
- option:not(:disabled) {
1140
- color: ${(props) => props.$textColor || "var(--black)"};
1141
- font-weight: 500;
1142
- }
1143
- `;
1144
- var FormErrorMessage3 = styled6.small`
1145
- font-size: 0.75rem;
1146
- line-height: 1.125rem;
1147
- color: var(--red);
1148
- margin-top: 0.25rem;
1149
- display: block;
1150
- `;
1151
- var FormControl3 = styled6.div.withConfig({
1152
- shouldForwardProp: (prop) => !["isInvalid"].includes(prop)
1153
- })`
1154
- ${FormLabel3} {
1155
- ${(props) => props.isInvalid && css5`
1156
- color: var(--red);
1157
- `};
1158
- }
1159
-
1160
- ${Select} {
1161
- ${(props) => props.isInvalid && css5`
1162
- border: 1px solid var(--red);
1163
- `};
1164
-
1165
- &:focus {
1166
- ${(props) => props.isInvalid && css5`
1167
- border: 1px solid var(--red);
1168
- `};
1169
- }
1170
- }
1171
- `;
1172
-
1173
- // src/components/ProductSelect/index.tsx
1174
- import { jsx as jsx5, jsxs as jsxs5 } from "react/jsx-runtime";
1175
- var ProductSelectBase = ({
1176
- id,
1177
- label,
1178
- error,
1179
- showErrorMessage = true,
1180
- labelTextColor,
1181
- backgroundColor,
1182
- borderColor,
1183
- focusBorderColor,
1184
- textColor,
1185
- products,
1186
- placeholder = "Selecione um produto",
1187
- ...rest
1188
- }, ref) => {
1189
- return /* @__PURE__ */ jsxs5(FormControl3, { isInvalid: !!error, children: [
1190
- !!label && /* @__PURE__ */ jsx5(FormLabel3, { htmlFor: id, $textColor: labelTextColor, children: label }),
1191
- /* @__PURE__ */ jsxs5(
1192
- Select,
1193
- {
1194
- id,
1195
- ref,
1196
- "aria-invalid": !!error && showErrorMessage ? "true" : "false",
1197
- $backgroundColor: backgroundColor,
1198
- $borderColor: borderColor,
1199
- $focusBorderColor: focusBorderColor,
1200
- $textColor: textColor,
1201
- ...rest,
1202
- children: [
1203
- /* @__PURE__ */ jsx5("option", { value: "", disabled: true, children: placeholder }),
1204
- products.map((product) => /* @__PURE__ */ jsx5("option", { value: product.id, children: product.name }, product.id))
1205
- ]
1206
- }
1207
- ),
1208
- !!error && showErrorMessage && /* @__PURE__ */ jsx5(FormErrorMessage3, { "data-testid": "error-message", children: typeof error === "string" ? error : error.message })
1209
- ] });
1210
- };
1211
- var ProductSelect = forwardRef3(ProductSelectBase);
1212
-
1213
1082
  // src/components/Form/index.tsx
1214
- import { Fragment, jsx as jsx6, jsxs as jsxs6 } from "react/jsx-runtime";
1083
+ import { Fragment, jsx as jsx5, jsxs as jsxs5 } from "react/jsx-runtime";
1215
1084
  var phoneUtil = PhoneNumberUtil.getInstance();
1216
1085
  var isPhoneValid = (phone) => {
1217
1086
  try {
@@ -1221,7 +1090,7 @@ var isPhoneValid = (phone) => {
1221
1090
  return false;
1222
1091
  }
1223
1092
  };
1224
- var baseSchema = {
1093
+ var schema = yup.object().shape({
1225
1094
  name: yup.string().required("Nome \xE9 obrigat\xF3rio"),
1226
1095
  email: yup.string().required("Email \xE9 obrigat\xF3rio").email("Email inv\xE1lido"),
1227
1096
  phone: yup.object().shape({
@@ -1236,14 +1105,9 @@ var baseSchema = {
1236
1105
  inputValue: yup.string().required("Telefone \xE9 obrigat\xF3rio!"),
1237
1106
  dialCode: yup.string().required("C\xF3digo de pa\xEDs \xE9 obrigat\xF3rio")
1238
1107
  })
1239
- };
1240
- var schemaWithProduct = yup.object().shape({
1241
- ...baseSchema,
1242
- productId: yup.string().required("Produto \xE9 obrigat\xF3rio")
1243
1108
  });
1244
- var schema = yup.object().shape(baseSchema);
1245
1109
  var MitreFormComponent = React3.forwardRef(({
1246
- products,
1110
+ productId,
1247
1111
  apiUrl,
1248
1112
  apiToken,
1249
1113
  showHeader = true,
@@ -1256,42 +1120,17 @@ var MitreFormComponent = React3.forwardRef(({
1256
1120
  inputBorderColor = "var(--transparent)",
1257
1121
  inputFocusBorderColor = "var(--yellow-500)",
1258
1122
  inputPlaceholderColor = "var(--gray-100)",
1259
- inputTextColor = "var(--black)"
1123
+ inputTextColor = "var(--black)",
1124
+ onSuccess,
1125
+ onError
1260
1126
  }, ref) => {
1261
1127
  const [loading, setIsLoading] = useState3(false);
1262
1128
  const { error, handleError, clearError } = useError();
1263
1129
  const [successMessage, setSuccessMessage] = useState3("");
1264
1130
  const [formKey, setFormKey] = useState3(0);
1265
- const [selectedProductId, setSelectedProductId] = useState3(null);
1266
- const validateProducts = () => {
1267
- if (!products) {
1268
- return "Lista de produtos n\xE3o foi fornecida";
1269
- }
1270
- if (!Array.isArray(products)) {
1271
- return "Lista de produtos deve ser um array";
1272
- }
1273
- if (products.length === 0) {
1274
- return "Lista de produtos n\xE3o pode estar vazia";
1275
- }
1276
- for (let i = 0; i < products.length; i++) {
1277
- const product = products[i];
1278
- if (!product) {
1279
- return `Produto na posi\xE7\xE3o ${i} \xE9 inv\xE1lido (nulo/undefined)`;
1280
- }
1281
- if (typeof product.id !== "number" || product.id <= 0) {
1282
- return `Produto na posi\xE7\xE3o ${i} possui ID inv\xE1lido (deve ser um n\xFAmero positivo)`;
1283
- }
1284
- if (typeof product.name !== "string" || product.name.trim().length === 0) {
1285
- return `Produto na posi\xE7\xE3o ${i} possui nome inv\xE1lido (deve ser uma string n\xE3o vazia)`;
1286
- }
1287
- }
1288
- return null;
1289
- };
1290
- const productsValidationError = validateProducts();
1291
1131
  const [utm, setUtm] = useState3({ utm_source: "direto", createdAt: (/* @__PURE__ */ new Date()).toISOString() });
1292
- const formSchema = products && products.length > 1 ? schemaWithProduct : schema;
1293
1132
  const { control, register, handleSubmit, formState: { errors }, reset, clearErrors } = useForm({
1294
- resolver: yupResolver(formSchema),
1133
+ resolver: yupResolver(schema),
1295
1134
  mode: "onSubmit"
1296
1135
  });
1297
1136
  React3.useEffect(() => {
@@ -1300,16 +1139,11 @@ var MitreFormComponent = React3.forwardRef(({
1300
1139
  setUtm(data);
1301
1140
  }, []);
1302
1141
  const resetForm = () => {
1303
- const resetData = {
1142
+ reset({
1304
1143
  name: "",
1305
1144
  email: "",
1306
1145
  phone: { phone: "", inputValue: "", dialCode: "" }
1307
- };
1308
- if (products && products.length > 1) {
1309
- resetData.productId = "";
1310
- setSelectedProductId(null);
1311
- }
1312
- reset(resetData, {
1146
+ }, {
1313
1147
  keepErrors: false,
1314
1148
  keepDirty: false,
1315
1149
  keepTouched: false,
@@ -1322,40 +1156,26 @@ var MitreFormComponent = React3.forwardRef(({
1322
1156
  setFormKey((k) => k + 1);
1323
1157
  };
1324
1158
  const sendMessage = async (data) => {
1325
- const { name, email, phone, productId } = data;
1159
+ const { name, email, phone } = data;
1326
1160
  const phoneValue = phone.inputValue;
1327
1161
  const phoneDigitsOnly = phoneValue?.replace(/\D/g, "") || "";
1328
1162
  const message = "Gostaria de mais informa\xE7\xF5es sobre o produto";
1329
1163
  try {
1330
1164
  setIsLoading(true);
1331
- if (!products || products.length === 0 || !apiToken) {
1165
+ if (!productId || !apiToken) {
1332
1166
  throw new Error("Par\xE2metros obrigat\xF3rios n\xE3o informados");
1333
1167
  }
1334
- let selectedProduct;
1335
- if (products.length === 1) {
1336
- selectedProduct = products[0];
1337
- } else {
1338
- if (!productId) {
1339
- throw new Error("Produto deve ser selecionado");
1340
- }
1341
- const foundProduct = products.find((p) => p.id.toString() === productId);
1342
- if (!foundProduct) {
1343
- throw new Error("Produto selecionado n\xE3o encontrado");
1344
- }
1345
- selectedProduct = foundProduct;
1346
- }
1347
1168
  const requestBody = {
1348
1169
  name,
1349
1170
  email,
1350
1171
  phone: phoneDigitsOnly,
1351
1172
  message,
1352
- productId: selectedProduct.id.toString(),
1173
+ productId,
1353
1174
  utm_source: utm.utm_source,
1354
1175
  utm_campaign: utm.utm_campaign,
1355
1176
  utm_medium: utm.utm_medium,
1356
1177
  utm_term: utm.utm_term
1357
1178
  };
1358
- console.log("requestBody = " + JSON.stringify(requestBody));
1359
1179
  const response = await fetch(`${apiUrl}/leads`, {
1360
1180
  method: "POST",
1361
1181
  headers: {
@@ -1365,32 +1185,25 @@ var MitreFormComponent = React3.forwardRef(({
1365
1185
  body: JSON.stringify(requestBody)
1366
1186
  });
1367
1187
  if (!response.ok) {
1368
- console.log("response = " + JSON.stringify(response));
1369
1188
  throw new Error("Falha ao enviar a mensagem!");
1370
1189
  }
1190
+ const responseData = await response.json();
1191
+ const leadId = responseData.leadId || responseData.id || "";
1371
1192
  setSuccessMessage("Mensagem enviada com sucesso!");
1372
1193
  resetForm();
1194
+ onSuccess?.(requestBody, leadId);
1373
1195
  } catch (err) {
1196
+ const error2 = err instanceof Error ? err : new Error("Erro desconhecido");
1374
1197
  handleError(err);
1198
+ onError?.(error2);
1375
1199
  } finally {
1376
1200
  setIsLoading(false);
1377
1201
  }
1378
1202
  };
1379
- if (productsValidationError) {
1380
- console.error("Erro na valida\xE7\xE3o dos produtos:", productsValidationError);
1381
- return /* @__PURE__ */ jsxs6(Fragment, { children: [
1382
- /* @__PURE__ */ jsx6(global_default, {}),
1383
- /* @__PURE__ */ jsx6(GlobalStyles, {}),
1384
- /* @__PURE__ */ jsx6(FormContainer, { $backgroundColor: backgroundColor, $innerPadding: innerPadding, ref, children: /* @__PURE__ */ jsxs6(HeaderContainer, { children: [
1385
- /* @__PURE__ */ jsx6(Title, { $textColor: textColor, children: "Erro no carregamento do formul\xE1rio!" }),
1386
- /* @__PURE__ */ jsx6(Text, { $textColor: textColor, children: "N\xE3o foi poss\xEDvel carregar o formul\xE1rio. Tente novamente mais tarde." })
1387
- ] }) })
1388
- ] });
1389
- }
1390
- return /* @__PURE__ */ jsxs6(Fragment, { children: [
1391
- /* @__PURE__ */ jsx6(global_default, {}),
1392
- /* @__PURE__ */ jsx6(GlobalStyles, {}),
1393
- error && /* @__PURE__ */ jsx6(
1203
+ return /* @__PURE__ */ jsxs5(Fragment, { children: [
1204
+ /* @__PURE__ */ jsx5(global_default, {}),
1205
+ /* @__PURE__ */ jsx5(GlobalStyles, {}),
1206
+ error && /* @__PURE__ */ jsx5(
1394
1207
  Alert,
1395
1208
  {
1396
1209
  type: "error",
@@ -1400,7 +1213,7 @@ var MitreFormComponent = React3.forwardRef(({
1400
1213
  children: error.message
1401
1214
  }
1402
1215
  ),
1403
- successMessage && /* @__PURE__ */ jsx6(
1216
+ successMessage && /* @__PURE__ */ jsx5(
1404
1217
  Alert,
1405
1218
  {
1406
1219
  type: "success",
@@ -1410,30 +1223,13 @@ var MitreFormComponent = React3.forwardRef(({
1410
1223
  children: successMessage
1411
1224
  }
1412
1225
  ),
1413
- /* @__PURE__ */ jsxs6(FormContainer, { $backgroundColor: backgroundColor, $innerPadding: innerPadding, ref, children: [
1414
- showHeader && /* @__PURE__ */ jsxs6(HeaderContainer, { children: [
1415
- /* @__PURE__ */ jsx6(Title, { $textColor: textColor, children: "Atendimento por mensagem" }),
1416
- /* @__PURE__ */ jsx6(Text, { $textColor: textColor, children: "Informe seus dados e retornaremos a mensagem." })
1226
+ /* @__PURE__ */ jsxs5(FormContainer, { $backgroundColor: backgroundColor, $innerPadding: innerPadding, ref, children: [
1227
+ showHeader && /* @__PURE__ */ jsxs5(HeaderContainer, { children: [
1228
+ /* @__PURE__ */ jsx5(Title, { $textColor: textColor, children: "Atendimento por mensagem" }),
1229
+ /* @__PURE__ */ jsx5(Text, { $textColor: textColor, children: "Informe seus dados e retornaremos a mensagem." })
1417
1230
  ] }),
1418
- /* @__PURE__ */ jsxs6(Form, { $textColor: textColor, onSubmit: handleSubmit(sendMessage), noValidate: true, children: [
1419
- products.length > 1 && /* @__PURE__ */ jsx6(
1420
- ProductSelect,
1421
- {
1422
- id: "productId",
1423
- label: "Produto de interesse *",
1424
- placeholder: "Selecione um produto",
1425
- ...register("productId"),
1426
- error: errors.productId?.message,
1427
- products,
1428
- required: true,
1429
- labelTextColor: textColor,
1430
- backgroundColor: inputBackgroundColor,
1431
- borderColor: inputBorderColor,
1432
- focusBorderColor: inputFocusBorderColor,
1433
- textColor: inputTextColor
1434
- }
1435
- ),
1436
- /* @__PURE__ */ jsx6(
1231
+ /* @__PURE__ */ jsxs5(Form, { $textColor: textColor, onSubmit: handleSubmit(sendMessage), noValidate: true, children: [
1232
+ /* @__PURE__ */ jsx5(
1437
1233
  Input2,
1438
1234
  {
1439
1235
  id: "name",
@@ -1451,7 +1247,7 @@ var MitreFormComponent = React3.forwardRef(({
1451
1247
  inputTextColor
1452
1248
  }
1453
1249
  ),
1454
- /* @__PURE__ */ jsx6(
1250
+ /* @__PURE__ */ jsx5(
1455
1251
  Input2,
1456
1252
  {
1457
1253
  id: "email",
@@ -1470,7 +1266,7 @@ var MitreFormComponent = React3.forwardRef(({
1470
1266
  inputTextColor
1471
1267
  }
1472
1268
  ),
1473
- /* @__PURE__ */ jsx6(
1269
+ /* @__PURE__ */ jsx5(
1474
1270
  Controller,
1475
1271
  {
1476
1272
  name: "phone",
@@ -1479,7 +1275,7 @@ var MitreFormComponent = React3.forwardRef(({
1479
1275
  shouldUnregister: true,
1480
1276
  render: ({ field }) => {
1481
1277
  const errorMsg = errors.phone?.inputValue?.message || errors.phone?.phone?.message || errors.phone?.message;
1482
- return /* @__PURE__ */ jsx6(
1278
+ return /* @__PURE__ */ jsx5(
1483
1279
  PhoneInput2,
1484
1280
  {
1485
1281
  id: "phone",
@@ -1502,12 +1298,12 @@ var MitreFormComponent = React3.forwardRef(({
1502
1298
  },
1503
1299
  formKey
1504
1300
  ),
1505
- /* @__PURE__ */ jsx6("h6", { children: "* Campos de preenchimento obrigat\xF3rio." }),
1506
- /* @__PURE__ */ jsx6(ButtonContainer, { children: /* @__PURE__ */ jsx6(Button2, { bgColor: buttonBackgroundColor, color: buttonTextColor, type: "submit", isSubmitting: loading, children: "Enviar mensagem" }) }),
1507
- /* @__PURE__ */ jsxs6("p", { children: [
1301
+ /* @__PURE__ */ jsx5("h6", { children: "* Campos de preenchimento obrigat\xF3rio." }),
1302
+ /* @__PURE__ */ jsx5(ButtonContainer, { children: /* @__PURE__ */ jsx5(Button2, { bgColor: buttonBackgroundColor, color: buttonTextColor, type: "submit", isSubmitting: loading, children: "Enviar mensagem" }) }),
1303
+ /* @__PURE__ */ jsxs5("p", { children: [
1508
1304
  "A Mitre Realty respeita a sua privacidade e utiliza os seus dados pessoais para contat\xE1-lo por e-mail ou telefone aqui registrados. Para saber mais, acesse a nossa",
1509
1305
  " ",
1510
- /* @__PURE__ */ jsx6(
1306
+ /* @__PURE__ */ jsx5(
1511
1307
  "a",
1512
1308
  {
1513
1309
  href: "https://www.mitrerealty.com.br/politica-de-privacidade",