PromucFlow_constructor/app/client/src/reducers/entityReducers/metaReducer.ts
Nikhil Nandagopal 51a267c8f2
Fix/release (#254)
* Added Redux action to clear widget meta properties when page is fetched

* reset widget meta for view mode too

Co-authored-by: Nikhil Nandagopal <nikhil@appsmith.com>
2020-08-10 12:22:47 +05:30

63 lines
1.8 KiB
TypeScript

import { createReducer } from "utils/AppsmithUtils";
import { ReduxActionTypes, ReduxAction } from "constants/ReduxActionConstants";
import { UpdateWidgetMetaPropertyPayload } from "actions/metaActions";
export type MetaState = Record<string, object>;
const initialState: MetaState = {};
export const metaReducer = createReducer(initialState, {
[ReduxActionTypes.SET_META_PROP]: (
state: MetaState,
action: ReduxAction<UpdateWidgetMetaPropertyPayload>,
) => {
const next = { ...state };
let widgetMetaProps: Record<string, any> = next[action.payload.widgetId];
if (widgetMetaProps === undefined) {
widgetMetaProps = {};
next[action.payload.widgetId] = widgetMetaProps;
}
(widgetMetaProps as Record<string, any>)[action.payload.propertyName] =
action.payload.propertyValue;
return next;
},
[ReduxActionTypes.WIDGET_DELETE]: (
state: MetaState,
action: ReduxAction<{ widgetId: string }>,
) => {
const next = { ...state };
delete next[action.payload.widgetId];
return next;
},
[ReduxActionTypes.RESET_WIDGET_META]: (
state: MetaState,
action: ReduxAction<{ widgetId: string }>,
) => {
const widgetId = action.payload.widgetId;
if (widgetId in state) {
const resetData: Record<string, any> = {
...state[widgetId],
};
Object.keys(resetData).forEach((key: string) => {
resetData[key] = undefined;
});
return { ...state, [widgetId]: { ...resetData } };
}
return state;
},
[ReduxActionTypes.FETCH_PAGE_SUCCESS]: (
state: MetaState,
action: ReduxAction<{ widgetId: string }>,
) => {
return initialState;
},
[ReduxActionTypes.FETCH_PUBLISHED_PAGE_SUCCESS]: (
state: MetaState,
action: ReduxAction<{ widgetId: string }>,
) => {
return initialState;
},
});
export default metaReducer;