2016-04-15 11:09:19 +02:00
import React , { PropTypes , Component } from 'react' ;
2016-04-17 10:29:52 +09:00
import { Modal } from 'react-bootstrap' ;
2017-03-23 16:27:59 +09:00
import Users from 'meteor/vulcan:users' ;
2017-05-01 10:49:27 +09:00
import { withDocument , Components , registerComponent , withMessages } from 'meteor/vulcan:core' ;
2017-02-28 11:49:40 +01:00
import { FormattedMessage , intlShape } from 'react-intl' ;
2017-05-01 10:49:27 +09:00
import { gql } from 'react-apollo' ;
2017-02-28 11:49:40 +01:00
2017-05-03 14:42:39 +09:00
const UsersProfileCheck = ( { currentUser , document , loading , flash } , context ) => {
2017-02-28 11:49:40 +01:00
2017-05-01 10:49:27 +09:00
// we're loading all fields marked as "mustComplete" using withDocument
const userMustCompleteFields = document ;
2017-02-28 11:49:40 +01:00
2017-05-03 14:42:39 +09:00
// if user is not logged in, or userMustCompleteFields is still loading, don't return anything
if ( ! currentUser || loading ) {
2017-05-01 10:49:27 +09:00
return null ;
} else {
2017-02-28 11:49:40 +01:00
// return fields that are required by the schema but haven't been filled out yet
2017-05-01 10:49:27 +09:00
const fieldsToComplete = _ . filter ( Users . getRequiredFields ( ) , fieldName => {
return ! userMustCompleteFields [ fieldName ] ;
2017-02-28 11:49:40 +01:00
} ) ;
2017-05-03 14:42:39 +09:00
if ( fieldsToComplete . length > 0 ) {
2017-05-01 10:49:27 +09:00
return (
< Modal bsSize = 'small' show = { true } >
< Modal.Header >
< Modal.Title > < FormattedMessage id = "users.complete_profile" / > < / Modal.Title >
< / Modal.Header >
< Modal.Body >
< Components.SmartForm
collection = { Users }
documentId = { currentUser . _id }
fields = { fieldsToComplete }
successCallback = { user => {
const newUser = { ... currentUser , ... user } ;
if ( Users . hasCompletedProfile ( newUser ) ) {
flash ( context . intl . formatMessage ( { id : "users.profile_completed" } ) , 'success' ) ;
}
} }
/ >
< / Modal.Body >
< Modal.Footer >
< FormattedMessage id = "app.or" / > < a className = "complete-profile-logout" onClick = { ( ) => Meteor . logout ( ( ) => window . location . reload ( ) /* something is broken here when giving the apollo client as a prop*/ ) } > < FormattedMessage id = "users.log_out" / > < / a >
< / Modal.Footer >
< / Modal >
)
} else {
return null ;
}
2017-02-28 11:49:40 +01:00
}
2016-04-15 11:09:19 +02:00
2016-10-14 08:47:18 +02:00
} ;
2016-04-17 10:29:52 +09:00
2016-11-15 18:33:16 +01:00
UsersProfileCheck . propsTypes = {
2016-10-14 08:47:18 +02:00
currentUser : React . PropTypes . object
} ;
2016-04-16 18:02:21 +02:00
2017-02-28 11:49:40 +01:00
UsersProfileCheck . contextTypes = {
intl : intlShape
} ;
2016-05-22 16:42:24 +09:00
UsersProfileCheck . displayName = "UsersProfileCheck" ;
2017-05-01 10:49:27 +09:00
const mustCompleteFragment = gql `
fragment UsersMustCompleteFragment on User {
_id
$ { Users . getRequiredFields ( ) . join ( '\n' ) }
}
`
const options = {
collection : Users ,
queryName : 'usersMustCompleteQuery' ,
fragment : mustCompleteFragment ,
} ;
registerComponent ( 'UsersProfileCheck' , UsersProfileCheck , withMessages , [ withDocument , options ] ) ;