Fix worker

pull/2116/head
Jérémy Rialland 2022-04-26 15:15:29 +02:00 committed by Jérémy Rialland
parent f96f83abe8
commit cdefb0e1e2
4 changed files with 27 additions and 20 deletions

View File

@ -1,6 +1,5 @@
import NumberInput from '@/components/conversation/NumberInput'
import SelectCommune from '@/components/conversation/select/SelectCommune'
import SelectAtmp from '@/components/conversation/select/SelectTauxRisque'
import { EngineContext } from '@/components/utils/EngineContext'
import { getMeta } from '@/utils'
import { DottedName } from 'modele-social'
@ -17,6 +16,7 @@ import { Choice, MultipleAnswerInput, OuiNonInput } from './ChoicesInput'
import DateInput from './DateInput'
import ParagrapheInput from './ParagrapheInput'
import SelectPaysDétachement from './select/SelectPaysDétachement'
import SelectAtmp from './select/SelectTauxRisque'
import TextInput from './TextInput'
type Props<Names extends string = DottedName> = Omit<

View File

@ -3,7 +3,7 @@ import TextField from '@/design-system/field/TextField'
import { useEffect, useState } from 'react'
import { Trans, useTranslation } from 'react-i18next'
import styled from 'styled-components'
import Worker from './SelectTauxRisque.worker.js?worker'
import Worker from './SelectTauxRisque.worker?worker'
const worker = !import.meta.env.SSR ? new Worker() : null
@ -16,7 +16,7 @@ const formatTauxNet = (taux: string) => {
return `${tauxNet} %`
}
interface Result {
export interface Result {
'Nature du risque': string
'Code risque': string
'Taux net': string
@ -34,7 +34,9 @@ function SelectComponent({
onChange?: (value: string | undefined) => void
onSubmit?: () => void
}) {
const { t } = useTranslation()
const [searchResults, setSearchResults] = useState<Result[]>()
const submitOnChange = (option: Result) => {
const tauxNet = parseFloat(option['Taux net'].replace(',', '.'))
if (isNaN(tauxNet)) {
@ -44,13 +46,13 @@ function SelectComponent({
onChange?.(isNaN(tauxNet) ? undefined : `${tauxNet}%`)
onSubmit?.()
}
const { t } = useTranslation()
useEffect(() => {
worker?.postMessage({
options,
})
if (worker?.onmessage) {
if (worker) {
worker.onmessage = ({ data: results }: { data: Result[] }) =>
setSearchResults(results)
}
@ -138,6 +140,7 @@ export default function SelectAtmp(
props: Parameters<typeof SelectComponent>[0]
) {
const [options, setOptions] = useState<Result[] | null>(null)
useEffect(() => {
fetch(
'https://raw.githubusercontent.com/betagouv/taux-collectifs-cotisation-atmp/master/taux-2021.json'

View File

@ -1,15 +0,0 @@
import Fuse from 'fuse.js'
let fuse = null
onmessage = function (event) {
if (event.data.options)
fuse = new Fuse(event.data.options, {
keys: ['Nature du risque', 'Catégorie'],
shouldSort: true,
})
if (event.data.input) {
let results = fuse.search(event.data.input).map(({ item }) => item)
postMessage(results)
}
}

View File

@ -0,0 +1,19 @@
import Fuse from 'fuse.js'
import { Result } from './SelectTauxRisque'
let fuse: Fuse<Result> | null = null
onmessage = function (
event: MessageEvent<{ options: Result[]; input: string }>
) {
if (event.data.options)
fuse = new Fuse(event.data.options, {
keys: ['Nature du risque', 'Catégorie'],
shouldSort: true,
})
if (event.data.input && fuse) {
const results = fuse.search(event.data.input).map(({ item }) => item)
postMessage(results)
}
}