fs-walle-react-ts/src/LexEdit.tsx

133 lines
3.9 KiB
TypeScript
Raw Normal View History

2017-06-27 09:33:34 +00:00
import * as React from 'react';
import * as _ from 'lodash';
import {
textInput,
selectInput,
imagePreview
} from './LexSingleInput';
import {
2017-06-27 09:33:34 +00:00
Card,
Button
} from 'semantic-ui-react';
const { Box } = require('reflexbox');
2017-07-18 13:03:20 +00:00
const serialize = function(obj: any) {
var str = [];
for(var p in obj){
if (obj.hasOwnProperty(p)) {
str.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p]));
}
}
return str.join('&');
};
2017-06-27 09:33:34 +00:00
export class LexEdit extends React.Component<any, any> {
2017-07-13 10:30:36 +00:00
imageRoot = '/png/';
constructor(props: any) {
super(props);
this.state = { lexItem: this.props.lexItem };
}
2017-06-27 09:33:34 +00:00
public render() {
2017-07-13 10:30:36 +00:00
let li = this.state.lexItem;
let editLang = _.get<any>(li, this.props.fieldMetaMap.lang.lens, 'en');
let langSelOpts = _.get<any>(this.props.selectionMeta, editLang, []);
2017-07-12 06:02:01 +00:00
let lexFields = _.keys(this.props.fieldMetaMap).map(field => {
let lens = this.props.fieldMetaMap[field].lens;
let defaultText = _.get<any>(li, lens, '');
2017-07-18 08:04:02 +00:00
let originalText = _.get<any>(this.props.lexItem, lens, '');
let options = this.props.fieldMetaMap[field].options;
2017-07-18 10:24:19 +00:00
let changed = defaultText !== originalText && this.props.existing;
2017-07-18 08:04:02 +00:00
// console.log('changed:',changed);
2017-06-27 09:33:34 +00:00
let sh = (e: any) => {
let eventData = {};
eventData[field] = e.target.value;
this.handleOnChange(eventData);
};
2017-07-18 08:04:02 +00:00
let params = { field, sh, defaultText, options, langSelOpts, changed };
2017-07-12 06:02:01 +00:00
if (this.props.fieldMetaMap[field].type === 'text') {
2017-07-18 08:04:02 +00:00
return textInput(params);
2017-07-12 06:02:01 +00:00
} else if (this.props.fieldMetaMap[field].type === 'select') {
2017-07-18 08:04:02 +00:00
return selectInput(params);
} else if (this.props.fieldMetaMap[field].type === 'preview'
&& defaultText !== '') {
return imagePreview(params);
2017-06-27 09:33:34 +00:00
} else {
return null;
}
});
return (
<Box m={2}>
<Card raised={true}>
<Card.Content>
<Card.Header>
{_.get<any>(li, 'label', '')}
2017-07-18 08:04:02 +00:00
<Button
floated="right"
icon="download"
2017-07-18 10:24:19 +00:00
size="tiny"
2017-07-18 08:04:02 +00:00
onClick={(e, d) => this.handleOnLoad(d)}
/>
<Button
floated="right"
icon="undo"
2017-07-18 10:24:19 +00:00
size="tiny"
2017-07-18 08:04:02 +00:00
onClick={(e, d) => this.handleOnUndo(d)}
/>
2017-06-27 09:33:34 +00:00
</Card.Header>
<Card.Meta>
language: {_.get<any>(li, '$.id', '')}
</Card.Meta>
<Card.Description>
{lexFields}
</Card.Description>
</Card.Content>
<Card.Content extra={true}>
<Button
basic={true}
fluid={true}
color="green"
onClick={(e, d) => this.handleOnSave(d)}
>
Save
</Button>
</Card.Content>
</Card>
</Box>
);
}
2017-07-18 08:04:02 +00:00
private handleOnUndo(event: any) {
let lexItem = this.props.lexItem;
this.setState({ lexItem });
}
2017-07-18 13:03:20 +00:00
2017-07-18 08:04:02 +00:00
private handleOnLoad(event: any) {
2017-07-18 10:24:19 +00:00
// this.props.load(this.state.lexItem)
2017-07-18 13:03:20 +00:00
let lexItem = this.props.lexItem;
let word = _.get<any>(lexItem, this.props.fieldMetaMap.label.lens, '');
let pos = _.get<any>(lexItem, this.props.fieldMetaMap.pos.lens, '');
let args = serialize({word,pos});
fetch('/morph?'+args)
.then((response) => response.text())
.then((jsonMorph) => {
console.log(jsonMorph);
})
.catch((e) => {
console.log('errored :', e);
});
2017-07-18 08:04:02 +00:00
}
2017-06-27 09:33:34 +00:00
private handleOnChange(event: any) {
2017-07-13 10:30:36 +00:00
let type = _.keys(event)[0];
let value = _.values(event)[0];
let lens = this.props.fieldMetaMap[type].lens;
2017-07-18 08:04:02 +00:00
let lexItem = _.cloneDeep(this.state.lexItem);
2017-07-13 10:30:36 +00:00
_.set(lexItem, lens, value);
2017-07-18 08:04:02 +00:00
this.setState({ lexItem });
2017-06-27 09:33:34 +00:00
}
private handleOnSave(event: any) {
2017-07-13 13:36:47 +00:00
this.props.save(this.state.lexItem);
2017-06-27 09:33:34 +00:00
}
}