mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	fix: equality check on feature strategy (#2145)
fix: add ability to format objects to perform equality checks on
This commit is contained in:
		
							parent
							
								
									3ec1241412
								
							
						
					
					
						commit
						3d90a3d070
					
				| @ -20,13 +20,13 @@ import { CREATE_FEATURE_STRATEGY } from 'component/providers/AccessProvider/perm | ||||
| import { ISegment } from 'interfaces/segment'; | ||||
| import { useSegmentsApi } from 'hooks/api/actions/useSegmentsApi/useSegmentsApi'; | ||||
| import { formatStrategyName } from 'utils/strategyNames'; | ||||
| import { useFeatureImmutable } from 'hooks/api/getters/useFeature/useFeatureImmutable'; | ||||
| import { useFormErrors } from 'hooks/useFormErrors'; | ||||
| import { createFeatureStrategy } from 'utils/createFeatureStrategy'; | ||||
| import { useStrategy } from 'hooks/api/getters/useStrategy/useStrategy'; | ||||
| import { useCollaborateData } from 'hooks/useCollaborateData'; | ||||
| import { useFeature } from 'hooks/api/getters/useFeature/useFeature'; | ||||
| import { IFeatureToggle } from 'interfaces/featureToggle'; | ||||
| import { comparisonModerator } from '../featureStrategy.utils'; | ||||
| 
 | ||||
| export const FeatureStrategyCreate = () => { | ||||
|     const projectId = useRequiredPathParam('projectId'); | ||||
| @ -60,7 +60,8 @@ export const FeatureStrategyCreate = () => { | ||||
|             feature, | ||||
|             { | ||||
|                 afterSubmitAction: refetchFeature, | ||||
|             } | ||||
|             }, | ||||
|             comparisonModerator | ||||
|         ); | ||||
| 
 | ||||
|     useEffect(() => { | ||||
|  | ||||
| @ -24,6 +24,7 @@ import { sortStrategyParameters } from 'utils/sortStrategyParameters'; | ||||
| import { useCollaborateData } from 'hooks/useCollaborateData'; | ||||
| import { useFeature } from 'hooks/api/getters/useFeature/useFeature'; | ||||
| import { IFeatureToggle } from 'interfaces/featureToggle'; | ||||
| import { comparisonModerator } from '../featureStrategy.utils'; | ||||
| 
 | ||||
| export const FeatureStrategyEdit = () => { | ||||
|     const projectId = useRequiredPathParam('projectId'); | ||||
| @ -58,7 +59,8 @@ export const FeatureStrategyEdit = () => { | ||||
|             feature, | ||||
|             { | ||||
|                 afterSubmitAction: refetchFeature, | ||||
|             } | ||||
|             }, | ||||
|             comparisonModerator | ||||
|         ); | ||||
| 
 | ||||
|     useEffect(() => { | ||||
|  | ||||
| @ -0,0 +1,8 @@ | ||||
| import { IFeatureToggle } from 'interfaces/featureToggle'; | ||||
| 
 | ||||
| export const comparisonModerator = (data: IFeatureToggle) => { | ||||
|     const tempData = { ...data }; | ||||
|     delete tempData.lastSeenAt; | ||||
| 
 | ||||
|     return tempData; | ||||
| }; | ||||
| @ -2,6 +2,7 @@ import { useState, useEffect } from 'react'; | ||||
| import { SWRConfiguration } from 'swr'; | ||||
| import { dequal } from 'dequal'; | ||||
| import { StaleDataNotification } from 'component/common/StaleDataNotification/StaleDataNotification'; | ||||
| import { IFeatureToggle } from 'interfaces/featureToggle'; | ||||
| 
 | ||||
| interface IFormatUnleashGetterOutput<Type> { | ||||
|     data: Type; | ||||
| @ -42,7 +43,8 @@ interface IStaleNotificationOptions { | ||||
| export const useCollaborateData = <Type,>( | ||||
|     getterOptions: IGetterOptions, | ||||
|     initialData: Type, | ||||
|     notificationOptions: IStaleNotificationOptions | ||||
|     notificationOptions: IStaleNotificationOptions, | ||||
|     comparisonModeratorFunc: (data: Type) => any | ||||
| ): ICollaborateDataOutput<Type> => { | ||||
|     const { data, refetch } = formatUnleashGetter<Type>(getterOptions); | ||||
|     const [cache, setCache] = useState<Type | null>(initialData || null); | ||||
| @ -53,6 +55,17 @@ export const useCollaborateData = <Type,>( | ||||
|         setCache(data); | ||||
|     }; | ||||
| 
 | ||||
|     const formatDequalData = (data: Type | null) => { | ||||
|         if (!data) return data; | ||||
|         if ( | ||||
|             comparisonModeratorFunc && | ||||
|             typeof comparisonModeratorFunc === 'function' | ||||
|         ) { | ||||
|             return comparisonModeratorFunc(data); | ||||
|         } | ||||
|         return data; | ||||
|     }; | ||||
| 
 | ||||
|     useEffect(() => { | ||||
|         if (cache === null) { | ||||
|             setCache(initialData); | ||||
| @ -60,7 +73,9 @@ export const useCollaborateData = <Type,>( | ||||
|     }, [initialData]); | ||||
| 
 | ||||
|     useEffect(() => { | ||||
|         const equal = dequal(data, cache); | ||||
|         if (!cache || !data) return; | ||||
| 
 | ||||
|         const equal = dequal(formatDequalData(cache), formatDequalData(data)); | ||||
| 
 | ||||
|         if (!equal) { | ||||
|             setDataModified(true); | ||||
| @ -72,8 +87,8 @@ export const useCollaborateData = <Type,>( | ||||
|         refetch, | ||||
|         staleDataNotification: ( | ||||
|             <StaleDataNotification | ||||
|                 cache={cache} | ||||
|                 data={data} | ||||
|                 cache={formatDequalData(cache)} | ||||
|                 data={formatDequalData(data)} | ||||
|                 refresh={() => forceRefreshCache(data)} | ||||
|                 show={dataModified} | ||||
|                 afterSubmitAction={notificationOptions.afterSubmitAction} | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user