Skip to content

Commit

Permalink
use state when waiting for server to accept sharding
Browse files Browse the repository at this point in the history
  • Loading branch information
mabaasit committed Sep 26, 2024
1 parent 9437e07 commit 23c1361
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 39 deletions.
5 changes: 4 additions & 1 deletion packages/compass-global-writes/src/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ function ShardingStateView({
);
}

if (shardingStatus === ShardingStatuses.UNSHARDED) {
if (
shardingStatus === ShardingStatuses.UNSHARDED ||
shardingStatus === ShardingStatuses.SUBMITTING_FOR_SHARDING
) {
return <UnshardedState />;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
import { useAutocompleteFields } from '@mongodb-js/compass-field-store';
import { connect } from 'react-redux';
import type { CreateShardKeyData, RootState } from '../../store/reducer';
import { createShardKey } from '../../store/reducer';
import { createShardKey, ShardingStatuses } from '../../store/reducer';

const nbsp = '\u00a0';

Expand Down Expand Up @@ -329,7 +329,7 @@ export function UnshardedState(props: UnshardedStateProps) {
export default connect(
(state: RootState) => ({
namespace: state.namespace,
isLoading: state.createShardkey.isLoading,
isLoading: state.status === ShardingStatuses.SUBMITTING_FOR_SHARDING,
}),
{
onCreateShardKey: createShardKey,
Expand Down
65 changes: 29 additions & 36 deletions packages/compass-global-writes/src/store/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,26 @@ export type CreateShardKeyData = Pick<

enum GlobalWritesActionTypes {
IsManagedNamespaceFetched = 'global-writes/IsManagedNamespaceFetched',
ShardingInProgressStarted = 'global-writes/ShardingInProgressStarted',
ShardingInProgressFinished = 'global-writes/ShardingInProgressFinished',
ShardingInProgressErrored = 'global-writes/ShardingInProgressErrored',
SubmittingForShardingStarted = 'global-writes/SubmittingForShardingStarted',
SubmittingForShardingFinished = 'global-writes/SubmittingForShardingFinished',
SubmittingForShardingErrored = 'global-writes/SubmittingForShardingErrored',
}

type IsManagedNamespaceFetchedAction = {
type: GlobalWritesActionTypes.IsManagedNamespaceFetched;
isNamespaceManaged: boolean;
};

type ShardingInProgressStartedAction = {
type: GlobalWritesActionTypes.ShardingInProgressStarted;
type SubmittingForShardingStartedAction = {
type: GlobalWritesActionTypes.SubmittingForShardingStarted;
};

type ShardingInProgressFinishedAction = {
type: GlobalWritesActionTypes.ShardingInProgressFinished;
type SubmittingForShardingFinishedAction = {
type: GlobalWritesActionTypes.SubmittingForShardingFinished;
};

type ShardingInProgressErroredAction = {
type: GlobalWritesActionTypes.ShardingInProgressErrored;
type SubmittingForShardingErroredAction = {
type: GlobalWritesActionTypes.SubmittingForShardingErrored;
};

export enum ShardingStatuses {
Expand All @@ -54,6 +54,12 @@ export enum ShardingStatuses {
*/
UNSHARDED = 'UNSHARDED',

/**
* State when user submits namespace to be sharded and
* we are waiting for server to accept the request.
*/
SUBMITTING_FOR_SHARDING = 'SUBMITTING_FOR_SHARDING',

/**
* Namespace is being sharded.
*/
Expand All @@ -66,18 +72,12 @@ export type RootState = {
namespace: string;
isNamespaceSharded: boolean;
status: ShardingStatus;
createShardkey: {
isLoading: boolean;
};
};

const initialState: RootState = {
namespace: '',
isNamespaceSharded: false,
status: ShardingStatuses.NOT_READY,
createShardkey: {
isLoading: false,
},
};

const reducer: Reducer<RootState, Action> = (state = initialState, action) => {
Expand All @@ -97,46 +97,39 @@ const reducer: Reducer<RootState, Action> = (state = initialState, action) => {
}

if (
isAction<ShardingInProgressStartedAction>(
isAction<SubmittingForShardingStartedAction>(
action,
GlobalWritesActionTypes.ShardingInProgressStarted
GlobalWritesActionTypes.SubmittingForShardingStarted
)
) {
return {
...state,
createShardkey: {
isLoading: true,
},
status: ShardingStatuses.SUBMITTING_FOR_SHARDING,
};
}

if (
isAction<ShardingInProgressFinishedAction>(
isAction<SubmittingForShardingFinishedAction>(
action,
GlobalWritesActionTypes.ShardingInProgressFinished
GlobalWritesActionTypes.SubmittingForShardingFinished
)
) {
return {
...state,
isNamespaceSharded: true,
status: ShardingStatuses.SHARDING,
createShardkey: {
isLoading: false,
},
};
}

if (
isAction<ShardingInProgressErroredAction>(
isAction<SubmittingForShardingErroredAction>(
action,
GlobalWritesActionTypes.ShardingInProgressErrored
GlobalWritesActionTypes.SubmittingForShardingErrored
)
) {
return {
...state,
createShardkey: {
isLoading: false,
},
status: ShardingStatuses.UNSHARDED,
};
}

Expand Down Expand Up @@ -198,9 +191,9 @@ export const createShardKey =
data: CreateShardKeyData
): GlobalWritesThunkAction<
Promise<void>,
| ShardingInProgressStartedAction
| ShardingInProgressFinishedAction
| ShardingInProgressErroredAction
| SubmittingForShardingStartedAction
| SubmittingForShardingFinishedAction
| SubmittingForShardingErroredAction
> =>
async (
dispatch,
Expand All @@ -215,7 +208,7 @@ export const createShardKey =
const { clusterName, projectId } = connectionInfoRef.current.atlasMetadata;

dispatch({
type: GlobalWritesActionTypes.ShardingInProgressStarted,
type: GlobalWritesActionTypes.SubmittingForShardingStarted,
});

try {
Expand All @@ -224,7 +217,7 @@ export const createShardKey =
clusterName,
});
dispatch({
type: GlobalWritesActionTypes.ShardingInProgressFinished,
type: GlobalWritesActionTypes.SubmittingForShardingFinished,
});
} catch (error) {
logger.log.error(
Expand All @@ -243,7 +236,7 @@ export const createShardKey =
variant: 'important',
});
dispatch({
type: GlobalWritesActionTypes.ShardingInProgressErrored,
type: GlobalWritesActionTypes.SubmittingForShardingErrored,
});
}
};
Expand Down

0 comments on commit 23c1361

Please sign in to comment.