index.tsx 1.29 KB
import Component from "./Component";
import * as React from "react";
import { Query, Mutation } from "react-apollo";
import { withRouter, RouteComponentProps } from "react-router";

const FETCH_ALL_MODELS = require("./fetchAllModels.gql");
const REGISTER_DRONE = require("./registerDrone.gql");

class FetchAllModelsQuery extends Query<{
  models: Array<any>;
}> {}

class RegisterDroneMutation extends Mutation<{}> {}

const update = (history: any) => (cache, result) => history.push("/my-page");

const QueryComponent: React.SFC<RouteComponentProps<{}>> = ({ history }) => (
  <FetchAllModelsQuery query={FETCH_ALL_MODELS}>
    {({ loading, error, data }) => {
      if (loading) return <p>Loading...</p>;
      else if (error) return <p>Error :(</p>;
      else
        return (
          <RegisterDroneMutation
            mutation={REGISTER_DRONE}
            update={update(history)}
          >
            {mutate => {
              const registerDrone = input => mutate({ variables: { input } });
              return (
                <Component
                  models={data!.models}
                  registerDrone={registerDrone}
                />
              );
            }}
          </RegisterDroneMutation>
        );
    }}
  </FetchAllModelsQuery>
);

export default withRouter(QueryComponent);