11import React , { Component } from 'react' ;
22import PropTypes from 'prop-types' ;
33import { Link } from 'react-router-dom' ;
4- import { graphql } from 'react-apollo' ;
4+ import { compose , graphql } from 'react-apollo' ;
55import axios from 'axios' ;
66
77import mapErrors from 'react/util/mapErrors' ;
8+ import compactObject from 'react/util/compactObject' ;
89
910import Button from 'react/components/UI/GenericButton' ;
1011import Box from 'react/components/UI/Box' ;
@@ -13,25 +14,38 @@ import AuthForm from 'react/components/AuthForm';
1314import { Checkbox , Label , Input , ErrorMessage } from 'react/components/UI/Inputs' ;
1415
1516import registerMutation from 'react/components/RegistrationForm/mutations/register' ;
17+ import acceptInvitationMutation from 'react/components/RegistrationForm/mutations/acceptInvitation' ;
1618
1719const { REDIRECT_TO } = require ( 'sharify' ) . data ;
1820
1921class RegistrationForm extends Component {
2022 static propTypes = {
2123 register : PropTypes . func . isRequired ,
24+ acceptInvitation : PropTypes . func . isRequired ,
25+ email : PropTypes . string ,
26+ raw_invitation_token : PropTypes . string ,
2227 }
2328
24- state = {
25- mode : 'resting' ,
26- email : '' ,
27- first_name : '' ,
28- last_name : '' ,
29- password : '' ,
30- password_confirmation : '' ,
31- accept_terms : false ,
32- receive_newsletter : false ,
33- attributeErrors : { } ,
34- errorMessage : null ,
29+ static defaultProps = {
30+ email : null ,
31+ raw_invitation_token : null ,
32+ }
33+
34+ constructor ( props ) {
35+ super ( props ) ;
36+
37+ this . state = {
38+ mode : 'resting' ,
39+ email : props . email || '' ,
40+ first_name : '' ,
41+ last_name : '' ,
42+ password : '' ,
43+ password_confirmation : '' ,
44+ accept_terms : false ,
45+ receive_newsletter : false ,
46+ attributeErrors : { } ,
47+ errorMessage : null ,
48+ } ;
3549 }
3650
3751 handleInput = fieldName => ( { target : { value : fieldValue } } ) =>
@@ -58,7 +72,14 @@ class RegistrationForm extends Component {
5872 handleSubmit = ( e ) => {
5973 e . preventDefault ( ) ;
6074
61- const { register } = this . props ;
75+ const {
76+ register,
77+ acceptInvitation,
78+ raw_invitation_token,
79+ } = this . props ;
80+
81+ const mutation = raw_invitation_token ?
82+ acceptInvitation : register ;
6283
6384 const {
6485 email,
@@ -72,17 +93,18 @@ class RegistrationForm extends Component {
7293
7394 this . setState ( { mode : 'registering' } ) ;
7495
75- return register ( {
76- variables : {
77- email,
78- first_name,
79- last_name,
80- password,
81- accept_terms,
82- password_confirmation,
83- receive_newsletter,
84- } ,
85- } )
96+ const variables = compactObject ( {
97+ email,
98+ first_name,
99+ last_name,
100+ password,
101+ accept_terms,
102+ password_confirmation,
103+ receive_newsletter,
104+ invitation_token : raw_invitation_token ,
105+ } ) ;
106+
107+ return mutation ( { variables } )
86108 . then ( ( ) => {
87109 this . setState ( { mode : 'logging_in' } ) ;
88110 return axios . post ( '/me/sign_in' , { email, password } ) ;
@@ -127,6 +149,7 @@ class RegistrationForm extends Component {
127149 onChange = { this . handleEmail }
128150 value = { email }
129151 errorMessage = { attributeErrors . email }
152+ readOnly = { ! ! this . props . email }
130153 required
131154 />
132155
@@ -224,6 +247,7 @@ class RegistrationForm extends Component {
224247 }
225248}
226249
227- export default graphql ( registerMutation , {
228- name : 'register' ,
229- } ) ( RegistrationForm ) ;
250+ export default compose (
251+ graphql ( registerMutation , { name : 'register' } ) ,
252+ graphql ( acceptInvitationMutation , { name : 'acceptInvitation' } ) ,
253+ ) ( RegistrationForm ) ;
0 commit comments