|
1 | 1 | import type { Action, Reducer } from 'redux'; |
2 | 2 | import type { GlobalWritesThunkAction } from '.'; |
3 | | -import { openToast } from '@mongodb-js/compass-components'; |
| 3 | +import { openToast, showConfirmation } from '@mongodb-js/compass-components'; |
4 | 4 | import type { ManagedNamespace } from '../services/atlas-global-writes-service'; |
5 | 5 |
|
6 | 6 | export function isAction<A extends Action>( |
@@ -30,6 +30,10 @@ enum GlobalWritesActionTypes { |
30 | 30 | SubmittingForShardingFinished = 'global-writes/SubmittingForShardingFinished', |
31 | 31 | SubmittingForShardingErrored = 'global-writes/SubmittingForShardingErrored', |
32 | 32 |
|
| 33 | + CancellingShardingStarted = 'global-writes/CancellingShardingStarted', |
| 34 | + CancellingShardingFinished = 'global-writes/CancellingShardingFinished', |
| 35 | + CancellingShardingErrored = 'global-writes/CancellingShardingErrored', |
| 36 | + |
33 | 37 | UnmanagingNamespaceStarted = 'global-writes/UnmanagingNamespaceStarted', |
34 | 38 | UnmanagingNamespaceFinished = 'global-writes/UnmanagingNamespaceFinished', |
35 | 39 | UnmanagingNamespaceErrored = 'global-writes/UnmanagingNamespaceErrored', |
@@ -68,6 +72,19 @@ type SubmittingForShardingErroredAction = { |
68 | 72 | type: GlobalWritesActionTypes.SubmittingForShardingErrored; |
69 | 73 | }; |
70 | 74 |
|
| 75 | +type CancellingShardingStartedAction = { |
| 76 | + type: GlobalWritesActionTypes.CancellingShardingStarted; |
| 77 | +}; |
| 78 | + |
| 79 | +type CancellingShardingFinishedAction = { |
| 80 | + type: GlobalWritesActionTypes.CancellingShardingFinished; |
| 81 | + managedNamespace?: ManagedNamespace; |
| 82 | +}; |
| 83 | + |
| 84 | +type CancellingShardingErroredAction = { |
| 85 | + type: GlobalWritesActionTypes.CancellingShardingErrored; |
| 86 | +}; |
| 87 | + |
71 | 88 | type UnmanagingNamespaceStartedAction = { |
72 | 89 | type: GlobalWritesActionTypes.UnmanagingNamespaceStarted; |
73 | 90 | }; |
@@ -102,6 +119,12 @@ export enum ShardingStatuses { |
102 | 119 | */ |
103 | 120 | SHARDING = 'SHARDING', |
104 | 121 |
|
| 122 | + /** |
| 123 | + * State when user cancels the sharding and |
| 124 | + * we are waiting for server to accept the request. |
| 125 | + */ |
| 126 | + CANCEL_SHARDING = 'CANCEL_SHARDING', |
| 127 | + |
105 | 128 | /** |
106 | 129 | * Sharding failed. |
107 | 130 | */ |
@@ -434,6 +457,56 @@ export const createShardKey = ( |
434 | 457 | }; |
435 | 458 | }; |
436 | 459 |
|
| 460 | +export const cancelSharding = (): |
| 461 | + | GlobalWritesThunkAction< |
| 462 | + Promise<void>, |
| 463 | + | CancellingShardingStartedAction |
| 464 | + | CancellingShardingFinishedAction |
| 465 | + | CancellingShardingErroredAction |
| 466 | + > |
| 467 | + | undefined => { |
| 468 | + return async (dispatch, getState, { atlasGlobalWritesService, logger }) => { |
| 469 | + const confirmed = await showConfirmation({ |
| 470 | + title: 'Confirmation', |
| 471 | + description: 'Are you sure you want to cancel the sharding request?', |
| 472 | + }); |
| 473 | + |
| 474 | + if (!confirmed) return; |
| 475 | + |
| 476 | + const { namespace } = getState(); |
| 477 | + dispatch({ |
| 478 | + type: GlobalWritesActionTypes.CancellingShardingStarted, |
| 479 | + }); |
| 480 | + |
| 481 | + try { |
| 482 | + await atlasGlobalWritesService.unmanageNamespace(namespace); |
| 483 | + dispatch({ |
| 484 | + type: GlobalWritesActionTypes.CancellingShardingFinished, |
| 485 | + }); |
| 486 | + } catch (error) { |
| 487 | + logger.log.error( |
| 488 | + logger.mongoLogId(1_001_000_331), |
| 489 | + 'AtlasFetchError', |
| 490 | + 'Error cancelling the sharding process', |
| 491 | + { |
| 492 | + error: (error as Error).message, |
| 493 | + } |
| 494 | + ); |
| 495 | + openToast('global-writes-cancel-sharding-error', { |
| 496 | + title: `Failed to cancel the sharding process: ${ |
| 497 | + (error as Error).message |
| 498 | + }`, |
| 499 | + dismissible: true, |
| 500 | + timeout: 5000, |
| 501 | + variant: 'important', |
| 502 | + }); |
| 503 | + dispatch({ |
| 504 | + type: GlobalWritesActionTypes.CancellingShardingErrored, |
| 505 | + }); |
| 506 | + } |
| 507 | + }; |
| 508 | +}; |
| 509 | + |
437 | 510 | const setNamespaceBeingSharded = ( |
438 | 511 | managedNamespace?: ManagedNamespace |
439 | 512 | ): GlobalWritesThunkAction<void, SubmittingForShardingFinishedAction> => { |
|
0 commit comments