2020-04-13 08:24:13 +00:00
|
|
|
import { reduxBatch } from "@manaflair/redux-batch";
|
2022-03-25 10:43:26 +00:00
|
|
|
import { createStore, applyMiddleware, compose, Middleware } from "redux";
|
2019-12-23 12:16:33 +00:00
|
|
|
import {
|
|
|
|
|
useSelector as useReduxSelector,
|
|
|
|
|
TypedUseSelectorHook,
|
|
|
|
|
} from "react-redux";
|
|
|
|
|
import appReducer, { AppState } from "./reducers";
|
|
|
|
|
import createSagaMiddleware from "redux-saga";
|
2020-04-13 08:24:13 +00:00
|
|
|
import { rootSaga } from "sagas";
|
2019-12-23 12:16:33 +00:00
|
|
|
import { composeWithDevTools } from "redux-devtools-extension/logOnlyInProduction";
|
2020-08-28 11:31:07 +00:00
|
|
|
import * as Sentry from "@sentry/react";
|
2022-04-12 10:50:01 +00:00
|
|
|
import {
|
|
|
|
|
ReduxAction,
|
|
|
|
|
ReduxActionTypes,
|
|
|
|
|
} from "@appsmith/constants/ReduxActionConstants";
|
2022-04-04 11:11:52 +00:00
|
|
|
import { getRouteBuilderParams, updateURLFactory } from "RouteBuilder";
|
|
|
|
|
import { updateSlugNamesInURL } from "utils/helpers";
|
2019-12-23 12:16:33 +00:00
|
|
|
|
|
|
|
|
const sagaMiddleware = createSagaMiddleware();
|
2020-08-28 11:31:07 +00:00
|
|
|
const sentryReduxEnhancer = Sentry.createReduxEnhancer({
|
2020-12-24 04:32:25 +00:00
|
|
|
actionTransformer: (action) => {
|
2020-12-09 09:12:12 +00:00
|
|
|
if (
|
|
|
|
|
action.type === ReduxActionTypes.SET_EVALUATED_TREE ||
|
2021-08-27 09:25:28 +00:00
|
|
|
action.type === ReduxActionTypes.EXECUTE_PLUGIN_ACTION_SUCCESS
|
2020-12-09 09:12:12 +00:00
|
|
|
) {
|
|
|
|
|
// Return null to not log the action to Sentry
|
|
|
|
|
action.payload = null;
|
|
|
|
|
}
|
|
|
|
|
return action;
|
|
|
|
|
},
|
2020-08-28 11:31:07 +00:00
|
|
|
});
|
|
|
|
|
|
2022-03-25 10:43:26 +00:00
|
|
|
const routeParamsMiddleware: Middleware = () => (next: any) => (
|
|
|
|
|
action: ReduxAction<any>,
|
|
|
|
|
) => {
|
|
|
|
|
switch (action.type) {
|
|
|
|
|
case ReduxActionTypes.FETCH_APPLICATION_SUCCESS: {
|
|
|
|
|
const { applicationVersion, id, slug } = action.payload;
|
|
|
|
|
updateURLFactory({
|
|
|
|
|
applicationId: id,
|
|
|
|
|
applicationSlug: slug,
|
|
|
|
|
applicationVersion,
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ReduxActionTypes.CURRENT_APPLICATION_NAME_UPDATE: {
|
|
|
|
|
const { slug } = action.payload;
|
|
|
|
|
updateURLFactory({ applicationSlug: slug });
|
2022-04-04 11:11:52 +00:00
|
|
|
updateSlugNamesInURL({
|
|
|
|
|
applicationSlug: slug,
|
|
|
|
|
});
|
2022-03-25 10:43:26 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2022-04-04 15:25:03 +00:00
|
|
|
case ReduxActionTypes.SWITCH_CURRENT_PAGE_ID: {
|
|
|
|
|
const id = action.payload.id;
|
|
|
|
|
const slug = action.payload.slug;
|
|
|
|
|
updateURLFactory({ pageId: id, pageSlug: slug });
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-03-25 10:43:26 +00:00
|
|
|
case ReduxActionTypes.UPDATE_PAGE_SUCCESS: {
|
2022-04-04 11:11:52 +00:00
|
|
|
const id = action.payload.id;
|
|
|
|
|
const slug = action.payload.slug;
|
|
|
|
|
const { pageId } = getRouteBuilderParams();
|
2022-04-04 15:25:03 +00:00
|
|
|
// Update route params and page slug in URL only if the current page is updated
|
|
|
|
|
if (pageId === id) {
|
|
|
|
|
updateURLFactory({ pageSlug: slug });
|
2022-04-04 11:11:52 +00:00
|
|
|
updateSlugNamesInURL({
|
|
|
|
|
pageSlug: slug,
|
|
|
|
|
});
|
2022-04-04 15:25:03 +00:00
|
|
|
}
|
2022-03-25 10:43:26 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ReduxActionTypes.UPDATE_APPLICATION_SUCCESS:
|
|
|
|
|
const { applicationVersion } = action.payload;
|
|
|
|
|
updateURLFactory({ applicationVersion });
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return next(action);
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-23 12:16:33 +00:00
|
|
|
export default createStore(
|
|
|
|
|
appReducer,
|
2020-08-28 11:31:07 +00:00
|
|
|
composeWithDevTools(
|
|
|
|
|
reduxBatch,
|
2022-03-25 10:43:26 +00:00
|
|
|
applyMiddleware(sagaMiddleware, routeParamsMiddleware),
|
2020-08-28 11:31:07 +00:00
|
|
|
reduxBatch,
|
|
|
|
|
sentryReduxEnhancer,
|
|
|
|
|
),
|
2019-12-23 12:16:33 +00:00
|
|
|
);
|
2021-04-22 03:30:09 +00:00
|
|
|
|
|
|
|
|
export const testStore = (initialState: Partial<AppState>) =>
|
|
|
|
|
createStore(
|
|
|
|
|
appReducer,
|
|
|
|
|
initialState,
|
2022-03-25 10:43:26 +00:00
|
|
|
compose(
|
|
|
|
|
reduxBatch,
|
|
|
|
|
applyMiddleware(sagaMiddleware, routeParamsMiddleware),
|
|
|
|
|
reduxBatch,
|
|
|
|
|
),
|
2021-04-22 03:30:09 +00:00
|
|
|
);
|
|
|
|
|
|
2019-12-23 12:16:33 +00:00
|
|
|
sagaMiddleware.run(rootSaga);
|
|
|
|
|
|
|
|
|
|
export const useSelector: TypedUseSelectorHook<AppState> = useReduxSelector;
|