@@ -19,7 +19,7 @@ import { useHotkeys } from "react-hotkeys-hook";
1919import { TestRun } from "../../types" ;
2020import { testRunService , staticService } from "../../services" ;
2121import { TestStatus } from "../../types/testStatus" ;
22- import { useHistory , Prompt } from "react-router-dom" ;
22+ import { useHistory } from "react-router-dom" ;
2323import { IgnoreArea , UpdateIgnoreAreaDto } from "../../types/ignoreArea" ;
2424import { KonvaEventObject } from "konva/types/Node" ;
2525import {
@@ -34,7 +34,7 @@ import {
3434import { TestRunDetails } from "../TestRunDetails" ;
3535import useImage from "use-image" ;
3636import { routes } from "../../constants" ;
37- import { useTestRunDispatch , selectTestRun } from "../../contexts" ;
37+ import { useTestRunDispatch } from "../../contexts" ;
3838import { DrawArea } from "../DrawArea" ;
3939import { CommentsPopper } from "../CommentsPopper" ;
4040import { useSnackbar } from "notistack" ;
@@ -61,7 +61,9 @@ const useStyles = makeStyles((theme) => ({
6161
6262const TestDetailsModal : React . FunctionComponent < {
6363 testRun : TestRun ;
64- } > = ( { testRun } ) => {
64+ touched : boolean ;
65+ handleClose : ( ) => void ;
66+ } > = ( { testRun, touched, handleClose } ) => {
6567 const classes = useStyles ( ) ;
6668 const history = useHistory ( ) ;
6769 const { enqueueSnackbar } = useSnackbar ( ) ;
@@ -75,6 +77,13 @@ const TestDetailsModal: React.FunctionComponent<{
7577 const [ stageInitPos , setStageInitPos ] = React . useState ( defaultStagePos ) ;
7678 const [ stageOffset , setStageOffset ] = React . useState ( defaultStagePos ) ;
7779 const [ processing , setProcessing ] = React . useState ( false ) ;
80+ const [ isDrawMode , setIsDrawMode ] = useState ( false ) ;
81+ const [ valueOfIgnoreOrCompare , setValueOfIgnoreOrCompare ] = useState (
82+ "Ignore Areas"
83+ ) ;
84+ const [ isDiffShown , setIsDiffShown ] = useState ( ! ! testRun . diffName ) ;
85+ const [ selectedRectId , setSelectedRectId ] = React . useState < string > ( ) ;
86+ const [ ignoreAreas , setIgnoreAreas ] = React . useState < IgnoreArea [ ] > ( [ ] ) ;
7887
7988 const [ image , imageStatus ] = useImage (
8089 staticService . getImage ( testRun . imageName )
@@ -86,17 +95,32 @@ const TestDetailsModal: React.FunctionComponent<{
8695 staticService . getImage ( testRun . diffName )
8796 ) ;
8897
89- const [ isDrawMode , setIsDrawMode ] = useState ( false ) ;
90- const [ valueOfIgnoreOrCompare , setValueOfIgnoreOrCompare ] = useState (
91- "Ignore Areas"
92- ) ;
93- const [ isDiffShown , setIsDiffShown ] = useState ( false ) ;
94- const [ selectedRectId , setSelectedRectId ] = React . useState < string > ( ) ;
98+ React . useEffect ( ( ) => {
99+ fitStageToScreen ( ) ;
100+ // eslint-disable-next-line
101+ } , [ image ] ) ;
95102
96- const [ ignoreAreas , setIgnoreAreas ] = React . useState < IgnoreArea [ ] > (
97- JSON . parse ( testRun . ignoreAreas )
103+ React . useEffect ( ( ) => {
104+ setIgnoreAreas ( JSON . parse ( testRun . ignoreAreas ) ) ;
105+ } , [ testRun ] ) ;
106+
107+ const isImageSizeDiffer = React . useMemo (
108+ ( ) =>
109+ testRun . baselineName &&
110+ testRun . imageName &&
111+ ( image ?. height !== baselineImage ?. height ||
112+ image ?. width !== baselineImage ?. width ) ,
113+ [ image , baselineImage , testRun . baselineName , testRun . imageName ]
98114 ) ;
99115
116+ const handleIgnoreAreaChange = ( ignoreAreas : IgnoreArea [ ] ) => {
117+ setIgnoreAreas ( ignoreAreas ) ;
118+ testRunDispatch ( {
119+ type : "touched" ,
120+ payload : testRun . ignoreAreas !== JSON . stringify ( ignoreAreas ) ,
121+ } ) ;
122+ } ;
123+
100124 const removeSelection = ( event : KonvaEventObject < MouseEvent > ) => {
101125 // deselect when clicked not on Rect
102126 const isRectClicked = event . target . className === "Rect" ;
@@ -106,7 +130,7 @@ const TestDetailsModal: React.FunctionComponent<{
106130 } ;
107131
108132 const deleteIgnoreArea = ( id : string ) => {
109- setIgnoreAreas ( ignoreAreas . filter ( ( area ) => area . id !== id ) ) ;
133+ handleIgnoreAreaChange ( ignoreAreas . filter ( ( area ) => area . id !== id ) ) ;
110134 setSelectedRectId ( undefined ) ;
111135 } ;
112136
@@ -138,12 +162,13 @@ const TestDetailsModal: React.FunctionComponent<{
138162 head ( ignoreAreas )
139163 ) ;
140164
141- setIgnoreAreas ( invertedIgnoreAreas ) ;
165+ handleIgnoreAreaChange ( invertedIgnoreAreas ) ;
142166 saveTestRun (
143167 invertedIgnoreAreas ,
144168 "Selected area has been inverted to ignore areas and saved."
145169 ) ;
146170 }
171+ testRunDispatch ( { type : "touched" , payload : false } ) ;
147172 } ;
148173
149174 const onIgnoreOrCompareSelectChange = ( value : string ) => {
@@ -154,14 +179,6 @@ const TestDetailsModal: React.FunctionComponent<{
154179 }
155180 } ;
156181
157- const handleClose = ( ) => {
158- selectTestRun ( testRunDispatch , undefined ) ;
159- } ;
160-
161- const isIgnoreAreasSaved = ( ) => {
162- return testRun . ignoreAreas === JSON . stringify ( ignoreAreas ) ;
163- } ;
164-
165182 const setOriginalSize = ( ) => {
166183 setStageScale ( 1 ) ;
167184 resetPositioin ( ) ;
@@ -220,42 +237,16 @@ const TestDetailsModal: React.FunctionComponent<{
220237 }
221238 } ;
222239
223- React . useEffect ( ( ) => {
224- setIgnoreAreas ( JSON . parse ( testRun . ignoreAreas ) ) ;
225- } , [ testRun ] ) ;
226-
227- React . useEffect ( ( ) => {
228- fitStageToScreen ( ) ;
229- // eslint-disable-next-line
230- } , [ image ] ) ;
231-
232- React . useEffect ( ( ) => {
233- setIsDiffShown ( ! ! testRun . diffName ) ;
234- } , [ testRun . diffName ] ) ;
235-
236- const isImageSizeDiffer = React . useMemo (
237- ( ) =>
238- testRun . baselineName &&
239- testRun . imageName &&
240- ( image ?. height !== baselineImage ?. height ||
241- image ?. width !== baselineImage ?. width ) ,
242- [ image , baselineImage , testRun . baselineName , testRun . imageName ]
243- ) ;
244-
245240 useHotkeys (
246241 "d" ,
247242 ( ) =>
248243 shouldDiffHotKeyBeActive && setIsDiffShown ( ( isDiffShown ) => ! isDiffShown )
249244 ) ;
250- useHotkeys ( "ESC" , ( ) => handleClose ( ) ) ;
245+ useHotkeys ( "ESC" , handleClose , [ handleClose ] ) ;
251246 const shouldDiffHotKeyBeActive = ! ! testRun . diffName ;
252247
253248 return (
254249 < React . Fragment >
255- < Prompt
256- when = { ! isIgnoreAreasSaved ( ) }
257- message = { `You have unsaved changes that will be lost` }
258- />
259250 < AppBar position = "sticky" >
260251 < Toolbar >
261252 < Grid container justify = "space-between" >
@@ -353,7 +344,9 @@ const TestDetailsModal: React.FunctionComponent<{
353344 < Grid item >
354345 < IconButton
355346 disabled = { ignoreAreas . length === 0 }
356- onClick = { ( ) => setIgnoreAreas ( [ ] ) }
347+ onClick = { ( ) => {
348+ handleIgnoreAreaChange ( [ ] ) ;
349+ } }
357350 >
358351 < LayersClear />
359352 </ IconButton >
@@ -374,7 +367,7 @@ const TestDetailsModal: React.FunctionComponent<{
374367 </ Tooltip >
375368 < Grid item >
376369 < IconButton
377- disabled = { isIgnoreAreasSaved ( ) }
370+ disabled = { ! touched }
378371 onClick = { ( ) => saveIgnoreAreasOrCompareArea ( ) }
379372 >
380373 < Save />
@@ -430,7 +423,7 @@ const TestDetailsModal: React.FunctionComponent<{
430423 imageState = { [ baselineImage , baselineImageStatus ] }
431424 ignoreAreas = { [ ] }
432425 tempIgnoreAreas = { [ ] }
433- setIgnoreAreas = { setIgnoreAreas }
426+ setIgnoreAreas = { handleIgnoreAreaChange }
434427 selectedRectId = { selectedRectId }
435428 setSelectedRectId = { setSelectedRectId }
436429 onStageClick = { removeSelection }
@@ -450,7 +443,7 @@ const TestDetailsModal: React.FunctionComponent<{
450443 imageState = { [ diffImage , diffImageStatus ] }
451444 ignoreAreas = { ignoreAreas }
452445 tempIgnoreAreas = { JSON . parse ( testRun . tempIgnoreAreas ) }
453- setIgnoreAreas = { setIgnoreAreas }
446+ setIgnoreAreas = { handleIgnoreAreaChange }
454447 selectedRectId = { selectedRectId }
455448 setSelectedRectId = { setSelectedRectId }
456449 onStageClick = { removeSelection }
@@ -468,7 +461,7 @@ const TestDetailsModal: React.FunctionComponent<{
468461 imageState = { [ image , imageStatus ] }
469462 ignoreAreas = { ignoreAreas }
470463 tempIgnoreAreas = { JSON . parse ( testRun . tempIgnoreAreas ) }
471- setIgnoreAreas = { setIgnoreAreas }
464+ setIgnoreAreas = { handleIgnoreAreaChange }
472465 selectedRectId = { selectedRectId }
473466 setSelectedRectId = { setSelectedRectId }
474467 onStageClick = { removeSelection }
0 commit comments