Skip to content

Commit

Permalink
Merge pull request #183 from IQ-tech/feat/add-city-widget
Browse files Browse the repository at this point in the history
feat: add BRCityWidget
  • Loading branch information
Qnedy authored Apr 24, 2024
2 parents 95e6d4d + da68f68 commit 4bf3c0c
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
38 changes: 38 additions & 0 deletions packages/atlas-theme/Widgets/BRCityWidget/hook.js
Original file line number Diff line number Diff line change
@@ -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 }
}
49 changes: 49 additions & 0 deletions packages/atlas-theme/Widgets/BRCityWidget/index.jsx
Original file line number Diff line number Diff line change
@@ -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 (
<AutocompleteField
required={isRequired}
optional={isOptional}
invalid={hasError}
errorMessage={errorMessage}
label={label}
onBlur={onBlur}
onChange={onChange}
placeholder={placeholder}
name={`fbt-field-place`}
value={value}
isLoading={isLoading}
suggestionUse="mandatory"
tooltipConfig={getTooltipConfig(meta)}
options={options}
/>
)
}
16 changes: 16 additions & 0 deletions packages/atlas-theme/helpers/getTooltipConfig.js
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions packages/atlas-theme/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -14,6 +15,7 @@ const defaultTheme = {
"Radio-Unified": RadioButtonGroupWidget,
"Currency": CurrencyWidget,
"CEP": CEPWidget,
"BRCity": BRCityWidget,
}

export default defaultTheme
5 changes: 5 additions & 0 deletions packages/atlas-theme/services/getCities.js
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 4bf3c0c

Please sign in to comment.