mirror of
https://github.com/Unleash/unleash.git
synced 2025-07-31 13:47:02 +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 {
|
||||
ChangeRequestType,
|
||||
IChangeRequestAddStrategy,
|
||||
IChangeRequestFeature,
|
||||
IChangeRequestUpdateStrategy,
|
||||
IFeatureChange,
|
||||
} from 'component/changeRequest/changeRequest.types';
|
||||
@ -18,6 +19,23 @@ const isUpdateStrategyChange = (
|
||||
change: IFeatureChange,
|
||||
): change is IChangeRequestUpdateStrategy => change.action === 'updateStrategy';
|
||||
|
||||
const addConstraintIdsToFeatureChange = (change: IFeatureChange) => {
|
||||
if (isAddStrategyChange(change) || isUpdateStrategyChange(change)) {
|
||||
const { constraints, ...rest } = change.payload;
|
||||
return {
|
||||
...change,
|
||||
payload: {
|
||||
...rest,
|
||||
constraints: constraints.map((constraint) => ({
|
||||
...constraint,
|
||||
[constraintId]: uuidv4(),
|
||||
})),
|
||||
},
|
||||
} as IFeatureChange;
|
||||
}
|
||||
return change;
|
||||
};
|
||||
|
||||
export const useChangeRequest = (projectId: string, id: string) => {
|
||||
const { data, error, mutate } = useSWR<ChangeRequestType>(
|
||||
formatApiPath(`api/admin/projects/${projectId}/change-requests/${id}`),
|
||||
@ -25,47 +43,25 @@ export const useChangeRequest = (projectId: string, id: string) => {
|
||||
{ refreshInterval: 15000 },
|
||||
);
|
||||
|
||||
const dataWithConstraintIds: ChangeRequestType | undefined = useMemo(() => {
|
||||
if (!data) {
|
||||
return data;
|
||||
}
|
||||
const { features, ...dataProps } = data || {};
|
||||
const featuresWithConstraintIds: IChangeRequestFeature[] | undefined =
|
||||
useMemo(() => {
|
||||
return (
|
||||
features?.map((feature) => {
|
||||
const changes: IFeatureChange[] = feature.changes.map(
|
||||
addConstraintIdsToFeatureChange,
|
||||
);
|
||||
|
||||
const features = data.features.map((feature) => {
|
||||
const changes: IFeatureChange[] = feature.changes.map((change) => {
|
||||
if (
|
||||
isAddStrategyChange(change) ||
|
||||
isUpdateStrategyChange(change)
|
||||
) {
|
||||
const { constraints, ...rest } = change.payload;
|
||||
return {
|
||||
...change,
|
||||
payload: {
|
||||
...rest,
|
||||
constraints: constraints.map((constraint) => ({
|
||||
...constraint,
|
||||
[constraintId]: uuidv4(),
|
||||
})),
|
||||
},
|
||||
} as IFeatureChange;
|
||||
}
|
||||
return change;
|
||||
});
|
||||
|
||||
return {
|
||||
...feature,
|
||||
changes,
|
||||
};
|
||||
});
|
||||
|
||||
const value: ChangeRequestType = {
|
||||
...data,
|
||||
features,
|
||||
};
|
||||
return value;
|
||||
}, [data]);
|
||||
...feature,
|
||||
changes,
|
||||
};
|
||||
}) ?? []
|
||||
);
|
||||
}, [JSON.stringify(features)]);
|
||||
|
||||
return {
|
||||
data: dataWithConstraintIds,
|
||||
data: { ...dataProps, features: featuresWithConstraintIds },
|
||||
loading: !error && !data,
|
||||
refetchChangeRequest: () => mutate(),
|
||||
error,
|
||||
|
@ -38,7 +38,9 @@ export const useFeature = (
|
||||
mutate().catch(console.warn);
|
||||
}, [mutate]);
|
||||
|
||||
const feature = useMemo(enrichConstraintsWithIds(data), [data?.body]);
|
||||
const feature = useMemo(enrichConstraintsWithIds(data), [
|
||||
JSON.stringify(data?.body),
|
||||
]);
|
||||
|
||||
return {
|
||||
feature,
|
||||
@ -68,41 +70,42 @@ export const featureFetcher = async (
|
||||
};
|
||||
};
|
||||
|
||||
export const enrichConstraintsWithIds = (data?: IFeatureResponse) => () => {
|
||||
if (!data?.body) {
|
||||
return emptyFeature;
|
||||
}
|
||||
export const enrichConstraintsWithIds =
|
||||
(data?: IFeatureResponse) => (): IFeatureToggle => {
|
||||
if (!data?.body) {
|
||||
return emptyFeature;
|
||||
}
|
||||
|
||||
const { strategies, environments, ...rest } = data.body;
|
||||
const { strategies, environments, ...rest } = data.body;
|
||||
|
||||
const addConstraintIds = (strategy: IFeatureStrategy) => {
|
||||
const { constraints, ...strategyRest } = strategy;
|
||||
return {
|
||||
...strategyRest,
|
||||
constraints: constraints?.map((constraint) => ({
|
||||
...constraint,
|
||||
[constraintId]: uuidv4(),
|
||||
})),
|
||||
};
|
||||
};
|
||||
|
||||
const strategiesWithConstraintIds = strategies?.map(addConstraintIds);
|
||||
|
||||
const environmentsWithStrategyIds = environments?.map((environment) => {
|
||||
const { strategies, ...environmentRest } = environment;
|
||||
return {
|
||||
...environmentRest,
|
||||
strategies: strategies?.map(addConstraintIds),
|
||||
};
|
||||
});
|
||||
|
||||
const addConstraintIds = (strategy: IFeatureStrategy) => {
|
||||
const { constraints, ...strategyRest } = strategy;
|
||||
return {
|
||||
...strategyRest,
|
||||
constraints: constraints?.map((constraint) => ({
|
||||
...constraint,
|
||||
[constraintId]: uuidv4(),
|
||||
})),
|
||||
...rest,
|
||||
strategies: strategiesWithConstraintIds,
|
||||
environments: environmentsWithStrategyIds,
|
||||
};
|
||||
};
|
||||
|
||||
const strategiesWithConstraintIds = strategies?.map(addConstraintIds);
|
||||
|
||||
const environmentsWithStrategyIds = environments?.map((environment) => {
|
||||
const { strategies, ...environmentRest } = environment;
|
||||
return {
|
||||
...environmentRest,
|
||||
strategies: strategies?.map(addConstraintIds),
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
...rest,
|
||||
strategies: strategiesWithConstraintIds,
|
||||
environments: environmentsWithStrategyIds,
|
||||
};
|
||||
};
|
||||
|
||||
export const formatFeatureApiPath = (
|
||||
projectId: string,
|
||||
featureId: string,
|
||||
|
@ -29,7 +29,9 @@ export const useFeatureImmutable = (
|
||||
await refetchFeature();
|
||||
}, [mutate, refetchFeature]);
|
||||
|
||||
const feature = useMemo(enrichConstraintsWithIds(data), [data?.body]);
|
||||
const feature = useMemo(enrichConstraintsWithIds(data), [
|
||||
JSON.stringify(data?.body),
|
||||
]);
|
||||
|
||||
return {
|
||||
feature,
|
||||
|
Loading…
Reference in New Issue
Block a user