highlighting and discarding changes

master
Malar Kannan 2017-07-18 13:34:02 +05:30
parent 10576fbdc3
commit 9d9178fdcd
2 changed files with 54 additions and 20 deletions

View File

@ -24,18 +24,23 @@ export class LexEdit extends React.Component<any, any> {
let lexFields = _.keys(this.props.fieldMetaMap).map(field => { let lexFields = _.keys(this.props.fieldMetaMap).map(field => {
let lens = this.props.fieldMetaMap[field].lens; let lens = this.props.fieldMetaMap[field].lens;
let defaultText = _.get<any>(li, lens, ''); let defaultText = _.get<any>(li, lens, '');
let originalText = _.get<any>(this.props.lexItem, lens, '');
let options = this.props.fieldMetaMap[field].options; let options = this.props.fieldMetaMap[field].options;
let changed = defaultText !== originalText;
// console.log('changed:',changed);
let sh = (e: any) => { let sh = (e: any) => {
let eventData = {}; let eventData = {};
eventData[field] = e.target.value; eventData[field] = e.target.value;
this.handleOnChange(eventData); this.handleOnChange(eventData);
}; };
let params = { field, sh, defaultText, options, langSelOpts, changed };
if (this.props.fieldMetaMap[field].type === 'text') { if (this.props.fieldMetaMap[field].type === 'text') {
return textInput({field,sh,defaultText,options,langSelOpts}); return textInput(params);
} else if (this.props.fieldMetaMap[field].type === 'select') { } else if (this.props.fieldMetaMap[field].type === 'select') {
return selectInput({field,sh,defaultText,options,langSelOpts}); return selectInput(params);
} else if (this.props.fieldMetaMap[field].type === 'preview' && defaultText !== '') { } else if (this.props.fieldMetaMap[field].type === 'preview'
return imagePreview({field,sh,defaultText,options,langSelOpts}); && defaultText !== '') {
return imagePreview(params);
} else { } else {
return null; return null;
} }
@ -46,6 +51,16 @@ export class LexEdit extends React.Component<any, any> {
<Card.Content> <Card.Content>
<Card.Header> <Card.Header>
{_.get<any>(li, 'label', '')} {_.get<any>(li, 'label', '')}
<Button
floated="right"
icon="download"
onClick={(e, d) => this.handleOnLoad(d)}
/>
<Button
floated="right"
icon="undo"
onClick={(e, d) => this.handleOnUndo(d)}
/>
</Card.Header> </Card.Header>
<Card.Meta> <Card.Meta>
language: {_.get<any>(li, '$.id', '')} language: {_.get<any>(li, '$.id', '')}
@ -69,17 +84,25 @@ export class LexEdit extends React.Component<any, any> {
); );
} }
private handleOnUndo(event: any) {
let lexItem = this.props.lexItem;
this.setState({ lexItem });
}
private handleOnLoad(event: any) {
// this.props.load()
console.log('loading from server');
}
private handleOnChange(event: any) { private handleOnChange(event: any) {
let type = _.keys(event)[0]; let type = _.keys(event)[0];
let value = _.values(event)[0]; let value = _.values(event)[0];
let lens = this.props.fieldMetaMap[type].lens; let lens = this.props.fieldMetaMap[type].lens;
let { lexItem: lexItem } = this.state; let lexItem = _.cloneDeep(this.state.lexItem);
_.set(lexItem, lens, value); _.set(lexItem, lens, value);
this.setState(lexItem); this.setState({ lexItem });
} }
private handleOnSave(event: any) { private handleOnSave(event: any) {
this.props.save(this.state.lexItem); this.props.save(this.state.lexItem);
console.log('saving object :', this.state.lexItem);
} }
} }

View File

@ -14,11 +14,7 @@ class LexSingleInput extends React.Component<any, any> {
<Form> <Form>
<Form.Group as={Flex} inline={true} style={{ margin: '0 0 5px 5px' }}> <Form.Group as={Flex} inline={true} style={{ margin: '0 0 5px 5px' }}>
<Form.Field as={Box} w={2 / 5} style={{ textAlign: 'right' }}> <Form.Field as={Box} w={2 / 5} style={{ textAlign: 'right' }}>
<Label {this.props.label}
pointing="right"
>
{this.props.labelText}
</Label>
</Form.Field> </Form.Field>
<Form.Field as={Box} w={3 / 5} > <Form.Field as={Box} w={3 / 5} >
{this.props.children} {this.props.children}
@ -30,15 +26,30 @@ class LexSingleInput extends React.Component<any, any> {
} }
} }
function changedLabel(changed: boolean, text: string) {
let labelClass = changed ? 'olive' : '';
return (
<Label
pointing="right"
className={labelClass}
>
{text}
</Label>
);
}
const imageRoot = '/png/'; const imageRoot = '/png/';
export function textInput(params: any) { export function textInput(params: any) {
let {field,sh,defaultText} = params; let { field, sh, defaultText, changed } = params;
return ( return (
<LexSingleInput key={field} labelText={_.capitalize(field)}> <LexSingleInput
label={changedLabel(changed, field)}
key={field}
>
<Input <Input
onChange={sh} onChange={sh}
defaultValue={defaultText} value={defaultText}
placeholder={field} placeholder={field}
type="text" type="text"
dir="auto" dir="auto"
@ -49,16 +60,16 @@ export function textInput(params: any) {
} }
export function selectInput(params: any) { export function selectInput(params: any) {
let {field,sh,defaultText,options,langSelOpts} = params; let { field, sh, defaultText, options, langSelOpts, changed } = params;
let staticOpts = options; let staticOpts = options;
let fieldOpts = _.get<any>(langSelOpts, field, []); let fieldOpts = _.get<any>(langSelOpts, field, []);
let selOpts = staticOpts ? staticOpts : fieldOpts; let selOpts = staticOpts ? staticOpts : fieldOpts;
return ( return (
<LexSingleInput key={field} labelText={_.capitalize(field)}> <LexSingleInput key={field} label={changedLabel(changed, field)}>
<select <select
onChange={sh} onChange={sh}
style={{ width: '10em' }} style={{ width: '10em' }}
defaultValue={defaultText} value={defaultText}
> >
{selOpts.map((k: any, i: any, c: any) => { {selOpts.map((k: any, i: any, c: any) => {
return <option key={i} value={k}> {k}</option>; return <option key={i} value={k}> {k}</option>;
@ -69,10 +80,10 @@ export function selectInput(params: any) {
} }
export function imagePreview(params: any) { export function imagePreview(params: any) {
let {field,defaultText} = params; let { field, defaultText, changed } = params;
let imageSrc = imageRoot + defaultText; let imageSrc = imageRoot + defaultText;
return ( return (
<LexSingleInput key={field} labelText={_.capitalize(field)}> <LexSingleInput key={field} label={changedLabel(changed, field)}>
<Image src={imageSrc} size="tiny" bordered={true} /> <Image src={imageSrc} size="tiny" bordered={true} />
</LexSingleInput> </LexSingleInput>
); );