2019-09-16 08:08:03 +00:00
|
|
|
import CanvasWidgetsNormalizer from "../normalizers/CanvasWidgetsNormalizer";
|
|
|
|
|
import {
|
|
|
|
|
ReduxActionTypes,
|
|
|
|
|
ReduxAction,
|
|
|
|
|
LoadCanvasWidgetsPayload,
|
|
|
|
|
} from "../constants/ReduxActionConstants";
|
2019-09-17 15:09:55 +00:00
|
|
|
import {
|
2019-09-23 10:27:45 +00:00
|
|
|
updateCanvas,
|
2019-09-17 15:09:55 +00:00
|
|
|
savePageError,
|
|
|
|
|
savePageSuccess,
|
2019-09-18 10:48:56 +00:00
|
|
|
fetchPageError,
|
2019-09-17 15:09:55 +00:00
|
|
|
} from "../actions/pageActions";
|
|
|
|
|
import PageApi, {
|
|
|
|
|
FetchPageResponse,
|
|
|
|
|
SavePageResponse,
|
|
|
|
|
FetchPageRequest,
|
|
|
|
|
SavePageRequest,
|
|
|
|
|
} from "../api/PageApi";
|
2019-09-19 22:25:37 +00:00
|
|
|
import { FlattenedWidgetProps } from "../reducers/entityReducers/canvasWidgetsReducer";
|
2019-09-22 20:25:05 +00:00
|
|
|
import {
|
|
|
|
|
call,
|
|
|
|
|
select,
|
|
|
|
|
put,
|
|
|
|
|
takeLatest,
|
|
|
|
|
takeEvery,
|
|
|
|
|
all,
|
|
|
|
|
} from "redux-saga/effects";
|
2019-09-18 10:48:56 +00:00
|
|
|
import { extractCurrentDSL } from "./utils";
|
2019-09-22 20:25:05 +00:00
|
|
|
import { getEditorConfigs } from "./selectors";
|
2019-03-30 12:30:42 +00:00
|
|
|
|
2019-09-19 22:25:37 +00:00
|
|
|
export function* fetchPageSaga(
|
|
|
|
|
pageRequestAction: ReduxAction<FetchPageRequest>,
|
|
|
|
|
) {
|
2019-09-16 08:08:03 +00:00
|
|
|
const pageRequest = pageRequestAction.payload;
|
2019-08-26 12:41:21 +00:00
|
|
|
try {
|
2019-09-18 10:48:56 +00:00
|
|
|
const fetchPageResponse: FetchPageResponse = yield call(
|
|
|
|
|
PageApi.fetchPage,
|
|
|
|
|
pageRequest,
|
|
|
|
|
);
|
|
|
|
|
if (fetchPageResponse.responseMeta.success) {
|
2019-09-16 08:08:03 +00:00
|
|
|
const normalizedResponse = CanvasWidgetsNormalizer.normalize(
|
2019-09-18 10:48:56 +00:00
|
|
|
extractCurrentDSL(fetchPageResponse),
|
2019-09-16 08:08:03 +00:00
|
|
|
);
|
2019-09-13 09:56:11 +00:00
|
|
|
const canvasWidgetsPayload: LoadCanvasWidgetsPayload = {
|
2019-08-26 12:41:21 +00:00
|
|
|
pageWidgetId: normalizedResponse.result,
|
2019-09-16 08:08:03 +00:00
|
|
|
widgets: normalizedResponse.entities.canvasWidgets,
|
2019-09-23 10:27:45 +00:00
|
|
|
layoutId: fetchPageResponse.data.layouts[0].id, // TODO(abhinav): Handle for multiple layouts
|
2019-09-16 08:08:03 +00:00
|
|
|
};
|
2019-09-16 10:37:38 +00:00
|
|
|
yield all([
|
2019-09-23 10:27:45 +00:00
|
|
|
put(updateCanvas(canvasWidgetsPayload)),
|
2019-09-16 10:37:38 +00:00
|
|
|
put({
|
|
|
|
|
type: ReduxActionTypes.LOAD_CANVAS_ACTIONS,
|
2019-09-23 10:27:45 +00:00
|
|
|
payload: fetchPageResponse.data.layouts[0].actions, // TODO(abhinav): Handle for multiple layouts
|
2019-09-16 10:37:38 +00:00
|
|
|
}),
|
|
|
|
|
]);
|
2019-03-30 12:30:42 +00:00
|
|
|
}
|
2019-09-23 10:27:45 +00:00
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
yield put(fetchPageError(error));
|
2019-03-30 12:30:42 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-19 22:25:37 +00:00
|
|
|
export function* savePageSaga(savePageAction: ReduxAction<SavePageRequest>) {
|
2019-09-17 15:09:55 +00:00
|
|
|
const savePageRequest = savePageAction.payload;
|
|
|
|
|
try {
|
|
|
|
|
const savePageResponse: SavePageResponse = yield call(
|
|
|
|
|
PageApi.savePage,
|
|
|
|
|
savePageRequest,
|
|
|
|
|
);
|
|
|
|
|
yield put(savePageSuccess(savePageResponse));
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
yield put(savePageError(err));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-19 22:25:37 +00:00
|
|
|
export function* updateLayoutSaga(
|
|
|
|
|
updateLayoutAction: ReduxAction<{
|
|
|
|
|
widgets: { [widgetId: string]: FlattenedWidgetProps };
|
|
|
|
|
}>,
|
|
|
|
|
) {
|
|
|
|
|
try {
|
|
|
|
|
const { widgets } = updateLayoutAction.payload;
|
2019-09-22 20:25:05 +00:00
|
|
|
|
2019-09-19 22:25:37 +00:00
|
|
|
const denormalizedDSL = CanvasWidgetsNormalizer.denormalize(
|
|
|
|
|
Object.keys(widgets)[0],
|
|
|
|
|
{ canvasWidgets: widgets },
|
|
|
|
|
);
|
2019-09-22 20:25:05 +00:00
|
|
|
const editorConfigs = yield select(getEditorConfigs) as any;
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionTypes.SAVE_PAGE_INIT,
|
|
|
|
|
payload: {
|
|
|
|
|
...editorConfigs,
|
|
|
|
|
dsl: denormalizedDSL,
|
|
|
|
|
},
|
|
|
|
|
});
|
2019-09-19 22:25:37 +00:00
|
|
|
} catch (err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-17 15:09:55 +00:00
|
|
|
export default function* pageSagas() {
|
|
|
|
|
yield all([
|
2019-09-19 22:25:37 +00:00
|
|
|
takeLatest(ReduxActionTypes.FETCH_PAGE, fetchPageSaga),
|
|
|
|
|
takeLatest(ReduxActionTypes.SAVE_PAGE_INIT, savePageSaga),
|
|
|
|
|
takeEvery(ReduxActionTypes.UPDATE_LAYOUT, updateLayoutSaga),
|
2019-09-17 15:09:55 +00:00
|
|
|
]);
|
2019-03-30 12:30:42 +00:00
|
|
|
}
|