import * as React from "react" ; import { Mutation , Query , MutationFunc , FetchResult } from "react-apollo" ; import { NewsroomGqlProps } from "./Newsroom" ; import { userDataQuery , getCharterQuery } from "./queries" ; import { saveCharterMutation , SaveAddressMutation , saveStepsMutation } from "./mutations" ; import { Parameters } from "@joincivil/utils" ; import gql from "graphql-tag" ; import { EthAddress , CharterData , BigNumber } from "@joincivil/typescript-types" ; export interface DataWrapperProps { children ( props ) : any ; } export const parametersArray = [ Parameters . minDeposit , Parameters . applyStageLen ] ; export const PARAMETERS_QUERY = gql `
  query Parameters($input: [String!]) {
    parameters: parameters(paramNames: $input) {
      paramName
      value
    }
  }
` ; export class DataWrapper extends React . Component < DataWrapperProps > { public render ( ) { return ( < Query < any > query = { PARAMETERS_QUERY } variables = { { input : parametersArray } } > { ( { loading : paramsLoading , error : paramsError , data : paramsData , } ) => { if ( paramsLoading || paramsError ) { return < > < />; } const minDeposit = new BigNumber ( paramsData . parameters [ 0 ] . value ) ; const applyStageLen = new BigNumber ( paramsData . parameters [ 1 ] . value ) ; return ( < Query < any > query = { userDataQuery } > { ( { loading , error , data : userData } ) => { if ( loading ) { return "Loading..." ; } if ( error ) { return ( < > Sorry , there was an error ! < code > { JSON . stringify ( error ) } < /code> < />); } return ( < Query < any > query = { getCharterQuery } > { ( { loading : charterLoading , error : charterError , data : charterData , refetch : refetchCharterQuery , } ) => { if ( charterLoading ) { return "Loading..." ; } let doRefetchHack = false ; if ( charterError ) { if ( charterError . graphQLErrors [ 0 ] && charterError . graphQLErrors [ 0 ] . message === "No jsonb found" ) { doRefetchHack = true ; } else { return ( < > Sorry , there was an error ! < code > { JSON . stringify ( charterError ) } < /code> < />); } } let persistedCharter ; let grantRequested ; let grantApproved ; let newsroomDeployTx ; let newsroomAddress ; let tcrApplyTx ; if ( charterData && charterData . nrsignupNewsroom ) { if ( charterData . nrsignupNewsroom . charter ) { persistedCharter = charterData . nrsignupNewsroom . charter ; } grantRequested = charterData . nrsignupNewsroom . grantRequested ; grantApproved = charterData . nrsignupNewsroom . grantApproved ; newsroomDeployTx = charterData . nrsignupNewsroom . newsroomDeployTx ; newsroomAddress = charterData . nrsignupNewsroom . newsroomAddress ; tcrApplyTx = charterData . nrsignupNewsroom . tcrApplyTx ; } return ( < Mutation < any > mutation = { SaveAddressMutation } refetchQueries = { [ { query : getCharterQuery , } , ] } > { saveAddress => { return ( < Mutation mutation = { saveCharterMutation } onCompleted = { async ( ) => { if ( doRefetchHack ) { await refetchCharterQuery ( ) ; doRefetchHack = false ; } } } > { ( saveCharter ) => { return ( < Mutation mutation = { saveStepsMutation } > { ( saveSteps ) => { return this . props . children ( { grantRequested , grantApproved , newsroomDeployTx , newsroomAddress , tcrApplyTx , profileWalletAddress : userData . currentUser . ethAddress , savedStep : userData . currentUser . nrStep || 0 , furthestStep : userData . currentUser . nrFurthestStep || 0 , persistedCharter , saveAddress , saveSteps , persistCharter : this . saveCharterFuncFromMutation ( saveCharter ) , minDeposit , applyStageLen , } ) ; } } < /Mutation>); } } < /Mutation>); } } < /Mutation>); } } < /Query>); } } < /Query>); } } < /Query>); } private saveCharterFuncFromMutation ( mutation ) { return async ( charter ) => { return mutation ( { variables : { input : charter , } , } ) ; } ; } }