Fix react strict mode issue#1435
Conversation
Dependency Review✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.Scanned FilesNone |
9454f60 to
4a202c8
Compare
Huulivoide
left a comment
There was a problem hiding this comment.
@Huulivoide reviewed 6 files and all commit messages, and made 3 comments.
Reviewable status: 6 of 7 files reviewed, 3 unresolved discussions (waiting on patricvincit).
ui/src/components/map/routes/DrawRouteLayer.tsx line 169 at r1 (raw file):
}, [keyDown]); const isDrawingReady = useCallback(
Funktion nimi pikasesti implikois että tää on funktio joka tekee tarkastuksia ja sylkee ulos jonkin booleanin, vaikka oikeasti tää tekee "Set loading state based on given args". Tätä kutsutaan myös vaan yhdessä paikassa, niin sanosin että tästä voi ottaa ton "isReady" osuuden ulos ja siirtä tiedostoon ylätasolle function isDrawingReady(...): boolean.
Ja sit tuolla useFffectissä:
setLoadingState(isDrawingRead() ? a : b)
ui/src/components/map/routes/DrawRouteLayer.tsx line 171 at r1 (raw file):
const isDrawingReady = useCallback( (mapInstance: MapInstance, drawReference: MapboxDraw | null) => { const hasDrawRef = Boolean(drawReference);
Käsin koodatussa koodissa on käytetty !!-operaattoria boolean kästinä
ui/src/components/stop-registry/stops/stop-details/shelters/useSheltersForm.tsx line 18 at r1 (raw file):
.filter((num): num is number => num !== null && !Number.isNaN(num)); return (usedNumbers.length ? Math.max(...usedNumbers) : 0) + 1;
Math.max(0, ...usedNumbers) + 1
The basic details form became dirty right after opening edit mode in react strict mode. This happened because reasonForChange started as undefined, but the textarea used an empty string. React Hook Form saw that as a change and marked the form dirty. Set reasonForChange default to empty string so the form starts clean and no false unsaved-changes warning appears.
Shelter numbers could jump in Strict Mode because numbering was done in a component effect. Move number assignment to add/copy actions in the shelters form utilities. Now each new shelter gets its number only when the user adds or copies, so numbering stays predictable.
Prevent route creation clicks before draw mode is fully initialised.
4a202c8 to
ee5ce43
Compare
patricvincit
left a comment
There was a problem hiding this comment.
@patricvincit made 3 comments.
Reviewable status: 3 of 7 files reviewed, 3 unresolved discussions (waiting on Huulivoide).
ui/src/components/map/routes/DrawRouteLayer.tsx line 169 at r1 (raw file):
Previously, Huulivoide (Jesse Jaara) wrote…
Funktion nimi pikasesti implikois että tää on funktio joka tekee tarkastuksia ja sylkee ulos jonkin booleanin, vaikka oikeasti tää tekee "Set loading state based on given args". Tätä kutsutaan myös vaan yhdessä paikassa, niin sanosin että tästä voi ottaa ton "isReady" osuuden ulos ja siirtä tiedostoon ylätasolle
function isDrawingReady(...): boolean.Ja sit tuolla useFffectissä:
setLoadingState(isDrawingRead() ? a : b)
Tässä tää callback funktio aiheuttikin ongelmia routen editoinnissa. Antoi väärää tietoa valmiudesta ja testit kaatui kun klikkailtiin taas liian aikaisin (oli eri testit kuin alkuperäiset mitä korjailin niin jäi huomaamatta). Kevensin nyt tota rakennetta vähäsen samassa, enää ei ole erikseen tota aiempaa handleDrawReadyä, joka kutsuu sitten tota toista funktiota, vaan siirsin suoraan handleDrawReadyyn sen valmiuden käsittelyn (ja koitin nimetä selkeemmäksi).
Ja tiputin const { current: mapboxDraw } = drawRef; alustamisen pois, kun 95% komponentista käytti suoraan drawRef.current, niin pysyy sitten yhtenäisenä
ui/src/components/map/routes/DrawRouteLayer.tsx line 171 at r1 (raw file):
Previously, Huulivoide (Jesse Jaara) wrote…
Käsin koodatussa koodissa on käytetty !!-operaattoria boolean kästinä
Done.
ui/src/components/stop-registry/stops/stop-details/shelters/useSheltersForm.tsx line 18 at r1 (raw file):
Previously, Huulivoide (Jesse Jaara) wrote…
Math.max(0, ...usedNumbers) + 1
Done.
Huulivoide
left a comment
There was a problem hiding this comment.
@Huulivoide reviewed 4 files and all commit messages, and resolved 3 discussions.
Reviewable status:complete! all files reviewed, all discussions resolved (waiting on patricvincit).
Enabling strict mode caused 6 tests to fail.
stopDetails.cy.ts
Failed because the form was marked as dirty on strict mode which caused a confirmation modal to open. This happened because reason for change was not initialised on the form.
This failed on strict mode because shelter numbering was done in useEffect so strict mode caused it to give wrong shelter numbers as a side effect.
createRoute.cy.ts
All failed because cypress started clicking on the map before draw layer was actually ready. Fixed by adding a loader on the route creation while the drawing is not ready.
This change is