improved xml selection performance

master
Malar Kannan 2017-07-29 14:37:28 +05:30
parent 44181748d6
commit a26d63881b
1 changed files with 45 additions and 27 deletions

View File

@ -6,19 +6,16 @@ import * as XML from 'xml2js';
import { xmlToEntries } from './LexAccessors'; import { xmlToEntries } from './LexAccessors';
import { LexEditor } from './LexEditor'; import { LexEditor } from './LexEditor';
export class LexSetup extends React.Component<any, any> { export class LexXMLSelect extends React.Component<any, any> {
xmlBuilder = new XML.Builder();
constructor(props: any) { constructor(props: any) {
super(props); super(props);
this.state = { this.state = {
xmlLoaded: false, dirty: false, xmlFiles: [], xmlFileName: 'new_es.xml' xmlFiles: [], xmlFileName: 'new_es.xml'
}; };
} }
public componentDidMount() { public componentDidMount() {
this.getXMLFiles(); this.getXMLFiles();
this.loadXML();
} }
render() { render() {
let files: string[] = [].concat(this.state.xmlFiles); let files: string[] = [].concat(this.state.xmlFiles);
if (files.indexOf(this.state.xmlFileName as string) === -1) { if (files.indexOf(this.state.xmlFileName as string) === -1) {
@ -27,15 +24,15 @@ export class LexSetup extends React.Component<any, any> {
let dropOptions = files.map((k: any, i: any, c: any) => { let dropOptions = files.map((k: any, i: any, c: any) => {
return { key: i, value: k, text: k }; return { key: i, value: k, text: k };
}); });
let selectFile = ( let onFileChange = (e: any, d: any) => {
this.setState({ xmlFileName: d.value as string });
this.props.onFileSelected(d.value);
};
return (
<Dropdown <Dropdown
options={dropOptions} options={dropOptions}
onChange={(e, d) => { onChange={onFileChange}
this.setState({ xmlFileName: d.value as string }); onAddItem={onFileChange}
}}
onAddItem={(e, d) => {
this.setState({ xmlFileName: d.value as string });
}}
value={this.state.xmlFileName} value={this.state.xmlFileName}
compact={true} compact={true}
selection={true} selection={true}
@ -47,6 +44,35 @@ export class LexSetup extends React.Component<any, any> {
}} }}
/> />
); );
}
private getXMLFiles() {
fetch('api/xmlfiles')
.then((response) => response.text())
.then((jsonStr) => {
let xmlFiles = JSON.parse(jsonStr);
this.setState({ xmlFiles });
})
.catch((e) => {
console.log('errored :', e);
});
}
}
export class LexSetup extends React.Component<any, any> {
xmlBuilder = new XML.Builder();
xmlFileName = 'new_es.xml';
constructor(props: any) {
super(props);
this.state = {
xmlLoaded: false, dirty: false, xmlFiles: [],
};
}
public componentDidMount() {
this.loadXML();
}
render() {
let saveButton = this.state.dirty ? let saveButton = this.state.dirty ?
( (
<Button floated="right" size="mini" onClick={() => { this.saveXML(); }}> <Button floated="right" size="mini" onClick={() => { this.saveXML(); }}>
@ -70,7 +96,11 @@ export class LexSetup extends React.Component<any, any> {
<Header color="teal" size="mini"> <Header color="teal" size="mini">
{saveButton} {saveButton}
{loadButton} {loadButton}
{selectFile} <LexXMLSelect
onFileSelected={(nf: string) => {
this.xmlFileName = nf;
}}
/>
<Icon name="edit" size="small" /> <Icon name="edit" size="small" />
<Header.Content> <Header.Content>
Freespeech Lexicon Editor Freespeech Lexicon Editor
@ -91,7 +121,7 @@ export class LexSetup extends React.Component<any, any> {
private loadXML() { private loadXML() {
this.setState({ xmlLoaded: false }); this.setState({ xmlLoaded: false });
fetch(this.state.xmlFileName) fetch(this.xmlFileName)
.then((response) => response.text()) .then((response) => response.text())
.then((xmlString) => { .then((xmlString) => {
XML.parseString(xmlString, (err, xmlData) => { XML.parseString(xmlString, (err, xmlData) => {
@ -106,23 +136,11 @@ export class LexSetup extends React.Component<any, any> {
}); });
} }
private getXMLFiles() {
fetch('api/xmlfiles')
.then((response) => response.text())
.then((jsonStr) => {
let xmlFiles = JSON.parse(jsonStr);
this.setState({ xmlFiles });
})
.catch((e) => {
console.log('errored :', e);
});
}
private saveXML() { private saveXML() {
let xmlText = this.xmlBuilder.buildObject(this.props.xmlData); let xmlText = this.xmlBuilder.buildObject(this.props.xmlData);
let data = new FormData(); let data = new FormData();
data.append('file', xmlText); data.append('file', xmlText);
data.append('name', this.state.xmlFileName); data.append('name', this.xmlFileName);
fetch('/api/save', { fetch('/api/save', {
method: 'POST', method: 'POST',
body: data body: data