|
| 1 | +import React from 'react' |
| 2 | +import { Query, withApollo } from 'react-apollo' |
| 3 | +import gql from 'graphql-tag' |
| 4 | +import Button from 'material-ui/Button' |
| 5 | +import { withRouter } from 'react-router' |
| 6 | + |
| 7 | +let attemptingToClaim = false |
| 8 | + |
| 9 | +const Team = ({ urlToken, user, login, client, history }) => { |
| 10 | + const claimSeat = async () => { |
| 11 | + if (user) { |
| 12 | + if (user.hasPurchased && !attemptingToClaim) { |
| 13 | + alert( |
| 14 | + 'You already have access to the Guide. Contact us with questions: team@graphql.guide' |
| 15 | + ) |
| 16 | + } else { |
| 17 | + // todo spinner |
| 18 | + await client.mutate({ mutation: JOIN_TEAM, variables: { urlToken } }) |
| 19 | + history.push('/welcome') |
| 20 | + } |
| 21 | + attemptingToClaim = false |
| 22 | + } else { |
| 23 | + attemptingToClaim = true |
| 24 | + login() |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + const claimerJustLoggedIn = attemptingToClaim && user |
| 29 | + if (claimerJustLoggedIn) { |
| 30 | + claimSeat() |
| 31 | + } |
| 32 | + |
| 33 | + return ( |
| 34 | + <main className="Team"> |
| 35 | + <Query query={TEAM_QUERY} variables={{ urlToken }}> |
| 36 | + {({ loading, data }) => { |
| 37 | + if (loading) return <div className="Spinner" /> |
| 38 | + |
| 39 | + if (!data.team) return 'No such team' |
| 40 | + const { |
| 41 | + team: { name, totalSeats, members } |
| 42 | + } = data |
| 43 | + const seatsLeft = totalSeats - members.length |
| 44 | + |
| 45 | + return ( |
| 46 | + <div> |
| 47 | + <div className="Team-header-wrapper"> |
| 48 | + <header className="Team-header"> |
| 49 | + <h1>Team {name}</h1> |
| 50 | + </header> |
| 51 | + </div> |
| 52 | + <div className="Team-content"> |
| 53 | + {seatsLeft && ( |
| 54 | + <p> |
| 55 | + {seatsLeft} seats left |
| 56 | + <Button |
| 57 | + color="primary" |
| 58 | + variant="raised" |
| 59 | + className="Team-claim-seat" |
| 60 | + onClick={claimSeat} |
| 61 | + > |
| 62 | + Claim seat |
| 63 | + </Button> |
| 64 | + </p> |
| 65 | + )} |
| 66 | + <div className="Team-member-list"> |
| 67 | + <p>Team members:</p> |
| 68 | + <ul> |
| 69 | + {members.map(({ name, id }) => ( |
| 70 | + <li key={id}>{name}</li> |
| 71 | + ))} |
| 72 | + </ul> |
| 73 | + </div> |
| 74 | + <p className="Team-summary"> |
| 75 | + {totalSeats}-seat license. Add 5 more seats{' '} |
| 76 | + <a href="//paypal.me/graphqlguide/1000">by PayPal</a>. (Put |
| 77 | + the team name in the payment note.) |
| 78 | + </p> |
| 79 | + </div> |
| 80 | + </div> |
| 81 | + ) |
| 82 | + }} |
| 83 | + </Query> |
| 84 | + </main> |
| 85 | + ) |
| 86 | +} |
| 87 | + |
| 88 | +const TEAM_QUERY = gql` |
| 89 | + query TeamQuery($urlToken: String!) { |
| 90 | + team(urlToken: $urlToken) { |
| 91 | + id |
| 92 | + members { |
| 93 | + id |
| 94 | + name |
| 95 | + } |
| 96 | + name |
| 97 | + totalSeats |
| 98 | + } |
| 99 | + } |
| 100 | +` |
| 101 | + |
| 102 | +const JOIN_TEAM = gql` |
| 103 | + mutation JoinTeamMutation($urlToken: String!) { |
| 104 | + joinTeam(urlToken: $urlToken) { |
| 105 | + id |
| 106 | + members { |
| 107 | + id |
| 108 | + name |
| 109 | + hasPurchased |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +` |
| 114 | + |
| 115 | +export default withApollo(withRouter(Team)) |
0 commit comments