import * as React from 'react'; import { compose } from 'recompose'; import styled from 'react-emotion'; import Card, { CardBlock, CardTitle, CardActions, CardDivider } from 'mineral-ui/Card'; import { FormField } from 'mineral-ui/Form'; import TextInput from 'mineral-ui/TextInput'; import Button from 'mineral-ui/Button'; import Grid, { GridItem } from 'mineral-ui/Grid'; import Text from 'mineral-ui/Text'; import { ILayoutProps, INewIndexInput, IExternalProps } from './types'; import Alert from 'src/components/Alert'; import { getFileContentCollection } from './utils'; import withLocalFormState from './localState'; import withAddProjectMutation from './graphqlService'; const StyledCard = styled(Card)` width: 1000px; max-width: 100%; position: relative; `; interface IIndexConfigArgs { position: number; index: INewIndexInput; } const Layout: React.ComponentType = (props) => { const { formState: { mutations: { setProjectId, addIndex, setIndexMutationInput, setIndexConfig, removeIndex, setError, setLoadingState, }, state: { projectId, indices, error, isloading }, }, onCancel, addProject, onProjectAdded, } = props; const onProjectIdInputChange = (e: React.SyntheticEvent) => setProjectId(e.currentTarget.value); const onSubmit = async () => { try { setLoadingState(true); await addProject({ projectId, indexConfigs: indices, }); onProjectAdded(); } catch (err) { setLoadingState(false); setError(err); } }; const onIndexAddClick = () => addIndex({ newIndexMutationInput: { projectId, graphqlField: '', esIndex: '', }, config: null, }); const onIndexGraphqlFieldChange = (arg: IIndexConfigArgs) => ( e: React.SyntheticEvent, ) => setIndexMutationInput(arg.position)({ ...arg.index, graphqlField: e.currentTarget.value, }); const onIndexEsIndexChange = (arg: IIndexConfigArgs) => ( e: React.SyntheticEvent, ) => setIndexMutationInput(arg.position)({ ...arg.index, esIndex: e.currentTarget.value, }); const onFileUpload = ({ position }: { position: number }) => async ( e: React.SyntheticEvent, ) => { const files = e.currentTarget.files; if (files) { try { const filesCollection = await getFileContentCollection(files); return setIndexConfig(position)(filesCollection); } catch (err) { setIndexConfig(position)(null); setError(err); } } return; }; const onIndexConfigRemoveClick = (position: number) => () => removeIndex(position); return ( Project Config {error && ( {error.message} )} {indices.map((index, position) => ( ))} ); }; export default compose<{}, IExternalProps>(withLocalFormState, withAddProjectMutation)(Layout);