From da68f68083794beb0df94db8444295ae83404147 Mon Sep 17 00:00:00 2001 From: Teresa Lazarini Date: Wed, 24 Apr 2024 10:49:05 -0300 Subject: [PATCH] feat: add BRCityWidget --- .../atlas-theme/Widgets/BRCityWidget/hook.js | 38 ++++++++++++++ .../Widgets/BRCityWidget/index.jsx | 49 +++++++++++++++++++ .../atlas-theme/helpers/getTooltipConfig.js | 16 ++++++ packages/atlas-theme/index.js | 2 + packages/atlas-theme/services/getCities.js | 5 ++ 5 files changed, 110 insertions(+) create mode 100644 packages/atlas-theme/Widgets/BRCityWidget/hook.js create mode 100644 packages/atlas-theme/Widgets/BRCityWidget/index.jsx create mode 100644 packages/atlas-theme/helpers/getTooltipConfig.js create mode 100644 packages/atlas-theme/services/getCities.js diff --git a/packages/atlas-theme/Widgets/BRCityWidget/hook.js b/packages/atlas-theme/Widgets/BRCityWidget/hook.js new file mode 100644 index 00000000..2322e94e --- /dev/null +++ b/packages/atlas-theme/Widgets/BRCityWidget/hook.js @@ -0,0 +1,38 @@ +import { useState, useEffect } from "react" +import stateFormats from "../../constants/state-formats" +import getCities from "../../services/getCities" + +export default function useBRCityWidget({ + payload, + modifyPayloadKeys, + stateFieldSlug, + stateFormat = "br", + slug, +}) { + const [options, setOptions] = useState([]) + const [isLoading, setIsloading] = useState(false) + const provinceValue = payload?.[stateFieldSlug] + + useEffect(_handleStateChange, [provinceValue]) + + function _handleStateChange() { + const correctStateKey = Object.keys(stateFormats).find( + (key) => stateFormats?.[key]?.[stateFormat] === provinceValue + ) + const correctStateNumber = stateFormats?.[correctStateKey]?.uf + + if (correctStateNumber) { + setIsloading(true) + getCities(correctStateNumber).then((res = []) => { + const formatted = res?.map((item) => ({ + label: item?.nome, + value: item?.nome, + })) + setIsloading(false) + setOptions(formatted) + }) + } + } + + return { options, isLoading } +} diff --git a/packages/atlas-theme/Widgets/BRCityWidget/index.jsx b/packages/atlas-theme/Widgets/BRCityWidget/index.jsx new file mode 100644 index 00000000..ab5655fa --- /dev/null +++ b/packages/atlas-theme/Widgets/BRCityWidget/index.jsx @@ -0,0 +1,49 @@ +import React from "react" +import { AutocompleteField } from "iq-blueberry" +import useBRCityWidget from "./hook" +import getTooltipConfig from "../../helpers/getTooltipConfig" + +export default function BRCityWidget({ + stateFieldSlug, + stateFormat, + modifyPayloadKeys, + payload, + hasError, + errorMessage, + label, + onBlur, + onChange, + placeholder, + slug, + value, + isOptional, + isRequired, + meta, +}) { + const { options, isLoading } = useBRCityWidget({ + stateFieldSlug, + stateFormat, + modifyPayloadKeys, + payload, + slug, + }) + + return ( + + ) +} diff --git a/packages/atlas-theme/helpers/getTooltipConfig.js b/packages/atlas-theme/helpers/getTooltipConfig.js new file mode 100644 index 00000000..15e51de4 --- /dev/null +++ b/packages/atlas-theme/helpers/getTooltipConfig.js @@ -0,0 +1,16 @@ +const getTooltipConfig = (fbFieldMetadata) => { + const safeFieldMetadata = fbFieldMetadata || {} + const tooltipText = safeFieldMetadata["tooltip_text"] + const tooltipTitle = safeFieldMetadata["tooltip_title"] + const tooltipContainer = safeFieldMetadata["tooltip_container"] + return !!tooltipText + ? { + placement: "top", + desc: tooltipText, + title: tooltipTitle, + tooltipContainer: tooltipContainer, + } + : false +} + +export default getTooltipConfig diff --git a/packages/atlas-theme/index.js b/packages/atlas-theme/index.js index bf4312dd..939b4f70 100644 --- a/packages/atlas-theme/index.js +++ b/packages/atlas-theme/index.js @@ -5,6 +5,7 @@ import RadioGroupWidget from "./Widgets/RadioGroupWidget" import RadioButtonGroupWidget from "./Widgets/RadioButtonGroupWidget" import CurrencyWidget from "./Widgets/CurrencyWidget" import CEPWidget from "./Widgets/CEPWidget" +import BRCityWidget from "./Widgets/BRCityWidget" const defaultTheme = { "Text": TextWidget, @@ -14,6 +15,7 @@ const defaultTheme = { "Radio-Unified": RadioButtonGroupWidget, "Currency": CurrencyWidget, "CEP": CEPWidget, + "BRCity": BRCityWidget, } export default defaultTheme diff --git a/packages/atlas-theme/services/getCities.js b/packages/atlas-theme/services/getCities.js new file mode 100644 index 00000000..4ec98a2a --- /dev/null +++ b/packages/atlas-theme/services/getCities.js @@ -0,0 +1,5 @@ +export default async function getCities(stateUfNumber) { + const route = `https://servicodados.ibge.gov.br/api/v1/localidades/estados/${stateUfNumber}/municipios` + const res = (await fetch(route).then((data) => data.json())) || [] + return res +}