Skip to content

Commit

Permalink
fetch reason
Browse files Browse the repository at this point in the history
  • Loading branch information
lerouxb committed Sep 23, 2024
1 parent 961971d commit 385106a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 31 deletions.
39 changes: 23 additions & 16 deletions packages/compass-indexes/src/modules/regular-indexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import {
} from '@mongodb-js/compass-components';

import { FetchStatuses, NOT_FETCHABLE_STATUSES } from '../utils/fetch-status';
import type { FetchStatus, FetchingStatus } from '../utils/fetch-status';
import type { FetchStatus } from '../utils/fetch-status';
import { FetchReasons } from '../utils/fetch-reason';
import type { FetchReason } from '../utils/fetch-reason';
import { isAction } from '../utils/is-action';
import { ActionTypes as CreateIndexActionTypes } from './create-index';
import type {
Expand Down Expand Up @@ -67,7 +69,7 @@ type IndexesClosedAction = {

type FetchIndexesStartedAction = {
type: ActionTypes.FetchIndexesStarted;
status: FetchingStatus;
reason: FetchReason;
};

type FetchIndexesSucceededAction = {
Expand Down Expand Up @@ -120,7 +122,12 @@ export default function reducer(
) {
return {
...state,
status: action.status,
status:
action.reason === FetchReasons.POLL
? FetchStatuses.POLLING
: action.reason === FetchReasons.REFRESH
? FetchStatuses.REFRESHING
: FetchStatuses.FETCHING,
};
}

Expand Down Expand Up @@ -241,10 +248,10 @@ export default function reducer(
}

const fetchIndexesStarted = (
status: FetchingStatus
reason: FetchReason
): FetchIndexesStartedAction => ({
type: ActionTypes.FetchIndexesStarted,
status,
reason,
});

const fetchIndexesSucceeded = (
Expand All @@ -265,7 +272,7 @@ type FetchIndexesActions =
| FetchIndexesFailedAction;

const fetchIndexes = (
newStatus: FetchingStatus
reason: FetchReason
): IndexesThunkAction<Promise<void>, FetchIndexesActions> => {
return async (dispatch, getState, { dataService, localAppRegistry }) => {
const {
Expand All @@ -285,7 +292,7 @@ const fetchIndexes = (
}

try {
dispatch(fetchIndexesStarted(newStatus));
dispatch(fetchIndexesStarted(reason));
// This makes sure that when the user or something else triggers a
// re-fetch for the list of indexes with this action, the tab header also
// gets updated.
Expand All @@ -306,11 +313,11 @@ export const refreshRegularIndexes = (): IndexesThunkAction<

// If we are in a READY state, then we have already fetched the data
// and are refreshing the list.
const newStatus: FetchStatus =
const reason: FetchReason =
status === FetchStatuses.READY
? FetchStatuses.REFRESHING
: FetchStatuses.FETCHING;
await dispatch(fetchIndexes(newStatus));
? FetchReasons.REFRESH
: FetchReasons.INITIAL_FETCH;
await dispatch(fetchIndexes(reason));
};
};

Expand All @@ -319,7 +326,7 @@ export const pollRegularIndexes = (): IndexesThunkAction<
FetchIndexesActions
> => {
return async (dispatch) => {
return await dispatch(fetchIndexes(FetchStatuses.POLLING));
return await dispatch(fetchIndexes(FetchReasons.POLL));
};
};

Expand Down Expand Up @@ -390,7 +397,7 @@ export const dropIndex = (

// By fetching the indexes again we make sure the merged list doesn't have
// it either.
await dispatch(fetchIndexes(FetchStatuses.REFRESHING));
await dispatch(fetchIndexes(FetchReasons.REFRESH));
return;
}

Expand All @@ -415,7 +422,7 @@ export const dropIndex = (
title: `Index "${indexName}" dropped`,
timeout: 3000,
});
await dispatch(fetchIndexes(FetchStatuses.REFRESHING));
await dispatch(fetchIndexes(FetchReasons.REFRESH));
} catch (err) {
openToast('drop-index-error', {
variant: 'important',
Expand Down Expand Up @@ -448,7 +455,7 @@ export const hideIndex = (
hidden: true,
},
});
await dispatch(fetchIndexes(FetchStatuses.REFRESHING));
await dispatch(fetchIndexes(FetchReasons.REFRESH));
} catch (error) {
openToast('hide-index-error', {
title: 'Failed to hide the index',
Expand Down Expand Up @@ -482,7 +489,7 @@ export const unhideIndex = (
hidden: false,
},
});
await dispatch(fetchIndexes(FetchStatuses.REFRESHING));
await dispatch(fetchIndexes(FetchReasons.REFRESH));
} catch (error) {
openToast('unhide-index-error', {
title: 'Failed to unhide the index',
Expand Down
38 changes: 23 additions & 15 deletions packages/compass-indexes/src/modules/search-indexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import type { SearchIndex } from 'mongodb-data-service';

import { isAction } from '../utils/is-action';
import { FetchStatuses, NOT_FETCHABLE_STATUSES } from '../utils/fetch-status';
import type { FetchStatus, FetchingStatus } from '../utils/fetch-status';
import type { FetchStatus } from '../utils/fetch-status';

import { FetchReasons } from '../utils/fetch-reason';
import type { FetchReason } from '../utils/fetch-reason';

import type { IndexesThunkAction } from '.';
import { switchToSearchIndexes } from './index-view';
Expand Down Expand Up @@ -44,7 +47,7 @@ export enum ActionTypes {

type FetchSearchIndexesStartedAction = {
type: ActionTypes.FetchSearchIndexesStarted;
status: FetchingStatus;
reason: FetchReason;
};

type FetchSearchIndexesSucceededAction = {
Expand Down Expand Up @@ -307,7 +310,12 @@ export default function reducer(
) {
return {
...state,
status: action.status,
status:
action.reason === FetchReasons.POLL
? FetchStatuses.POLLING
: action.reason === FetchReasons.REFRESH
? FetchStatuses.REFRESHING
: FetchStatuses.FETCHING,
};
}

Expand Down Expand Up @@ -367,10 +375,10 @@ export const updateSearchIndexClosed = (): UpdateSearchIndexClosedAction => ({
});

const fetchSearchIndexesStarted = (
status: FetchingStatus
reason: FetchReason
): FetchSearchIndexesStartedAction => ({
type: ActionTypes.FetchSearchIndexesStarted,
status,
reason,
});

const fetchSearchIndexesSucceeded = (
Expand Down Expand Up @@ -513,7 +521,7 @@ export const createIndex = ({
});

dispatch(switchToSearchIndexes());
await dispatch(fetchIndexes(FetchStatuses.REFRESHING));
await dispatch(fetchIndexes(FetchReasons.REFRESH));
};
};

Expand Down Expand Up @@ -566,7 +574,7 @@ export const updateIndex = ({
timeout: 5000,
variant: 'progress',
});
await dispatch(fetchIndexes(FetchStatuses.REFRESHING));
await dispatch(fetchIndexes(FetchReasons.REFRESH));
} catch (e) {
const error = (e as Error).message;
dispatch(
Expand All @@ -583,7 +591,7 @@ type FetchSearchIndexesActions =
| FetchSearchIndexesFailedAction;

const fetchIndexes = (
newStatus: FetchingStatus
reason: FetchReason
): IndexesThunkAction<Promise<void>, FetchSearchIndexesActions> => {
return async (dispatch, getState, { dataService }) => {
const {
Expand All @@ -603,7 +611,7 @@ const fetchIndexes = (
}

try {
dispatch(fetchSearchIndexesStarted(newStatus));
dispatch(fetchSearchIndexesStarted(reason));
const indexes = await dataService.getSearchIndexes(namespace);
dispatch(fetchSearchIndexesSucceeded(indexes));
} catch (err) {
Expand All @@ -621,11 +629,11 @@ export const refreshSearchIndexes = (): IndexesThunkAction<

// If we are in a READY state, then we have already fetched the data
// and are refreshing the list.
const newStatus: FetchStatus =
const reason: FetchReason =
status === FetchStatuses.READY
? FetchStatuses.REFRESHING
: FetchStatuses.FETCHING;
await dispatch(fetchIndexes(newStatus));
? FetchReasons.REFRESH
: FetchReasons.INITIAL_FETCH;
await dispatch(fetchIndexes(reason));
};
};

Expand All @@ -634,7 +642,7 @@ export const pollSearchIndexes = (): IndexesThunkAction<
FetchSearchIndexesActions
> => {
return async (dispatch) => {
return await dispatch(fetchIndexes(FetchStatuses.POLLING));
return await dispatch(fetchIndexes(FetchReasons.POLL));
};
};

Expand Down Expand Up @@ -679,7 +687,7 @@ export const dropSearchIndex = (
timeout: 5000,
variant: 'progress',
});
await dispatch(fetchIndexes(FetchStatuses.REFRESHING));
await dispatch(fetchIndexes(FetchReasons.REFRESH));
} catch (e) {
openToast('search-index-delete-failed', {
title: `Failed to drop index.`,
Expand Down
16 changes: 16 additions & 0 deletions packages/compass-indexes/src/utils/fetch-reason.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export enum FetchReasons {
/**
*
*/
INITIAL_FETCH = 'INITIAL_FETCH',
/**
*
*/
REFRESH = 'REFRESH',
/**
*
*/
POLL = 'POLL',
}

export type FetchReason = keyof typeof FetchReasons;

0 comments on commit 385106a

Please sign in to comment.