mirror of
https://github.com/strapi/strapi.git
synced 2025-12-24 13:43:41 +00:00
Update React, refacto code and fix nmber type bug
This commit is contained in:
parent
58fd6c92b3
commit
a6d079cd89
@ -91,7 +91,7 @@
|
||||
"lodash": "^4.17.5",
|
||||
"moment": "^2.16.0",
|
||||
"prop-types": "^15.5.10",
|
||||
"react": "^16.5.2",
|
||||
"react": "^16.8.4",
|
||||
"react-copy-to-clipboard": "^5.0.1",
|
||||
"react-datetime": "^2.15.0",
|
||||
"react-dnd": "^5.0.0",
|
||||
|
||||
@ -48,6 +48,12 @@ export class App extends React.Component { // eslint-disable-line react/prefer-s
|
||||
this.props.getData();
|
||||
}
|
||||
|
||||
canOpenModalCreateContentType = () => {
|
||||
const { models } = this.props;
|
||||
|
||||
return models.every(model => (model.isTemporary === false));
|
||||
}
|
||||
|
||||
renderRoute = (route) => {
|
||||
const { component: Component, to } = route;
|
||||
|
||||
@ -55,9 +61,15 @@ export class App extends React.Component { // eslint-disable-line react/prefer-s
|
||||
return (
|
||||
<Route
|
||||
key={to}
|
||||
path={to}
|
||||
render={props => <Component {...this.props} {...props} />}
|
||||
exact
|
||||
path={to}
|
||||
render={props => (
|
||||
<Component
|
||||
{...this.props}
|
||||
{...props}
|
||||
canOpenModalAddContentType={this.canOpenModalCreateContentType()}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@ -85,6 +97,7 @@ App.propTypes = {
|
||||
deleteModel: PropTypes.func.isRequired,
|
||||
getData: PropTypes.func.isRequired,
|
||||
isLoading: PropTypes.bool.isRequired,
|
||||
models: PropTypes.array.isRequired,
|
||||
onChangeNewContentType: PropTypes.func.isRequired,
|
||||
};
|
||||
|
||||
|
||||
@ -43,13 +43,13 @@ function appReducer(state = initialState, action) {
|
||||
state.getIn(['temporaryAttribute', 'name']),
|
||||
], () => {
|
||||
const temporaryAttributeType = state.getIn(['temporaryAttribute', 'type']);
|
||||
const type = type === 'number' && !!temporaryAttributeType ? 'integer' : action.attributeType;
|
||||
const type = action.attributeType === 'number' ? temporaryAttributeType : action.attributeType;
|
||||
const newAttribute = state
|
||||
.get('temporaryAttribute')
|
||||
.remove('name')
|
||||
.setIn(['temporaryAttribute', 'type'], type);
|
||||
|
||||
return newAttribute;
|
||||
return newAttribute.get('temporaryAttribute');
|
||||
})
|
||||
.update('temporaryAttribute', () => Map({}));
|
||||
}
|
||||
|
||||
@ -16,7 +16,12 @@ describe('<App />', () => {
|
||||
getData: jest.fn(),
|
||||
initialData: {},
|
||||
isLoading: true,
|
||||
models: [],
|
||||
models: [
|
||||
{ icon: 'fa-cube', name: 'permission', description: '', fields: 6, source: 'users-permissions', isTemporary: false },
|
||||
{ icon: 'fa-cube', name: 'user', description: '', fields: 6, source: 'users-permissions', isTemporary: false },
|
||||
{ icon: 'fa-cube', name: 'role', description: '', fields: 6, source: 'users-permissions', isTemporary: false },
|
||||
{ icon: 'fa-cube', name: 'product', description: 'super api', fields: 6, isTemporary: false },
|
||||
],
|
||||
modifiedData: {},
|
||||
onChangeNewContentType: jest.fn(),
|
||||
};
|
||||
@ -50,6 +55,23 @@ describe('<App />', () => {
|
||||
expect(renderRoute(route)).not.toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('shouldcanOpenModalCreateContentTypeOpenModal', () => {
|
||||
it('should return true if the is no temporary model', () => {
|
||||
const wrapper = shallow(<App {...props} />);
|
||||
const { canOpenModalCreateContentType } = wrapper.instance();
|
||||
|
||||
expect(canOpenModalCreateContentType()).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should return false if there is more than 1 temporary model', () => {
|
||||
props.models[0].isTemporary = true;
|
||||
const wrapper = shallow(<App {...props} />);
|
||||
const { canOpenModalCreateContentType } = wrapper.instance();
|
||||
|
||||
expect(canOpenModalCreateContentType()).toBeFalsy();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@ -41,9 +41,9 @@ class HomePage extends React.Component { // eslint-disable-line react/prefer-sta
|
||||
}
|
||||
|
||||
handleClick = () => {
|
||||
const { history: { push } } = this.props;
|
||||
const {canOpenModalAddContentType, history: { push } } = this.props;
|
||||
|
||||
if (this.shouldOpenModalAdd()) {
|
||||
if (canOpenModalAddContentType) {
|
||||
push({
|
||||
search: 'modalType=model&settingType=base&actionType=create',
|
||||
});
|
||||
@ -56,12 +56,6 @@ class HomePage extends React.Component { // eslint-disable-line react/prefer-sta
|
||||
this.props.deleteModel(modelName);
|
||||
}
|
||||
|
||||
shouldOpenModalAdd = () => {
|
||||
const { models } = this.props;
|
||||
|
||||
return models.every(model => (model.isTemporary === false));
|
||||
}
|
||||
|
||||
render() {
|
||||
const {
|
||||
cancelNewContentType,
|
||||
@ -125,12 +119,14 @@ class HomePage extends React.Component { // eslint-disable-line react/prefer-sta
|
||||
}
|
||||
|
||||
HomePage.defaultProps = {
|
||||
canOpenModalAddContentType: true,
|
||||
models: [],
|
||||
modifiedData: {},
|
||||
};
|
||||
|
||||
HomePage.propTypes = {
|
||||
cancelNewContentType: PropTypes.func.isRequired,
|
||||
canOpenModalAddContentType: PropTypes.bool,
|
||||
createTempContentType: PropTypes.func.isRequired,
|
||||
deleteModel: PropTypes.func.isRequired,
|
||||
models: PropTypes.array,
|
||||
|
||||
@ -14,6 +14,7 @@ describe('CTB <HomePage />', () => {
|
||||
beforeEach(() => {
|
||||
props = {
|
||||
cancelNewContentType: jest.fn(),
|
||||
canOpenModalAddContentType: true,
|
||||
createTempContentType: jest.fn(),
|
||||
deleteModel: jest.fn(),
|
||||
models: [
|
||||
@ -107,7 +108,7 @@ describe('CTB <HomePage />', () => {
|
||||
|
||||
describe('handleClick', () => {
|
||||
it('should change the search if there is no temporary model', () => {
|
||||
props.models[0].isTemporary = false;
|
||||
props.canOpenModalAddContentType = true;
|
||||
|
||||
const wrapper = shallow(<HomePage {...props} />);
|
||||
const { handleClick } = wrapper.instance();
|
||||
@ -119,7 +120,7 @@ describe('CTB <HomePage />', () => {
|
||||
});
|
||||
|
||||
it('should display a notification if there is a temporary model', () => {
|
||||
props.models[0].isTemporary = true;
|
||||
props.canOpenModalAddContentType = false;
|
||||
|
||||
const wrapper = shallow(<HomePage {...props} />);
|
||||
const { handleClick } = wrapper.instance();
|
||||
@ -141,22 +142,5 @@ describe('CTB <HomePage />', () => {
|
||||
expect(props.deleteModel).toHaveBeenCalledWith('test');
|
||||
});
|
||||
});
|
||||
|
||||
describe('shouldOpenModal', () => {
|
||||
it('should return true if the is no temporary model', () => {
|
||||
const wrapper = shallow(<HomePage {...props} />);
|
||||
const { shouldOpenModalAdd } = wrapper.instance();
|
||||
|
||||
expect(shouldOpenModalAdd()).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should return false if there is more than 1 temporary model', () => {
|
||||
props.models[0].isTemporary = true;
|
||||
const wrapper = shallow(<HomePage {...props} />);
|
||||
const { shouldOpenModalAdd } = wrapper.instance();
|
||||
|
||||
expect(shouldOpenModalAdd()).toBeFalsy();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -112,6 +112,27 @@ export class ModelPage extends React.Component { // eslint-disable-line react/pr
|
||||
|
||||
getModelRelationShipsLength = () => Object.keys(this.getModelRelationShips()).length;
|
||||
|
||||
getPluginHeaderActions = () => {
|
||||
if (this.isUpdatingTemporaryContentType() && this.getModelAttributesLength() > 0) {
|
||||
return [
|
||||
{
|
||||
label: `${pluginId}.form.button.cancel`,
|
||||
onClick: () => {},
|
||||
kind: 'secondary',
|
||||
type: 'button',
|
||||
},
|
||||
{
|
||||
label: `${pluginId}.form.button.save`,
|
||||
onClick: () => {},
|
||||
kind: 'primary',
|
||||
type: 'submit',
|
||||
id: 'saveData',
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
getSectionTitle = () => {
|
||||
const base = `${pluginId}.menu.section.contentTypeBuilder.name.`;
|
||||
|
||||
@ -126,9 +147,9 @@ export class ModelPage extends React.Component { // eslint-disable-line react/pr
|
||||
}
|
||||
|
||||
handleClickOpenModalCreateCT = () => {
|
||||
const { history: { push } } = this.props;
|
||||
const { canOpenModalAddContentType, history: { push } } = this.props;
|
||||
|
||||
if (this.shouldOpenModalAddCT()) {
|
||||
if (canOpenModalAddContentType) {
|
||||
push({
|
||||
search: 'modalType=model&settingType=base&actionType=create',
|
||||
});
|
||||
@ -155,12 +176,6 @@ export class ModelPage extends React.Component { // eslint-disable-line react/pr
|
||||
return isTemporary;
|
||||
}
|
||||
|
||||
shouldOpenModalAddCT = () => {
|
||||
const { models } = this.props;
|
||||
|
||||
return models.every(model => (model.isTemporary === false));
|
||||
}
|
||||
|
||||
shouldRedirect = () => {
|
||||
const { models } = this.props;
|
||||
|
||||
@ -246,6 +261,7 @@ export class ModelPage extends React.Component { // eslint-disable-line react/pr
|
||||
description={this.getModelDescription()}
|
||||
icon="fa fa-pencil"
|
||||
title={this.getModelName()}
|
||||
actions={this.getPluginHeaderActions()}
|
||||
/>
|
||||
{this.getModelAttributesLength() === 0 ? (
|
||||
<EmptyAttributesBlock
|
||||
@ -330,10 +346,15 @@ export class ModelPage extends React.Component { // eslint-disable-line react/pr
|
||||
}
|
||||
}
|
||||
|
||||
ModelPage.defaultProps = {
|
||||
canOpenModalAddContentType: true,
|
||||
};
|
||||
|
||||
ModelPage.propTypes = {
|
||||
...routerPropTypes(
|
||||
{ params: PropTypes.string },
|
||||
).isRequired,
|
||||
canOpenModalAddContentType: PropTypes.bool,
|
||||
clearTemporaryAttribute: PropTypes.func.isRequired,
|
||||
initialData: PropTypes.object.isRequired,
|
||||
models: PropTypes.array.isRequired,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user