Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

formik/ENG-5251 actions and selectors #1567

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { initialize } from 'redux-form';
import { addToast, addErrors, TOAST_SUCCESS, TOAST_ERROR } from '@entando/messages';
import { toggleLoading } from 'state/loading/actions';

Expand Down Expand Up @@ -59,15 +58,12 @@ export const fetchComponentRepositories = (page = { page: 1, pageSize: 10 }, par
})
);

export const fetchComponentRepository = (id, initForm = false) => dispatch => (
export const fetchComponentRepository = id => dispatch => (
new Promise((resolve) => {
getComponentRepository(id).then((response) => {
response.json().then((data) => {
if (response.ok) {
dispatch(setSelectedComponentRepository(data.payload));
if (initForm) {
dispatch(initialize('ecrSettings', data.payload));
}
} else {
dispatch(addErrors(data.errors.map(err => err.message)));
data.errors.forEach(err => dispatch(addToast(err.message, TOAST_ERROR)));
Expand Down
2 changes: 0 additions & 2 deletions src/state/content-template/actions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { convertToQueryString, FILTER_OPERATORS } from '@entando/utils';
import { addErrors, addToast, clearErrors, TOAST_ERROR } from '@entando/messages';
import { initialize } from 'redux-form';
import { setPage } from 'state/pagination/actions';
import { NAMESPACE_CONTENT_TEMPLATES } from 'state/pagination/const';
import {
Expand Down Expand Up @@ -144,7 +143,6 @@ export const fetchContentTemplate = id => dispatch => new Promise(resolve => (
response.json().then((json) => {
if (response.ok) {
dispatch(setContentTemplate(json.payload));
dispatch(initialize('contenttemplateform', json.payload));
resolve(json.payload);
} else {
dispatch(addErrors(json.errors.map(err => err.message)));
Expand Down
45 changes: 2 additions & 43 deletions src/state/content-type/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import {
getActionModeContentTypeSelectedAttribute,
getContentTypeAttributesIdList,
getMonolistAttributeType,
getFormTypeValue,
getSelectedAttributeType,
getContentTypeSelectedAttributeType,
getContentTypeSelectedAttribute,
Expand All @@ -46,7 +45,6 @@ import {
getNewAttributeComposite,
getSelectedContentType,
} from 'state/content-type/selectors';
import { initialize } from 'redux-form';

import {
TYPE_MONOLIST,
Expand Down Expand Up @@ -231,18 +229,12 @@ export const fetchContentTypeListPaged = (
.catch(() => {});
});

export const fetchContentType = (
contentTypeCode,
initForm = true,
) => dispatch => new Promise((resolve, reject) => {
export const fetchContentType = contentTypeCode => dispatch => new Promise((resolve, reject) => {
getContentType(contentTypeCode)
.then((response) => {
response.json().then((json) => {
if (response.ok) {
dispatch(setSelectedContentType(json.payload));
if (initForm) {
dispatch(initialize('ContentType', json.payload));
}
resolve(json.payload);
} else {
dispatch(addErrors(json.errors.map(err => err.message)));
Expand Down Expand Up @@ -380,16 +372,14 @@ export const fetchContentTypeAttributeRef = (
contentTypeAttributeCode,
routeFunc,
selectedAttributeType = '',
formName,
) => (dispatch, getState) => new Promise((resolve) => {
let typeAttribute = contentTypeAttributeCode;
const typeAttribute = contentTypeAttributeCode;

const checkCompositeSubAttribute = selectedAttributeType === TYPE_COMPOSITE
|| (selectedAttributeType === TYPE_MONOLIST
&& getMonolistAttributeType(getState()) === TYPE_COMPOSITE);

if (checkCompositeSubAttribute) {
typeAttribute = getFormTypeValue(getState(), formName);
dispatch(setActionMode(MODE_ADD_ATTRIBUTE_COMPOSITE));
const selectedAttr = getContentTypeSelectedAttribute(getState());
dispatch(pushParentSelectedAttribute(selectedAttr));
Expand All @@ -403,32 +393,6 @@ export const fetchContentTypeAttributeRef = (
response.json().then((json) => {
if (response.ok) {
dispatch(setSelectedAttributeRef(json.payload));
switch (actionMode) {
case MODE_ADD_ATTRIBUTE_COMPOSITE: {
dispatch(initialize(formName, {
type: json.payload.code,
compositeAttributeType: TYPE_COMPOSITE,
code: '',
name: '',
}));
break;
}
case MODE_ADD_SUB_ATTRIBUTE_MONOLIST_COMPOSITE: {
dispatch(initialize(formName, { type: json.payload.code }));
break;
}
case MODE_ADD_MONOLIST_ATTRIBUTE_COMPOSITE: {
const nestedAttribute = {
...json.payload,
type: json.payload.code,
compositeAttributeType: TYPE_COMPOSITE,
};
dispatch(initialize(formName, { nestedAttribute }));
break;
}
default:
break;
}
if (routeFunc && actionMode !== MODE_ADD_ATTRIBUTE_COMPOSITE) {
routeFunc();
}
Expand Down Expand Up @@ -517,7 +481,6 @@ export const fetchAttributeFromContentType = (formName, contentTypeCode, attribu
}
const actionMode = getActionModeContentTypeSelectedAttribute(getState());
if (actionMode !== MODE_ADD_ATTRIBUTE_COMPOSITE) {
dispatch(initialize(formName, payload));
dispatch(setSelectedContentTypeAttribute(json.payload));
dispatch(fetchContentTypeAttributeRef(
contentTypeCode,
Expand Down Expand Up @@ -585,10 +548,6 @@ export const sendPutAttributeFromContentType = (
type === TYPE_MONOLIST
&& getIsMonolistCompositeAttributeType(getState())
)) {
dispatch(initialize('attribute', {
...json.payload,
compositeAttributeType: TYPE_COMPOSITE,
}));
if (mode === MODE_ADD_ATTRIBUTE_COMPOSITE
|| mode === MODE_ADD_MONOLIST_ATTRIBUTE_COMPOSITE) {
history.push(routeConverter(ROUTE_CMS_CONTENT_TYPE_ATTRIBUTE_EDIT, {
Expand Down
2 changes: 0 additions & 2 deletions src/state/content-type/selectors.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createSelector } from 'reselect';
import { formValueSelector } from 'redux-form';
import {
get,
isEmpty,
Expand Down Expand Up @@ -252,7 +251,6 @@ export const getNewAttributeComposite = createSelector(
sel => sel.newAttributeComposite,
);

export const getFormTypeValue = (state, formName) => formValueSelector(formName)(state, 'type');

export const getShapeMethodsByAttributeType = (type) => {
switch (type) {
Expand Down
5 changes: 1 addition & 4 deletions src/state/data-models/actions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { initialize } from 'redux-form';
import { addToast, addErrors, TOAST_SUCCESS, TOAST_ERROR } from '@entando/messages';

import { getDataModels, getDataModel, postDataModel, putDataModel, deleteDataModel } from 'api/dataModels';
Expand Down Expand Up @@ -38,9 +37,7 @@ export const fetchDataModelListPaged = (page = { page: 1, pageSize: 10 }, params
export const fetchDataModel = dataModelId => dispatch => new Promise((resolve) => {
getDataModel(dataModelId).then((response) => {
response.json().then((json) => {
if (response.ok) {
dispatch(initialize('dataModel', json.payload));
} else {
if (!response.ok) {
dispatch(addErrors(json.errors.map(err => err.message)));
json.errors.forEach(err => dispatch(addToast(err.message, TOAST_ERROR)));
}
Expand Down
70 changes: 3 additions & 67 deletions src/state/data-types/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import { addToast, addErrors, clearErrors, TOAST_SUCCESS, TOAST_ERROR } from '@e
import { routeConverter } from '@entando/utils';
import { setPage } from 'state/pagination/actions';
import { toggleLoading } from 'state/loading/actions';
import { initialize } from 'redux-form';

import { isUndefined } from 'lodash';
import moment from 'moment';

import {
history,
Expand Down Expand Up @@ -54,7 +53,6 @@ import {
getDataTypeSelectedAttributeType,
getSelectedDataType,
getSelectedAttributeType,
getFormTypeValue,
getAttributeSelectFromDataType,
getActionModeDataTypeSelectedAttribute,
getNewAttributeComposite,
Expand Down Expand Up @@ -288,7 +286,6 @@ export const fetchDataType = dataTypeCode => dispatch => (
response.json().then((json) => {
if (response.ok) {
dispatch(setSelectedDataType(json.payload));
dispatch(initialize('DataType', json.payload));
} else {
dispatch(addErrors(json.errors.map(err => err.message)));
json.errors.forEach(err => dispatch(addToast(err.message, TOAST_ERROR)));
Expand Down Expand Up @@ -319,18 +316,17 @@ export const fetchDataTypes = (page = { page: 1, pageSize: 10 }, params = '') =>
);

export const fetchDataTypeAttribute =
(dataTypeAttributeCode, route, selectedAttributeType = '', formName) =>
(dataTypeAttributeCode, route, selectedAttributeType = '') =>
(dispatch, getState) => (
new Promise((resolve) => {
let typeAttribute = dataTypeAttributeCode;
const typeAttribute = dataTypeAttributeCode;

const checkCompositeSubAttribute =
selectedAttributeType === TYPE_COMPOSITE ||
(selectedAttributeType === TYPE_MONOLIST &&
getMonolistAttributeType(getState()) === TYPE_COMPOSITE);

if (checkCompositeSubAttribute) {
typeAttribute = getFormTypeValue(getState(), formName);
dispatch(setActionMode(MODE_ADD_ATTRIBUTE_COMPOSITE));
}
const actionMode = getActionModeDataTypeSelectedAttribute(getState());
Expand All @@ -341,26 +337,6 @@ export const fetchDataTypeAttribute =
response.json().then((json) => {
if (response.ok) {
dispatch(setSelectedAttribute(json.payload));
switch (actionMode) {
case MODE_ADD_ATTRIBUTE_COMPOSITE: {
dispatch(initialize(formName, { type: json.payload.code, code: '', name: '' }));
break;
}
case MODE_ADD_SUB_ATTRIBUTE_MONOLIST_COMPOSITE: {
dispatch(initialize(formName, { type: json.payload.code }));
break;
}
case MODE_ADD_MONOLIST_ATTRIBUTE_COMPOSITE: {
const nestedAttribute = {
...json.payload,
type: json.payload.code,
compositeAttributeType: TYPE_COMPOSITE,
};
dispatch(initialize(formName, { nestedAttribute }));
break;
}
default: break;
}
if (route && actionMode !== MODE_ADD_ATTRIBUTE_COMPOSITE) {
history.push(routeConverter(route.route, route.params));
}
Expand All @@ -374,53 +350,14 @@ export const fetchDataTypeAttribute =
})
);

const fmtDateDDMMYYY = (date) => {
let d = new Date(date);
d = `${d.getDate()}/${d.getMonth() + 1}/${d.getFullYear()}`;
return moment(d, 'DD/MM/YYYY').format('DD/MM/YYYY');
};

export const fetchAttributeFromDataType = (formName, dataTypeCode, attributeCode) =>
(dispatch, getState) => (
new Promise((resolve) => {
getAttributeFromDataType(dataTypeCode, attributeCode).then((response) => {
response.json().then((json) => {
if (response.ok) {
const joinRoles = json.payload.roles ? json.payload.roles.map(role => (role.code)) : [];
let payload = {
...json.payload,
joinRoles,
joinAllowedOptions: joinRoles,
compositeAttributeType: TYPE_COMPOSITE,
};
if (json.payload.type === TYPE_DATE) {
let {
rangeStartDate, rangeEndDate, equalDate,
rangeStartDateAttribute, rangeEndDateAttribute, equalDateAttribute,
} = json.payload.validationRules;
rangeStartDate = rangeStartDate && fmtDateDDMMYYY(rangeStartDate);
rangeEndDate = rangeEndDate && fmtDateDDMMYYY(rangeEndDate);
equalDate = equalDate && fmtDateDDMMYYY(equalDate);
rangeStartDateAttribute =
rangeStartDateAttribute && fmtDateDDMMYYY(rangeStartDateAttribute);
rangeEndDateAttribute =
rangeEndDateAttribute && fmtDateDDMMYYY(rangeEndDateAttribute);
equalDateAttribute = equalDateAttribute && fmtDateDDMMYYY(equalDateAttribute);
payload = {
...payload,
validationRules: {
rangeStartDate,
rangeEndDate,
equalDate,
rangeStartDateAttribute,
rangeEndDateAttribute,
equalDateAttribute,
},
};
}
const actionMode = getActionModeDataTypeSelectedAttribute(getState());
if (actionMode !== MODE_ADD_ATTRIBUTE_COMPOSITE) {
dispatch(initialize(formName, payload));
dispatch(setSelectedAttributeDataType(json.payload));
dispatch(fetchDataTypeAttribute(getSelectedAttributeType(getState())));
}
Expand Down Expand Up @@ -484,7 +421,6 @@ export const sendPutAttributeFromDataType = (
dispatch(setSelectedAttributeDataType(json.payload));
const { type, code } = attributeObject;
if (type === TYPE_COMPOSITE) {
dispatch(initialize('attribute', { ...json.payload, compositeAttributeType: TYPE_COMPOSITE }));
history.push(routeConverter(
ROUTE_DATA_TYPE_ATTRIBUTE_EDIT,
{ entityCode, attributeCode: code },
Expand Down
2 changes: 0 additions & 2 deletions src/state/data-types/selectors.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createSelector } from 'reselect';
import { formValueSelector } from 'redux-form';
import { get, isEmpty, isUndefined } from 'lodash';
import {
TYPE_MONOLIST,
Expand Down Expand Up @@ -115,4 +114,3 @@ export const getIsMonolistComposteAttributeType =
export const getNewAttributeComposite =
createSelector([getSelectedDataType], sel => sel.newAttributeComposite);

export const getFormTypeValue = (state, formName) => formValueSelector(formName)(state, 'type');
25 changes: 1 addition & 24 deletions src/state/edit-content/actions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { addErrors, addToast, clearErrors, TOAST_ERROR } from '@entando/messages';
import { initialize, formValueSelector, change } from 'redux-form';
import moment from 'moment';
import { isEmpty, pickBy, isObject, cloneDeep } from 'lodash';

Expand Down Expand Up @@ -60,15 +59,6 @@ export const fetchContent = params => dispatch => new Promise((resolve, reject)
const content = json.payload;
dispatch(setContentEntry(content));
dispatch(setJoinedCategories(content.categories));
const {
mainGroup, groups, description, status,
} = content;
dispatch(initialize('editcontentform', {
mainGroup,
groups,
description,
...(status !== 'PUBLIC' && { status }),
}));
} else {
dispatch(addErrors(json.errors.map(err => err.message)));
reject();
Expand Down Expand Up @@ -105,9 +95,7 @@ export const copyAttributeEngValue = (attribute, attributeType) => (dispatch, ge
}
};

export const duplicateEngFieldValues = () => (dispatch, getState) => {
const state = getState();

export const duplicateEngFieldValues = () => (dispatch) => {
const traverseAttributes = (attributeValues, attributeList) => {
const mappedAttributes = attributeList.reduce(
(curr, attribute) => ({ ...curr, [attribute.code]: attribute }),
Expand Down Expand Up @@ -143,17 +131,6 @@ export const duplicateEngFieldValues = () => (dispatch, getState) => {
}
});
};

const mainAttributeValues = formValueSelector('editcontentform')(state, 'attributes');

dispatch(change(
'editcontentform',
'attributes',
traverseAttributes(
mainAttributeValues,
getSelectedContentTypeAttributes(state),
),
));
};

export const setOwnerGroupDisable = disabled => ({
Expand Down
5 changes: 1 addition & 4 deletions src/state/file-browser/actions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { addToast, addErrors, TOAST_SUCCESS, TOAST_ERROR } from '@entando/messages';
import { initialize } from 'redux-form';

import { getFileBrowser, getFile, postFile, putFile, postCreateFolder, deleteFolder, deleteFile } from 'api/fileBrowser';
import { toggleLoading } from 'state/loading/actions';
Expand Down Expand Up @@ -54,9 +53,7 @@ export const fetchFile = (filename, extensions = ['.txt']) => (dispatch, getStat
const queryString = `?protectedFolder=${protectedFolder === null ? false : protectedFolder}&currentPath=${currentPath}/${filename}`;
getFile(queryString).then((response) => {
response.json().then((json) => {
if (response.ok) {
dispatch(initialize('CreateTextFileForm', { content: window.atob(json.payload.base64) }));
} else {
if (!response.ok) {
dispatch(addErrors(json.errors.map(e => e.message)));
json.errors.forEach(err => dispatch(addToast(err.message, TOAST_ERROR)));
history.push(ROUTE_FILE_BROWSER);
Expand Down
5 changes: 0 additions & 5 deletions src/state/forms/selectors.js

This file was deleted.

Loading
Loading