Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useMemo, useState } from 'react';
import React, { useEffect, useMemo, useState } from 'react';
import { useIntl } from 'react-intl';
import { observer } from 'mobx-react';
import { BBox, Feature, GeoJsonProperties, Geometry, Polygon } from 'geojson';
Expand All @@ -21,7 +21,14 @@ import { useEnums } from '../../../../common/hooks/useEnum.hook';
import CONFIG from '../../../../common/config';
import { getErrorMessage, IServerError } from '../../../../common/components/error/helpers';
import { GeojsonFeatureInput } from '../../../models/RootStore.base';
import { LayerRasterRecordModelType, RecordType, useStore } from '../../../models';
import {
LayerRasterRecordModelType,
RecordType,
RootStoreType,
useQuery,
useStore,
} from '../../../models';
import { ProductType } from '../../../models/ProductTypeEnum';
import useZoomLevelsTable from '../../export-layer/hooks/useZoomLevelsTable';
import { ActionDialogProps, DestructiveActionDialog } from '../destructive-action-dialog';
import { useRasterBackupData } from './use-raster-backup-data.hook';
Expand All @@ -43,6 +50,8 @@ import { FeatureType } from './feature-type.enum';

import './entity.raster.revert-dialog.css';

type RevertRasterLayerResult = Awaited<ReturnType<RootStoreType['mutateRevertRasterLayer']>>;

const WFS_BUFFER_DELTA = -0.2;
const NO_VALUE = '–';

Expand All @@ -69,10 +78,15 @@ export const EntityRevertRasterDialog: React.FC<ActionDialogProps> = observer(
const ZOOM_LEVELS_TABLE = useZoomLevelsTable();
const currentLayer = props.layerRecord as LayerRasterRecordModelType;

const mutationQuery = useQuery<RevertRasterLayerResult>();

const [mutationError, setMutationError] = useState<any>(null);
const [polygonPartsError, setPolygonPartsError] = useState<Record<string, string[]> | null>(
null
);
const [showChangedArea, setShowChangedArea] = useState(true);
const [showBackup, setShowBackup] = useState(true);
const [showExisting, setShowExisting] = useState(false);
const [submitError, setSubmitError] = useState<IServerError>();

const {
backupMetadata,
Expand Down Expand Up @@ -215,6 +229,32 @@ export const EntityRevertRasterDialog: React.FC<ActionDialogProps> = observer(
[changedArea]
);

useEffect(() => {
if (store.discreteLayersStore.customValidationError) {
setPolygonPartsError(store.discreteLayersStore.customValidationError);
setMutationError(null);
} else {
setPolygonPartsError(null);
}
}, [store.discreteLayersStore.customValidationError]);

useEffect(() => {
return () => {
store.discreteLayersStore.clearCustomValidationError();
};
}, []);

useEffect(() => {
if (mutationQuery.data && !mutationQuery.error) {
props.onSuccess?.();
props.onSetOpen(false);
}
if (mutationQuery.error) {
setMutationError(mutationQuery.error);
setPolygonPartsError(null);
}
}, [mutationQuery.data, mutationQuery.error]);

const closeDialog = (): void => {
props.onSetOpen(false);
};
Expand All @@ -234,23 +274,24 @@ export const EntityRevertRasterDialog: React.FC<ActionDialogProps> = observer(
};

const revertLayer = (approverName: string, approvalCode: string): void => {
const MOCK_CONFLICTING_JOB_ID = 'b3df1d88-6c06-4995-849b-f2c9c022f079';
try {
// eslint-disable-next-line no-console
console.log('[EntityRevertRasterDialog] mock revert submit', {
id: props.layerRecord.id,
type: props.layerRecord.type as RecordType,
approverName,
approvalCode,
});
// TODO: remove mock throw once a real revert mutation exists
throw new Error(`Revert failed, job ${MOCK_CONFLICTING_JOB_ID} is already in progress`);
} catch (error) {
setSubmitError(error as IServerError);
}
mutationQuery.setQuery(
store.mutateRevertRasterLayer({
data: {
type: currentLayer.type as RecordType,
productId: currentLayer.productId as string,
productType: currentLayer.productType as ProductType,
productVersion: currentLayer.productVersion as string,
approverName,
approvalCode,
},
})
);
};

const submitErrorJobId = useMemo(() => extractJobIdFromError(submitError), [submitError]);
const submitErrorJobId = useMemo(
() => extractJobIdFromError(mutationError as IServerError | undefined),
[mutationError]
);

return (
<DestructiveActionDialog
Expand All @@ -262,9 +303,14 @@ export const EntityRevertRasterDialog: React.FC<ActionDialogProps> = observer(
disclaimerActionId="action.dialog.revert"
onClose={closeDialog}
onSubmit={revertLayer}
loading={loading}
loading={loading || mutationQuery.loading}
openRelatedJob={submitErrorJobId ? { jobId: submitErrorJobId } : undefined}
error={metadataError || outerPerimeterError || submitError}
error={metadataError || outerPerimeterError || mutationError}
polygonPartsError={polygonPartsError}
onFieldsValidate={(): void => {
setMutationError(null);
setPolygonPartsError(null);
}}
map={
<OlLayerMap
layerRecord={props.layerRecord}
Expand Down
14 changes: 13 additions & 1 deletion src/discrete-layer/models/RootStore.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,14 @@ export type JobActionParams = {
export type JobApproveAndResumeData = {
approver: string
}
export type RevertRasterLayerData = {
type: RecordType
productId: string
productType: ProductType
productVersion: string
approverName: string
approvalCode: string
}
/* The TypeScript type that explicits the refs to other models in order to prevent a circular refs issue */
type Refs = {
layerRasterRecords: ObservableMap<string, LayerRasterRecordModelType>,
Expand Down Expand Up @@ -529,7 +537,8 @@ mutateDeleteRasterLayer="mutateDeleteRasterLayer",
mutateUpdateJob="mutateUpdateJob",
mutateJobAbort="mutateJobAbort",
mutateJobRetry="mutateJobRetry",
mutateJobApproveAndResume="mutateJobApproveAndResume"
mutateJobApproveAndResume="mutateJobApproveAndResume",
mutateRevertRasterLayer="mutateRevertRasterLayer"
}

/**
Expand Down Expand Up @@ -771,4 +780,7 @@ export const RootStoreBase = withTypedRefs<Refs>()(MSTGQLStore
mutateJobApproveAndResume(variables: { data: JobApproveAndResumeData, jobApproveAndResumeParams: JobActionParams }, optimisticUpdate?: () => void) {
return self.mutate<{ jobApproveAndResume: string }>(`mutation jobApproveAndResume($data: JobApproveAndResumeData!, $jobApproveAndResumeParams: JobActionParams!) { jobApproveAndResume(data: $data, jobApproveAndResumeParams: $jobApproveAndResumeParams) }`, variables, optimisticUpdate)
},
mutateRevertRasterLayer(variables: { data: RevertRasterLayerData }, optimisticUpdate?: () => void) {
return self.mutate<{ revertRasterLayer: string }>(`mutation revertRasterLayer($data: RevertRasterLayerData!) { revertRasterLayer(data: $data) }`, variables, optimisticUpdate)
},
})))
Loading