2019-09-16 08:08:03 +00:00
|
|
|
import CanvasWidgetsNormalizer from "../normalizers/CanvasWidgetsNormalizer";
|
|
|
|
|
import {
|
|
|
|
|
ReduxActionTypes,
|
2019-09-27 16:05:33 +00:00
|
|
|
ReduxActionErrorTypes,
|
2019-09-16 08:08:03 +00:00
|
|
|
ReduxAction,
|
2019-09-24 12:36:03 +00:00
|
|
|
UpdateCanvasPayload,
|
2019-10-24 09:23:50 +00:00
|
|
|
LayoutPayload,
|
2019-09-16 08:08:03 +00:00
|
|
|
} from "../constants/ReduxActionConstants";
|
2019-09-27 16:05:33 +00:00
|
|
|
import { updateCanvas, savePageSuccess } from "../actions/pageActions";
|
2019-09-17 15:09:55 +00:00
|
|
|
import PageApi, {
|
|
|
|
|
FetchPageResponse,
|
|
|
|
|
SavePageResponse,
|
|
|
|
|
FetchPageRequest,
|
|
|
|
|
SavePageRequest,
|
2019-10-24 07:03:59 +00:00
|
|
|
FetchPublishedPageRequest,
|
|
|
|
|
FetchPublishedPageResponse,
|
2019-09-17 15:09:55 +00:00
|
|
|
} 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-27 16:05:33 +00:00
|
|
|
|
2019-09-24 12:36:03 +00:00
|
|
|
import { extractCurrentDSL } from "../utils/WidgetPropsUtils";
|
Property Pane Controls
- Fixes #121, #122, #123, #124, #90, #46, #65, #100, #101, #68, #102
2019-10-24 05:24:45 +00:00
|
|
|
import { getEditorConfigs, getWidgets } from "./selectors";
|
2019-09-27 16:05:33 +00:00
|
|
|
import { validateResponse } from "./ErrorSagas";
|
2019-03-30 12:30:42 +00:00
|
|
|
|
2019-09-19 22:25:37 +00:00
|
|
|
export function* fetchPageSaga(
|
|
|
|
|
pageRequestAction: ReduxAction<FetchPageRequest>,
|
|
|
|
|
) {
|
2019-08-26 12:41:21 +00:00
|
|
|
try {
|
2019-09-27 16:05:33 +00:00
|
|
|
const pageRequest = pageRequestAction.payload;
|
2019-09-18 10:48:56 +00:00
|
|
|
const fetchPageResponse: FetchPageResponse = yield call(
|
|
|
|
|
PageApi.fetchPage,
|
|
|
|
|
pageRequest,
|
|
|
|
|
);
|
2019-09-27 16:05:33 +00:00
|
|
|
const isValidResponse = yield validateResponse(fetchPageResponse);
|
|
|
|
|
if (isValidResponse) {
|
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-24 12:36:03 +00:00
|
|
|
const canvasWidgetsPayload: UpdateCanvasPayload = {
|
2019-08-26 12:41:21 +00:00
|
|
|
pageWidgetId: normalizedResponse.result,
|
2019-09-27 08:08:31 +00:00
|
|
|
currentPageName: fetchPageResponse.data.name,
|
|
|
|
|
currentPageId: fetchPageResponse.data.id,
|
2019-09-16 08:08:03 +00:00
|
|
|
widgets: normalizedResponse.entities.canvasWidgets,
|
2019-09-27 08:08:31 +00:00
|
|
|
currentLayoutId: fetchPageResponse.data.layouts[0].id, // TODO(abhinav): Handle for multiple layouts
|
2019-10-24 07:03:59 +00:00
|
|
|
currentApplicationId: fetchPageResponse.data.applicationId,
|
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-26 11:11:28 +00:00
|
|
|
payload: fetchPageResponse.data.actions,
|
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) {
|
2019-09-27 16:05:33 +00:00
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionErrorTypes.FETCH_PAGE_ERROR,
|
|
|
|
|
payload: {
|
|
|
|
|
error,
|
|
|
|
|
},
|
|
|
|
|
});
|
2019-03-30 12:30:42 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-24 07:03:59 +00:00
|
|
|
export function* fetchPublishedPageSaga(
|
|
|
|
|
pageRequestAction: ReduxAction<FetchPublishedPageRequest>,
|
|
|
|
|
) {
|
|
|
|
|
try {
|
2019-10-24 09:23:50 +00:00
|
|
|
const request: FetchPublishedPageRequest = pageRequestAction.payload;
|
|
|
|
|
const response: FetchPublishedPageResponse = yield call(
|
2019-10-24 07:03:59 +00:00
|
|
|
PageApi.fetchPublishedPage,
|
2019-10-24 09:23:50 +00:00
|
|
|
request,
|
2019-10-24 07:03:59 +00:00
|
|
|
);
|
2019-10-24 09:23:50 +00:00
|
|
|
const isValidResponse = yield validateResponse(response);
|
2019-10-24 07:03:59 +00:00
|
|
|
if (isValidResponse) {
|
2019-10-24 09:23:50 +00:00
|
|
|
const normalizedResponse = CanvasWidgetsNormalizer.normalize(
|
|
|
|
|
response.data.dsl,
|
|
|
|
|
);
|
|
|
|
|
const layoutPayload: LayoutPayload = {
|
|
|
|
|
widgets: normalizedResponse,
|
|
|
|
|
layoutId: response.data.id,
|
|
|
|
|
pageId: request.pageId,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionTypes.FETCH_PUBLISED_PAGE_SUCCESS,
|
|
|
|
|
payload: layoutPayload,
|
|
|
|
|
});
|
2019-10-24 07:03:59 +00:00
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionErrorTypes.FETCH_PUBLISHED_PAGE_ERROR,
|
|
|
|
|
payload: {
|
|
|
|
|
error,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
|
|
|
|
);
|
2019-09-27 16:05:33 +00:00
|
|
|
const isValidResponse = validateResponse(savePageResponse);
|
|
|
|
|
if (isValidResponse) {
|
|
|
|
|
yield put(savePageSuccess(savePageResponse));
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionErrorTypes.SAVE_PAGE_ERROR,
|
|
|
|
|
payload: {
|
|
|
|
|
error,
|
|
|
|
|
},
|
|
|
|
|
});
|
2019-09-17 15:09:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Property Pane Controls
- Fixes #121, #122, #123, #124, #90, #46, #65, #100, #101, #68, #102
2019-10-24 05:24:45 +00:00
|
|
|
function getLayoutSavePayload(
|
|
|
|
|
widgets: {
|
|
|
|
|
[widgetId: string]: FlattenedWidgetProps;
|
|
|
|
|
},
|
|
|
|
|
editorConfigs: any,
|
|
|
|
|
) {
|
|
|
|
|
const denormalizedDSL = CanvasWidgetsNormalizer.denormalize(
|
|
|
|
|
Object.keys(widgets)[0],
|
|
|
|
|
{ canvasWidgets: widgets },
|
|
|
|
|
);
|
|
|
|
|
return {
|
|
|
|
|
...editorConfigs,
|
|
|
|
|
dsl: denormalizedDSL,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-24 12:36:03 +00:00
|
|
|
export function* saveLayoutSaga(
|
2019-09-19 22:25:37 +00:00
|
|
|
updateLayoutAction: ReduxAction<{
|
|
|
|
|
widgets: { [widgetId: string]: FlattenedWidgetProps };
|
|
|
|
|
}>,
|
|
|
|
|
) {
|
|
|
|
|
try {
|
|
|
|
|
const { widgets } = updateLayoutAction.payload;
|
2019-09-22 20:25:05 +00:00
|
|
|
const editorConfigs = yield select(getEditorConfigs) as any;
|
Property Pane Controls
- Fixes #121, #122, #123, #124, #90, #46, #65, #100, #101, #68, #102
2019-10-24 05:24:45 +00:00
|
|
|
|
2019-09-22 20:25:05 +00:00
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionTypes.SAVE_PAGE_INIT,
|
Property Pane Controls
- Fixes #121, #122, #123, #124, #90, #46, #65, #100, #101, #68, #102
2019-10-24 05:24:45 +00:00
|
|
|
payload: getLayoutSavePayload(widgets, editorConfigs),
|
2019-09-22 20:25:05 +00:00
|
|
|
});
|
2019-09-19 22:25:37 +00:00
|
|
|
} catch (err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Property Pane Controls
- Fixes #121, #122, #123, #124, #90, #46, #65, #100, #101, #68, #102
2019-10-24 05:24:45 +00:00
|
|
|
// TODO(abhinav): This has redundant code. The only thing different here is the lack of state update.
|
|
|
|
|
// For now this is fire and forget.
|
|
|
|
|
export function* asyncSaveLayout() {
|
|
|
|
|
try {
|
|
|
|
|
const widgets = yield select(getWidgets);
|
|
|
|
|
const editorConfigs = yield select(getEditorConfigs) as any;
|
|
|
|
|
|
|
|
|
|
const request: SavePageRequest = getLayoutSavePayload(
|
|
|
|
|
widgets,
|
|
|
|
|
editorConfigs,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const savePageResponse: SavePageResponse = yield call(
|
|
|
|
|
PageApi.savePage,
|
|
|
|
|
request,
|
|
|
|
|
);
|
|
|
|
|
if (!validateResponse(savePageResponse)) {
|
|
|
|
|
throw Error("Error when saving layout");
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionErrorTypes.UPDATE_WIDGET_PROPERTY_ERROR,
|
|
|
|
|
payload: {
|
|
|
|
|
error,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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),
|
2019-10-24 07:03:59 +00:00
|
|
|
takeLatest(
|
|
|
|
|
ReduxActionTypes.FETCH_PUBLISHED_PAGE_INIT,
|
|
|
|
|
fetchPublishedPageSaga,
|
|
|
|
|
),
|
2019-09-19 22:25:37 +00:00
|
|
|
takeLatest(ReduxActionTypes.SAVE_PAGE_INIT, savePageSaga),
|
2019-09-24 12:36:03 +00:00
|
|
|
takeEvery(ReduxActionTypes.UPDATE_LAYOUT, saveLayoutSaga),
|
Property Pane Controls
- Fixes #121, #122, #123, #124, #90, #46, #65, #100, #101, #68, #102
2019-10-24 05:24:45 +00:00
|
|
|
// No need to save layout everytime a property is updated.
|
|
|
|
|
// We save the latest request to update layout.
|
|
|
|
|
takeLatest(ReduxActionTypes.UPDATE_WIDGET_PROPERTY, asyncSaveLayout),
|
2019-09-17 15:09:55 +00:00
|
|
|
]);
|
2019-03-30 12:30:42 +00:00
|
|
|
}
|