11import React from 'react' ;
22import styled from 'styled-components' ;
33
4- import { Mutation } from 'react-apollo ' ;
4+ import { useMutation } from '@apollo/ react-hooks ' ;
55
66import Input from '@codesandbox/common/lib/components/Input' ;
77import { Button } from '@codesandbox/common/lib/components/Button' ;
@@ -19,68 +19,60 @@ const ErrorMessage = styled.div`
1919
2020export const AddTeamMember = ( { teamId } ) => {
2121 const { actions } = useOvermind ( ) ;
22+ const [ mutate , { loading, error } ] = useMutation ( INVITE_TO_TEAM ) ;
23+ let input = null ;
24+
25+ const submit = e => {
26+ e . preventDefault ( ) ;
27+ e . stopPropagation ( ) ;
28+
29+ let isEmail = input . value . includes ( '@' ) ;
30+
31+ track ( 'Team - Add Member' , { email : isEmail } ) ;
32+
33+ isEmail = false ;
34+
35+ // We don't enable email for now for privacy reasons
36+
37+ const variables = { teamId } ;
38+
39+ const { value } = input ;
40+ if ( isEmail ) {
41+ variables . email = value ;
42+ } else {
43+ variables . username = value ;
44+ }
45+
46+ mutate ( {
47+ variables,
48+ } ) . then ( ( ) => {
49+ actions . notificationAdded ( {
50+ message : `${ value } has been invited!` ,
51+ type : 'success' ,
52+ } ) ;
53+ } ) ;
54+
55+ input . value = '' ;
56+ } ;
57+
58+ const errorMessage =
59+ error && error . graphQLErrors && error . graphQLErrors [ 0 ] . message ;
60+
2261 return (
23- < Mutation mutation = { INVITE_TO_TEAM } >
24- { ( mutate , { loading, error } ) => {
25- let input = null ;
26-
27- const submit = e => {
28- e . preventDefault ( ) ;
29- e . stopPropagation ( ) ;
30-
31- let isEmail = input . value . includes ( '@' ) ;
32-
33- track ( 'Team - Add Member' , { email : isEmail } ) ;
34-
35- isEmail = false ;
36-
37- // We don't enable email for now for privacy reasons
38-
39- const variables = { teamId } ;
40-
41- const { value } = input ;
42- if ( isEmail ) {
43- variables . email = value ;
44- } else {
45- variables . username = value ;
46- }
47-
48- mutate ( {
49- variables,
50- } ) . then ( ( ) => {
51- actions . notificationAdded ( {
52- message : `${ value } has been invited!` ,
53- type : 'success' ,
54- } ) ;
55- } ) ;
56-
57- input . value = '' ;
58- } ;
59-
60- const errorMessage =
61- error && error . graphQLErrors && error . graphQLErrors [ 0 ] . message ;
62-
63- return (
64- < >
65- { errorMessage && < ErrorMessage > { errorMessage } </ ErrorMessage > }
66- < form
67- style = { { display : 'flex' } }
68- onSubmit = { loading ? undefined : submit }
69- >
70- < Input
71- ref = { node => {
72- input = node ;
73- } }
74- placeholder = "Add member by username"
75- block
76- />
77- < Button disabled = { loading } style = { { width : 200 } } small >
78- { loading ? 'Adding Member...' : 'Add Member' }
79- </ Button >
80- </ form >
81- </ >
82- ) ;
83- } }
84- </ Mutation >
62+ < >
63+ { errorMessage && < ErrorMessage > { errorMessage } </ ErrorMessage > }
64+ < form style = { { display : 'flex' } } onSubmit = { loading ? undefined : submit } >
65+ < Input
66+ ref = { node => {
67+ input = node ;
68+ } }
69+ placeholder = "Add member by username"
70+ block
71+ />
72+ < Button disabled = { loading } style = { { width : 200 } } small >
73+ { loading ? 'Adding Member...' : 'Add Member' }
74+ </ Button >
75+ </ form >
76+ </ >
8577 ) ;
8678} ;
0 commit comments