import * as React from 'react'; import { connect } from 'react-redux'; import { compose } from 'recompose'; import { ApolloConsumer } from 'react-apollo'; import { THoc } from 'src/utils'; import Component from 'react-component-component'; import { QUERY, IGqlData } from 'src/gql/queries/allProjectData'; import { ApolloQueryResult } from 'apollo-boost'; import Table, { TableRow, TableCell } from 'mineral-ui/Table'; import { Link as RouterLink } from 'react-router-dom'; import { ActionType } from 'src/store/configEditorReducer'; import { IGlobalState } from 'src/store'; import Link from 'src/components/Link'; import ProjectActionButtons from '../ProjectActionButtons'; import Flex, { FlexItem } from 'mineral-ui/Flex'; /***************** * provides data fetcher *****************/ export interface IPropsWithProjectDataGetter { getProjectData: (projectId: string) => Promise>; } export const withProjectDataGetter: THoc<{}, IPropsWithProjectDataGetter> = (Wrapped) => ( props, ) => { return ( {(client) => { const getProjectData = (projectId: string): Promise> => { return client.query({ query: QUERY, variables: { projectId, }, fetchPolicy: 'no-cache', }); }; const mergedProps: IPropsWithProjectDataGetter = { getProjectData, ...props, }; return ; }} ); }; /********************* * provides global state *********************/ interface IReduxStateProps { projectData: IGqlData | null; } interface IReduxDispatchProps { onDataLoaded: (data: IGqlData) => void; } export interface IPropsFromRedux extends IReduxStateProps, IReduxDispatchProps {} const mapStateToProps = (state: IGlobalState): IReduxStateProps => ({ projectData: state.configEditor.currentProjectData, }); const mapDispatchToProps = (dispatch): IReduxDispatchProps => ({ onDataLoaded: (data) => { const action = { type: ActionType.PROJECT_DATA_LOADED, payload: { data }, }; dispatch(action); }, }); export const reduxContainer = connect(mapStateToProps, mapDispatchToProps); /********************* * local state container *********************/ interface IInjectedProps extends IPropsWithProjectDataGetter, IPropsFromRedux {} interface IExternalProps { projectId: string; } const Dashboard: React.ComponentType = ({ projectId, getProjectData, onDataLoaded, projectData, }) => { React.useEffect(() => { if (!projectData) { getProjectData(projectId).then(({ data }) => { onDataLoaded(data); }); } }, []); if (!projectData) { return <>...loading; } else { const { project: { indices }, } = projectData; const rows = indices.map((index) => ({ row: () => { return ( {index.graphqlField} {index.esIndex} {index.esType} {index.hasMapping ? 'yes' : 'no'} ); }, })); return ( ); } }; export default compose( withProjectDataGetter, reduxContainer, )(Dashboard);