Skip to content

Commit

Permalink
hotfix: re-trigger controls when an input has changed
Browse files Browse the repository at this point in the history
  • Loading branch information
chloe-renaud committed Nov 20, 2024
1 parent ef6d3f8 commit 42ddce6
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 41 deletions.
3 changes: 1 addition & 2 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ about: Create a report to help us improve
title: ''
labels: 'Type: Bug'
assignees: ''

---

## Describe the bug
Expand Down Expand Up @@ -40,7 +39,7 @@ Specify the version number where the bug first appeared.
- [ ] Firefox
- [ ] Safari
- [ ] Edge
- [ ] Other (please specify): ******\_\_\_******
- [ ] Other (please specify): **\*\***\_\_\_**\*\***

## Additional context

Expand Down
17 changes: 14 additions & 3 deletions src/shared/components/Orchestrator/Orchestrator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { VTLDevTools } from './VTLDevTools/VTLDevtools'
import { createLunaticLogger } from './VTLDevTools/VTLErrorStore'
import { slotComponents } from './slotComponents'
import { useStromaeNavigation } from './useStromaeNavigation'
import { isBlockingError, isSameErrors } from './utils/controls'
import { isBlockingError } from './utils/controls'
import { trimCollectedData } from './utils/data'
import type {
LunaticComponentsProps,
Expand Down Expand Up @@ -119,6 +119,8 @@ export function Orchestrator(props: OrchestratorProps) {
activeControls: true,
getReferentiel,
autoSuggesterLoading: true,
// once the user has changed its input, we need to retrigger the controls
onChange: () => setIsControlsAcknowledged(false),
trackChanges: mode === 'collect',
withOverview: true,
})
Expand All @@ -129,6 +131,9 @@ export function Orchestrator(props: OrchestratorProps) {
const [lastUpdateDate, setLastUpdateDate] = useState<number | undefined>(
surveyUnitData?.stateData?.date
)
// whether or not the user has seen the errors and did not change anything
const [isControlsAcknowledged, setIsControlsAcknowledged] =
useState<boolean>(false)
const [activeErrors, setActiveErrors] = useState<
Record<string, LunaticError[]> | undefined
>(undefined)
Expand Down Expand Up @@ -158,15 +163,20 @@ export function Orchestrator(props: OrchestratorProps) {
if (isBlockingError(currentErrors)) {
//compileControls returns isCritical but I prefer define my own rules of blocking error in the orchestrator
setActiveErrors(currentErrors)
setIsControlsAcknowledged(true)
return
}

// activeErrors and currentErrors are the same and no blocking error, we go next
if (isSameErrors(currentErrors, activeErrors)) {
// we've already seen the errors and changed nothing since, we go next
if (isControlsAcknowledged) {
setActiveErrors(undefined)
goNext()
return
}

// we've never seen the errors or we've seen them but changed something,
// we display the errors and set them as acknowledged
setIsControlsAcknowledged(true)
setActiveErrors(currentErrors)
}

Expand Down Expand Up @@ -260,6 +270,7 @@ export function Orchestrator(props: OrchestratorProps) {
})
contentRef.current.removeAttribute('tabindex')
}
setIsControlsAcknowledged(false)
// Persist data and stateData when page change in "collect" mode
triggerDataAndStateUpdate()
}, [currentPage, pageTag])
Expand Down
24 changes: 0 additions & 24 deletions src/shared/components/Orchestrator/utils/controls.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { areArraysEqual } from '@/utils/compareArray'
import type { LunaticError } from '@inseefr/lunatic'

export function isBlockingError(errors: Record<string, LunaticError[]>) {
Expand All @@ -9,26 +8,3 @@ export function isBlockingError(errors: Record<string, LunaticError[]>) {
)
)
}

export function isSameErrors(
currentErrors: Record<string, LunaticError[]>,
errors: Record<string, LunaticError[]> | undefined
) {
if (!errors) {
//currentErrors can not be undefined (currentErrors is undefined when no error)
return false
}

// extract errors Id in an arrays to compare them easily
const extractErrorsIds = (rawErrors: Record<string, LunaticError[]>) => {
return Object.values(rawErrors).reduce(
(acc, val) => acc.concat(val.map((error) => error.id)),
[] as string[]
)
}

const currentErrorsIds = extractErrorsIds(currentErrors)
const errorsIds = extractErrorsIds(errors)

return areArraysEqual(currentErrorsIds, errorsIds)
}
12 changes: 0 additions & 12 deletions src/utils/compareArray.ts

This file was deleted.

1 change: 1 addition & 0 deletions website/src/pages/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#### Correction de bugs

- réduction de la taille du payload envoyé à l'API en enlevant des variables inutilisées
- on déclenche à nouveau les contrôles si une valeur a changé

### 1.2.3

Expand Down

0 comments on commit 42ddce6

Please sign in to comment.