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

120 lines
3.5 KiB
TypeScript
Raw Normal View History

2017-06-27 09:33:34 +00:00
import * as React from 'react';
import * as _ from 'lodash';
import { componentForType } 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) {
2017-07-18 13:03:20 +00:00
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 = this.props.fieldMetaMap.lang.get(li);
2017-07-13 10:30:36 +00:00
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 fieldMeta = this.props.fieldMetaMap[field];
2017-07-20 13:43:40 +00:00
let value = fieldMeta.get(li);
let origValue = fieldMeta.get(this.props.lexItem);
let options = fieldMeta.options;
2017-07-20 13:43:40 +00:00
let changed = !_.isEqual(value, origValue) && this.props.existing;
2017-07-27 07:39:48 +00:00
let sh = (v: any) => {
this.handleOnChange({ field, value: v });
2017-06-27 09:33:34 +00:00
};
2017-07-20 13:43:40 +00:00
let params = { field, sh, value, options, langSelOpts, changed };
2017-07-27 07:39:48 +00:00
return componentForType(fieldMeta.type, params);
2017-06-27 09:33:34 +00:00
});
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)
let li = this.state.lexItem;
let word = this.props.fieldMetaMap.label.get(li);
let pos = this.props.fieldMetaMap.pos.get(li);
let args = serialize({ word, pos });
let morphQuery = '/api/morph?' + args;
fetch(morphQuery)
2017-07-18 13:03:20 +00:00
.then((response) => response.text())
.then((jsonMorph) => {
let morphDict = JSON.parse(jsonMorph);
let morphMeta = this.props.fieldMetaMap.morphclass;
let lexItem = morphMeta.set(this.state.lexItem, morphDict.morphclass);
this.setState({ lexItem });
2017-07-18 13:03:20 +00:00
})
.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-27 07:39:48 +00:00
let { field, value } = event;
let meta = this.props.fieldMetaMap[field];
let lexItem = meta.set(this.state.lexItem, value);
2017-07-18 08:04:02 +00:00
this.setState({ lexItem });
2017-06-27 09:33:34 +00:00
}
private handleOnSave(event: any) {
this.props.save(this.state.lexItem, this.props.fieldMetaMap);
2017-07-28 06:56:45 +00:00
this.props.saveXMLBackend();
2017-06-27 09:33:34 +00:00
}
}