Skip to content

Commit 61d9709

Browse files
committed
Lite refactoring
1 parent baa8d22 commit 61d9709

File tree

3 files changed

+30
-31
lines changed

3 files changed

+30
-31
lines changed

src/components/catalog/CatalogForm.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ const CatalogForm = ({ inputList, formObject, collections,
1212
{inputList.map((input, index) => {
1313
return (
1414
<GenericInput key={index}
15-
type={input.type}
15+
datatype={input.type}
1616
category={input.category}
1717
collectionType={input.collection}
1818
collection={collections[input.type]}
1919
name={input.name}
2020
value={formObject[input.name]}
2121
label={input.displayName}
22-
permissions={permissions}
22+
readOnly={!permissions.includes('U')}
2323
onChange={onChange}/>
2424
);
2525
})}

src/components/catalog/ManageCatalogPage.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ export class ManageCatalogPage extends React.Component {
7070
};
7171

7272
updateCatalogState = (field, value) => {
73-
console.log(field, value);
7473
let form = this.state.form;
7574
form[field] = value;
7675
return this.setState({ form, isTouched: true });

src/components/common/GenericInput.js

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import moment from 'moment';
55

66

77
const GenericInput = ({ category, collection = [], collectionType,
8-
type, name, label, onChange, placeholder,
9-
value, error, permissions }) => {
8+
datatype, name, label, onChange, placeholder,
9+
value, error, readOnly }) => {
1010

1111
function onChangeDate(name) {
1212
return function(date, dateString) {
@@ -36,7 +36,7 @@ const GenericInput = ({ category, collection = [], collectionType,
3636

3737
function onChangeSelect(name) {
3838
return function(value) {
39-
onChange(name, { _id: value, _class: type });
39+
onChange(name, { _id: value, _class: datatype });
4040
};
4141
}
4242

@@ -62,55 +62,58 @@ const GenericInput = ({ category, collection = [], collectionType,
6262
const dateFormat = 'YYYY-MM-DD';
6363
const datetimeFormat = 'YYYY-MM-DD HH:mm:ss';
6464

65-
switch (type) {
65+
switch (datatype) {
6666
case '%Library.String':
67-
if (permissions.includes('U')) {
68-
input = <Input value={value} onChange={onChangeString(name)} name={name}/>;
67+
if (readOnly) {
68+
input = <Input value={value} readOnly name={name}/>;
6969
}
7070
else {
71-
input = <Input value={value} readOnly name={name}/>;
71+
input = <Input value={value} onChange={onChangeString(name)} name={name}/>;
7272
}
7373
break;
7474
case '%Library.Boolean':
75-
if (permissions.includes('U')) {
76-
input = <Checkbox checked={value} onChange={onChangeBoolean(name)} name={name}>{label}</Checkbox>;
75+
if (readOnly) {
76+
input = <Checkbox checked={value} readOnly name={name}>{label}</Checkbox>;
7777
}
7878
else {
79-
input = <Checkbox checked={value} readOnly name={name}>{label}</Checkbox>;
79+
input = <Checkbox checked={value} onChange={onChangeBoolean(name)} name={name}>{label}</Checkbox>;
8080
}
8181
break;
8282
case '%Library.Date':
83-
if (permissions.includes('U')) {
84-
input = (<DatePicker format={dateFormat} value={getMomentValue(value, dateFormat)}
85-
name={name} onChange={onChangeDate(name)}/>);
83+
if (readOnly) {
84+
input = <Input value={value} name={name} readOnly style={{ width: '200px' }}/>;
8685
}
8786
else {
88-
input = <Input value={value} name={name} readOnly style={{ width: '200px' }}/>;
87+
input = (<DatePicker format={dateFormat} value={getMomentValue(value, dateFormat)}
88+
name={name} onChange={onChangeDate(name)}/>);
8989
}
9090
break;
9191
case '%Library.TimeStamp':
92-
if (permissions.includes('U')) {
93-
input = (<DatePicker format={datetimeFormat} value={getMomentValue(value, moment.ISO_8601)}
94-
showTime name={name} onChange={onChangeTimestamp(name)}/>);
92+
if (readOnly) {
93+
input = <Input value={getMomentValue(value, moment.ISO_8601)} name={name} readOnly style={{ width: '200px' }}/>;
9594
}
9695
else {
97-
input = <Input value={getMomentValue(value, moment.ISO_8601)} name={name} readOnly style={{ width: '200px' }}/>;
96+
input = (<DatePicker format={datetimeFormat} value={getMomentValue(value, moment.ISO_8601)}
97+
showTime name={name} onChange={onChangeTimestamp(name)}/>);
9898
}
9999
break;
100100
case '%Library.Integer':
101101
case '%Library.Numeric':
102-
if (permissions.includes('U')) {
103-
input = <InputNumber name={name} value={value} onChange={onChangeNumber(name)}/>;
102+
if (readOnly) {
103+
input = <Input value={value} name={name} readOnly style={{ width: '200px' }}/>;
104104
}
105105
else {
106-
input = <Input value={value} name={name} readOnly style={{ width: '200px' }}/>;
106+
input = <InputNumber name={name} value={value} onChange={onChangeNumber(name)}/>;
107107
}
108108
break;
109109
default:
110110
if (category === 'form') {
111111

112112
if (!collectionType) {
113-
if (permissions.includes('U')) {
113+
if (readOnly) {
114+
input = <Input name={name} value={value.name} readOnly style={{ width: '200px' }}/>;
115+
}
116+
else {
114117
let options = collection.map(item => {
115118
return <Select.Option key={item._id} value={item._id}>{item.displayName}</Select.Option>;
116119
});
@@ -131,17 +134,14 @@ const GenericInput = ({ category, collection = [], collectionType,
131134
</Select>
132135
);
133136
}
134-
else {
135-
input = <Input name={name} value={value.name} readOnly style={{ width: '200px' }}/>;
136-
}
137137
}
138138

139139
}
140140
break;
141141
}
142142

143143
if (input) {
144-
if (type !== '%Library.Boolean') {
144+
if (datatype !== '%Library.Boolean') {
145145
return (
146146
<Form.Item label={label}>
147147
{input}
@@ -157,15 +157,15 @@ const GenericInput = ({ category, collection = [], collectionType,
157157
};
158158

159159
GenericInput.propTypes = {
160-
type: PropTypes.string.isRequired,
160+
datatype: PropTypes.string.isRequired,
161161
category: PropTypes.string.isRequired,
162162
collectionType: PropTypes.string.isRequired,
163163
collection: PropTypes.array,
164164
name: PropTypes.string.isRequired,
165165
label: PropTypes.string.isRequired,
166166
onChange: PropTypes.func.isRequired,
167167
placeholder: PropTypes.string,
168-
permissions: PropTypes.string,
168+
readOnly: PropTypes.bool,
169169
value: PropTypes.any,
170170
error: PropTypes.string
171171
};

0 commit comments

Comments
 (0)