mirror of
https://github.com/Unleash/unleash.git
synced 2025-08-04 13:48:56 +02:00
JSON.stringify some stuff and extract functions
This commit is contained in:
parent
872c582eb9
commit
6ee18b0fe0
@ -4,6 +4,7 @@ import handleErrorResponses from '../httpErrorResponseHandler.js';
|
|||||||
import type {
|
import type {
|
||||||
ChangeRequestType,
|
ChangeRequestType,
|
||||||
IChangeRequestAddStrategy,
|
IChangeRequestAddStrategy,
|
||||||
|
IChangeRequestFeature,
|
||||||
IChangeRequestUpdateStrategy,
|
IChangeRequestUpdateStrategy,
|
||||||
IFeatureChange,
|
IFeatureChange,
|
||||||
} from 'component/changeRequest/changeRequest.types';
|
} from 'component/changeRequest/changeRequest.types';
|
||||||
@ -18,24 +19,8 @@ const isUpdateStrategyChange = (
|
|||||||
change: IFeatureChange,
|
change: IFeatureChange,
|
||||||
): change is IChangeRequestUpdateStrategy => change.action === 'updateStrategy';
|
): change is IChangeRequestUpdateStrategy => change.action === 'updateStrategy';
|
||||||
|
|
||||||
export const useChangeRequest = (projectId: string, id: string) => {
|
const addConstraintIdsToFeatureChange = (change: IFeatureChange) => {
|
||||||
const { data, error, mutate } = useSWR<ChangeRequestType>(
|
if (isAddStrategyChange(change) || isUpdateStrategyChange(change)) {
|
||||||
formatApiPath(`api/admin/projects/${projectId}/change-requests/${id}`),
|
|
||||||
fetcher,
|
|
||||||
{ refreshInterval: 15000 },
|
|
||||||
);
|
|
||||||
|
|
||||||
const dataWithConstraintIds: ChangeRequestType | undefined = useMemo(() => {
|
|
||||||
if (!data) {
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
const features = data.features.map((feature) => {
|
|
||||||
const changes: IFeatureChange[] = feature.changes.map((change) => {
|
|
||||||
if (
|
|
||||||
isAddStrategyChange(change) ||
|
|
||||||
isUpdateStrategyChange(change)
|
|
||||||
) {
|
|
||||||
const { constraints, ...rest } = change.payload;
|
const { constraints, ...rest } = change.payload;
|
||||||
return {
|
return {
|
||||||
...change,
|
...change,
|
||||||
@ -49,23 +34,34 @@ export const useChangeRequest = (projectId: string, id: string) => {
|
|||||||
} as IFeatureChange;
|
} as IFeatureChange;
|
||||||
}
|
}
|
||||||
return change;
|
return change;
|
||||||
});
|
};
|
||||||
|
|
||||||
|
export const useChangeRequest = (projectId: string, id: string) => {
|
||||||
|
const { data, error, mutate } = useSWR<ChangeRequestType>(
|
||||||
|
formatApiPath(`api/admin/projects/${projectId}/change-requests/${id}`),
|
||||||
|
fetcher,
|
||||||
|
{ refreshInterval: 15000 },
|
||||||
|
);
|
||||||
|
|
||||||
|
const { features, ...dataProps } = data || {};
|
||||||
|
const featuresWithConstraintIds: IChangeRequestFeature[] | undefined =
|
||||||
|
useMemo(() => {
|
||||||
|
return (
|
||||||
|
features?.map((feature) => {
|
||||||
|
const changes: IFeatureChange[] = feature.changes.map(
|
||||||
|
addConstraintIdsToFeatureChange,
|
||||||
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...feature,
|
...feature,
|
||||||
changes,
|
changes,
|
||||||
};
|
};
|
||||||
});
|
}) ?? []
|
||||||
|
);
|
||||||
const value: ChangeRequestType = {
|
}, [JSON.stringify(features)]);
|
||||||
...data,
|
|
||||||
features,
|
|
||||||
};
|
|
||||||
return value;
|
|
||||||
}, [data]);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
data: dataWithConstraintIds,
|
data: { ...dataProps, features: featuresWithConstraintIds },
|
||||||
loading: !error && !data,
|
loading: !error && !data,
|
||||||
refetchChangeRequest: () => mutate(),
|
refetchChangeRequest: () => mutate(),
|
||||||
error,
|
error,
|
||||||
|
@ -38,7 +38,9 @@ export const useFeature = (
|
|||||||
mutate().catch(console.warn);
|
mutate().catch(console.warn);
|
||||||
}, [mutate]);
|
}, [mutate]);
|
||||||
|
|
||||||
const feature = useMemo(enrichConstraintsWithIds(data), [data?.body]);
|
const feature = useMemo(enrichConstraintsWithIds(data), [
|
||||||
|
JSON.stringify(data?.body),
|
||||||
|
]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
feature,
|
feature,
|
||||||
@ -68,7 +70,8 @@ export const featureFetcher = async (
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const enrichConstraintsWithIds = (data?: IFeatureResponse) => () => {
|
export const enrichConstraintsWithIds =
|
||||||
|
(data?: IFeatureResponse) => (): IFeatureToggle => {
|
||||||
if (!data?.body) {
|
if (!data?.body) {
|
||||||
return emptyFeature;
|
return emptyFeature;
|
||||||
}
|
}
|
||||||
@ -101,7 +104,7 @@ export const enrichConstraintsWithIds = (data?: IFeatureResponse) => () => {
|
|||||||
strategies: strategiesWithConstraintIds,
|
strategies: strategiesWithConstraintIds,
|
||||||
environments: environmentsWithStrategyIds,
|
environments: environmentsWithStrategyIds,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const formatFeatureApiPath = (
|
export const formatFeatureApiPath = (
|
||||||
projectId: string,
|
projectId: string,
|
||||||
|
@ -29,7 +29,9 @@ export const useFeatureImmutable = (
|
|||||||
await refetchFeature();
|
await refetchFeature();
|
||||||
}, [mutate, refetchFeature]);
|
}, [mutate, refetchFeature]);
|
||||||
|
|
||||||
const feature = useMemo(enrichConstraintsWithIds(data), [data?.body]);
|
const feature = useMemo(enrichConstraintsWithIds(data), [
|
||||||
|
JSON.stringify(data?.body),
|
||||||
|
]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
feature,
|
feature,
|
||||||
|
Loading…
Reference in New Issue
Block a user