mirror of
https://github.com/Unleash/unleash.git
synced 2025-04-06 01:15:28 +02: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 { ISegment } from 'interfaces/segment';
|
||||||
import { useSegmentsApi } from 'hooks/api/actions/useSegmentsApi/useSegmentsApi';
|
import { useSegmentsApi } from 'hooks/api/actions/useSegmentsApi/useSegmentsApi';
|
||||||
import { formatStrategyName } from 'utils/strategyNames';
|
import { formatStrategyName } from 'utils/strategyNames';
|
||||||
import { useFeatureImmutable } from 'hooks/api/getters/useFeature/useFeatureImmutable';
|
|
||||||
import { useFormErrors } from 'hooks/useFormErrors';
|
import { useFormErrors } from 'hooks/useFormErrors';
|
||||||
import { createFeatureStrategy } from 'utils/createFeatureStrategy';
|
import { createFeatureStrategy } from 'utils/createFeatureStrategy';
|
||||||
import { useStrategy } from 'hooks/api/getters/useStrategy/useStrategy';
|
import { useStrategy } from 'hooks/api/getters/useStrategy/useStrategy';
|
||||||
import { useCollaborateData } from 'hooks/useCollaborateData';
|
import { useCollaborateData } from 'hooks/useCollaborateData';
|
||||||
import { useFeature } from 'hooks/api/getters/useFeature/useFeature';
|
import { useFeature } from 'hooks/api/getters/useFeature/useFeature';
|
||||||
import { IFeatureToggle } from 'interfaces/featureToggle';
|
import { IFeatureToggle } from 'interfaces/featureToggle';
|
||||||
|
import { comparisonModerator } from '../featureStrategy.utils';
|
||||||
|
|
||||||
export const FeatureStrategyCreate = () => {
|
export const FeatureStrategyCreate = () => {
|
||||||
const projectId = useRequiredPathParam('projectId');
|
const projectId = useRequiredPathParam('projectId');
|
||||||
@ -60,7 +60,8 @@ export const FeatureStrategyCreate = () => {
|
|||||||
feature,
|
feature,
|
||||||
{
|
{
|
||||||
afterSubmitAction: refetchFeature,
|
afterSubmitAction: refetchFeature,
|
||||||
}
|
},
|
||||||
|
comparisonModerator
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
@ -24,6 +24,7 @@ import { sortStrategyParameters } from 'utils/sortStrategyParameters';
|
|||||||
import { useCollaborateData } from 'hooks/useCollaborateData';
|
import { useCollaborateData } from 'hooks/useCollaborateData';
|
||||||
import { useFeature } from 'hooks/api/getters/useFeature/useFeature';
|
import { useFeature } from 'hooks/api/getters/useFeature/useFeature';
|
||||||
import { IFeatureToggle } from 'interfaces/featureToggle';
|
import { IFeatureToggle } from 'interfaces/featureToggle';
|
||||||
|
import { comparisonModerator } from '../featureStrategy.utils';
|
||||||
|
|
||||||
export const FeatureStrategyEdit = () => {
|
export const FeatureStrategyEdit = () => {
|
||||||
const projectId = useRequiredPathParam('projectId');
|
const projectId = useRequiredPathParam('projectId');
|
||||||
@ -58,7 +59,8 @@ export const FeatureStrategyEdit = () => {
|
|||||||
feature,
|
feature,
|
||||||
{
|
{
|
||||||
afterSubmitAction: refetchFeature,
|
afterSubmitAction: refetchFeature,
|
||||||
}
|
},
|
||||||
|
comparisonModerator
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
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 { SWRConfiguration } from 'swr';
|
||||||
import { dequal } from 'dequal';
|
import { dequal } from 'dequal';
|
||||||
import { StaleDataNotification } from 'component/common/StaleDataNotification/StaleDataNotification';
|
import { StaleDataNotification } from 'component/common/StaleDataNotification/StaleDataNotification';
|
||||||
|
import { IFeatureToggle } from 'interfaces/featureToggle';
|
||||||
|
|
||||||
interface IFormatUnleashGetterOutput<Type> {
|
interface IFormatUnleashGetterOutput<Type> {
|
||||||
data: Type;
|
data: Type;
|
||||||
@ -42,7 +43,8 @@ interface IStaleNotificationOptions {
|
|||||||
export const useCollaborateData = <Type,>(
|
export const useCollaborateData = <Type,>(
|
||||||
getterOptions: IGetterOptions,
|
getterOptions: IGetterOptions,
|
||||||
initialData: Type,
|
initialData: Type,
|
||||||
notificationOptions: IStaleNotificationOptions
|
notificationOptions: IStaleNotificationOptions,
|
||||||
|
comparisonModeratorFunc: (data: Type) => any
|
||||||
): ICollaborateDataOutput<Type> => {
|
): ICollaborateDataOutput<Type> => {
|
||||||
const { data, refetch } = formatUnleashGetter<Type>(getterOptions);
|
const { data, refetch } = formatUnleashGetter<Type>(getterOptions);
|
||||||
const [cache, setCache] = useState<Type | null>(initialData || null);
|
const [cache, setCache] = useState<Type | null>(initialData || null);
|
||||||
@ -53,6 +55,17 @@ export const useCollaborateData = <Type,>(
|
|||||||
setCache(data);
|
setCache(data);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const formatDequalData = (data: Type | null) => {
|
||||||
|
if (!data) return data;
|
||||||
|
if (
|
||||||
|
comparisonModeratorFunc &&
|
||||||
|
typeof comparisonModeratorFunc === 'function'
|
||||||
|
) {
|
||||||
|
return comparisonModeratorFunc(data);
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (cache === null) {
|
if (cache === null) {
|
||||||
setCache(initialData);
|
setCache(initialData);
|
||||||
@ -60,7 +73,9 @@ export const useCollaborateData = <Type,>(
|
|||||||
}, [initialData]);
|
}, [initialData]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const equal = dequal(data, cache);
|
if (!cache || !data) return;
|
||||||
|
|
||||||
|
const equal = dequal(formatDequalData(cache), formatDequalData(data));
|
||||||
|
|
||||||
if (!equal) {
|
if (!equal) {
|
||||||
setDataModified(true);
|
setDataModified(true);
|
||||||
@ -72,8 +87,8 @@ export const useCollaborateData = <Type,>(
|
|||||||
refetch,
|
refetch,
|
||||||
staleDataNotification: (
|
staleDataNotification: (
|
||||||
<StaleDataNotification
|
<StaleDataNotification
|
||||||
cache={cache}
|
cache={formatDequalData(cache)}
|
||||||
data={data}
|
data={formatDequalData(data)}
|
||||||
refresh={() => forceRefreshCache(data)}
|
refresh={() => forceRefreshCache(data)}
|
||||||
show={dataModified}
|
show={dataModified}
|
||||||
afterSubmitAction={notificationOptions.afterSubmitAction}
|
afterSubmitAction={notificationOptions.afterSubmitAction}
|
||||||
|
Loading…
Reference in New Issue
Block a user