import * as React from 'react'; import {set, cloneDeep} from 'lodash'; import {IVocabularyItem} from 'superdesk-api'; import {ILocation} from '../../interfaces'; import {superdeskApi} from '../../superdeskApi'; import {formatLocationToAddress} from '../../utils/locations'; import {ButtonList} from '../UI'; import {Field, TextInput, SelectInputWithFreeText, TextAreaInput} from '../UI/Form'; import {Popup, Content, Footer, Header} from '../UI/Popup'; import './style.scss'; interface IProps { initialName?: string; target: string; regions: Array; countries: Array; defaultCountry?: IVocabularyItem['name']; initialAddressIsName?: boolean; onCancel(): void; onSave(location: Partial): void; popupContainer?(): HTMLElement; onPopupOpen?(): void; onPopupClose?(): void; } interface IState { formInvalid: boolean; item: { location: Partial; state?: IVocabularyItem | string; country?: IVocabularyItem | string; }; } export class CreateNewGeoLookup extends React.Component { dom: { name: React.RefObject; }; constructor(props) { super(props); this.state = { formInvalid: true, item: { location: { name: this.props.initialName, address: { line: [ this.props.initialAddressIsName ? this.props.initialName : '' ], country: this.props.defaultCountry ?? '', }, }, }, }; this.dom = {name: React.createRef()}; this.onChange = this.onChange.bind(this); this.onSave = this.onSave.bind(this); this.onCancel = this.onCancel.bind(this); } componentDidMount() { if (this.dom.name.current != null) { this.dom.name.current.focus(); } } getLocationFromState(item: IState['item']): Partial { const location = cloneDeep(item.location); const formattedAddress = formatLocationToAddress(location); location.unique_name = location.name.concat(' ', formattedAddress); location.address.state = typeof item.state === 'string' ? item.state : item.state?.name; location.address.country = typeof item.country === 'string' ? item.country : item.country?.name; return location; } onChange(field: string, value: string) { const item: IState['item'] = cloneDeep(this.state.item); set(item, field, value); this.setState({ formInvalid: !item.location.name?.length || !item.location.address.line?.[0]?.length || !item.location.address.city?.length || !item.country, item: item, }); } onSave() { this.props.onSave(this.getLocationFromState(this.state.item)); this.onCancel(); } onCancel() { this.props.onCancel(); } render() { const {gettext} = superdeskApi.localization; const buttons = [{ type: 'button', onClick: this.onCancel, text: gettext('Cancel'), }, { color: 'primary', type: 'submit', onClick: this.onSave, text: gettext('Save'), disabled: this.state.formInvalid, }, ]; const item = this.state.item; return (
0} noMargin clearable /> 0} required noMargin clearable /> ); } }