mirror of
https://github.com/Unleash/unleash.git
synced 2025-08-04 13:48:56 +02:00
Propagate change request constraint id mapper
This commit is contained in:
parent
c9efa26365
commit
2916ab3c40
@ -3,38 +3,11 @@ import { formatApiPath } from 'utils/formatPath';
|
|||||||
import handleErrorResponses from '../httpErrorResponseHandler.js';
|
import handleErrorResponses from '../httpErrorResponseHandler.js';
|
||||||
import type {
|
import type {
|
||||||
ChangeRequestType,
|
ChangeRequestType,
|
||||||
IChangeRequestAddStrategy,
|
|
||||||
IChangeRequestFeature,
|
IChangeRequestFeature,
|
||||||
IChangeRequestUpdateStrategy,
|
|
||||||
IFeatureChange,
|
IFeatureChange,
|
||||||
} from 'component/changeRequest/changeRequest.types';
|
} from 'component/changeRequest/changeRequest.types';
|
||||||
import { useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
import { constraintId } from 'constants/constraintId.js';
|
import { addConstraintIdsToFeatureChange } from 'utils/addConstraintIdsToFeatureChange.js';
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
|
||||||
|
|
||||||
const isAddStrategyChange = (
|
|
||||||
change: IFeatureChange,
|
|
||||||
): change is IChangeRequestAddStrategy => change.action === 'addStrategy';
|
|
||||||
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) => {
|
export const useChangeRequest = (projectId: string, id: string) => {
|
||||||
const { data, error, mutate } = useSWR<ChangeRequestType>(
|
const { data, error, mutate } = useSWR<ChangeRequestType>(
|
||||||
@ -60,8 +33,15 @@ export const useChangeRequest = (projectId: string, id: string) => {
|
|||||||
);
|
);
|
||||||
}, [JSON.stringify(features)]);
|
}, [JSON.stringify(features)]);
|
||||||
|
|
||||||
|
const mappedData = data
|
||||||
|
? {
|
||||||
|
...dataProps,
|
||||||
|
features: featuresWithConstraintIds,
|
||||||
|
}
|
||||||
|
: data;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
data: { ...dataProps, features: featuresWithConstraintIds },
|
data: mappedData,
|
||||||
loading: !error && !data,
|
loading: !error && !data,
|
||||||
refetchChangeRequest: () => mutate(),
|
refetchChangeRequest: () => mutate(),
|
||||||
error,
|
error,
|
||||||
|
@ -1,7 +1,12 @@
|
|||||||
import { formatApiPath } from 'utils/formatPath';
|
import { formatApiPath } from 'utils/formatPath';
|
||||||
import handleErrorResponses from '../httpErrorResponseHandler.js';
|
import handleErrorResponses from '../httpErrorResponseHandler.js';
|
||||||
import type { ChangeRequestType } from 'component/changeRequest/changeRequest.types';
|
import type {
|
||||||
|
ChangeRequestType,
|
||||||
|
IFeatureChange,
|
||||||
|
} from 'component/changeRequest/changeRequest.types';
|
||||||
import { useEnterpriseSWR } from '../useEnterpriseSWR/useEnterpriseSWR.js';
|
import { useEnterpriseSWR } from '../useEnterpriseSWR/useEnterpriseSWR.js';
|
||||||
|
import { useMemo } from 'react';
|
||||||
|
import { addConstraintIdsToFeatureChange } from 'utils/addConstraintIdsToFeatureChange.js';
|
||||||
|
|
||||||
const fetcher = (path: string) => {
|
const fetcher = (path: string) => {
|
||||||
return fetch(path)
|
return fetch(path)
|
||||||
@ -16,8 +21,29 @@ export const usePendingChangeRequests = (project: string) => {
|
|||||||
fetcher,
|
fetcher,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const mappedData: typeof data = useMemo(
|
||||||
|
() =>
|
||||||
|
data?.map((changeRequest) => {
|
||||||
|
const { features, ...rest } = changeRequest || {};
|
||||||
|
const featuresWithConstraintIds =
|
||||||
|
features?.map((feature) => {
|
||||||
|
const changes: IFeatureChange[] = feature.changes.map(
|
||||||
|
addConstraintIdsToFeatureChange,
|
||||||
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
data,
|
...feature,
|
||||||
|
changes,
|
||||||
|
};
|
||||||
|
}) ?? [];
|
||||||
|
|
||||||
|
return { ...rest, features: featuresWithConstraintIds };
|
||||||
|
}),
|
||||||
|
[JSON.stringify(data)],
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
mappedData,
|
||||||
loading: !error && !data,
|
loading: !error && !data,
|
||||||
refetch: mutate,
|
refetch: mutate,
|
||||||
error,
|
error,
|
||||||
|
31
frontend/src/utils/addConstraintIdsToFeatureChange.ts
Normal file
31
frontend/src/utils/addConstraintIdsToFeatureChange.ts
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import type {
|
||||||
|
IFeatureChange,
|
||||||
|
IChangeRequestAddStrategy,
|
||||||
|
IChangeRequestUpdateStrategy,
|
||||||
|
} from 'component/changeRequest/changeRequest.types';
|
||||||
|
import { constraintId } from 'constants/constraintId';
|
||||||
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
|
|
||||||
|
const isAddStrategyChange = (
|
||||||
|
change: IFeatureChange,
|
||||||
|
): change is IChangeRequestAddStrategy => change.action === 'addStrategy';
|
||||||
|
const isUpdateStrategyChange = (
|
||||||
|
change: IFeatureChange,
|
||||||
|
): change is IChangeRequestUpdateStrategy => change.action === 'updateStrategy';
|
||||||
|
|
||||||
|
export 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;
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user