Skip to content

Commit

Permalink
Merge pull request #269 from Sterbenfr/fix/app
Browse files Browse the repository at this point in the history
fix(app): Fixed a bug with select
  • Loading branch information
Aurelienschmi authored Aug 1, 2024
2 parents 18bac06 + d975a2f commit 1f9565b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 33 deletions.
4 changes: 3 additions & 1 deletion app/dons/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,9 @@ function DonsPage() {
id: 'code_contact_Entite_donatrice',
type: 'select',
value: codeContactEntiteDonatrice,
url: `../api/select/societe/entite/${EntiteDonatrice}/contact`,
url: EntiteDonatrice
? `../api/select/societe/entite/${EntiteDonatrice}/contact`
: '',
onChange: handleCodeContactEntiteDonatriceChange,
},
{
Expand Down
66 changes: 34 additions & 32 deletions components/select-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,39 +35,41 @@ export default function SelectComponent({

useEffect(() => {
const fetchOptions = async () => {
const response = await fetch(url)
if (!response.ok) {
console.error(
'Erreur lors de la récupération des données',
response,
)
}
const data = await response.json()
const formattedOptions = data.map(
(item: {
id: string
label: string
params1: string
params2: string
params3: string
}) => ({
value: item.id,
label: item.label,
params1: item.params1,
params2: item.params2,
params3: item.params3,
}),
)
setOptions(formattedOptions)
if (formattedOptions.length > 0 && onChange) {
const event = new Event('change', { bubbles: true })
Object.defineProperty(event, 'target', {
writable: false,
value: '',
})
onChange(
event as unknown as React.ChangeEvent<HTMLSelectElement>,
if (url !== '') {
const response = await fetch(url)
if (!response.ok) {
console.error(
'Erreur lors de la récupération des données',
response,
)
}
const data = await response.json()
const formattedOptions = data.map(
(item: {
id: string
label: string
params1: string
params2: string
params3: string
}) => ({
value: item.id,
label: item.label,
params1: item.params1,
params2: item.params2,
params3: item.params3,
}),
)
setOptions(formattedOptions)
if (formattedOptions.length > 0 && onChange) {
const event = new Event('change', { bubbles: true })
Object.defineProperty(event, 'target', {
writable: false,
value: '',
})
onChange(
event as unknown as React.ChangeEvent<HTMLSelectElement>,
)
}
}
}
fetchOptions()
Expand Down

0 comments on commit 1f9565b

Please sign in to comment.