Advertisement
Guest User

Untitled

a guest
Sep 30th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.08 KB | None | 0 0
  1. import React from 'react';
  2. import Button from '../../../Framework/Button';
  3. import Dialog from 'material-ui/Dialog';
  4. import Colors from '../../../Framework/Colors';
  5. import TextField from 'material-ui/TextField';
  6. import Checkbox from 'material-ui/Checkbox';
  7. import MenuItem from 'material-ui/MenuItem';
  8. import SelectField from 'material-ui/SelectField';
  9. import { lodash as _ } from 'meteor/erasaur:meteor-lodash';
  10. import { Accounts } from 'meteor/accounts-base';
  11. import TandCDialog from './TandCDialog';
  12.  
  13. import Alert from 'react-s-alert';
  14.  
  15. const countries = [
  16. { country: '' },
  17. { country: 'Belgium' },
  18. { country: 'Netherlands' },
  19. { country: 'Other' }
  20. ];
  21.  
  22. const styles = {
  23. buttons: {
  24. wrapper: {
  25. display: 'flex',
  26. justifyContent: 'flex-end',
  27. marginTop: '30px'
  28. },
  29. nonLastButton: {
  30. marginRight: '15px'
  31. }
  32. }
  33. };
  34.  
  35. class SignUpDialog extends React.Component {
  36.  
  37. constructor() {
  38. super();
  39. this.register = this.register.bind(this);
  40. this.setEmail = this.setEmail.bind(this);
  41. this.setUsername = this.setUsername.bind(this);
  42. this.setPassword = this.setPassword.bind(this);
  43. this.setCountryIndex = this.setCountryIndex.bind(this);
  44. this.setTac = this.setTac.bind(this);
  45. this.openTaCDialog = this.openTaCDialog.bind(this);
  46. this.state = {
  47. email: '',
  48. username: '',
  49. password: '',
  50. country: '',
  51. countryIndex: 0,
  52. Tac: false,
  53. emailError: '',
  54. usernameError: '',
  55. passwordError: '',
  56. countryError: '',
  57. tacLabel: '',
  58. validationError: false,
  59. showTaCDialog: false
  60. };
  61. }
  62.  
  63. setEmail(event) {
  64. this.setState({ email: event.target.value });
  65. }
  66. setUsername(event) {
  67. this.setState({ username: event.target.value });
  68. }
  69.  
  70. setPassword(event) {
  71. this.setState({ password: event.target.value });
  72. }
  73.  
  74. setTac(event, checked) {
  75. this.setState({ Tac: checked });
  76. }
  77.  
  78. setCountryIndex(event, index, value) {
  79. this.setState({ countryIndex: value });
  80. }
  81.  
  82. closeTandCDialog() {
  83. this.setState({ showTaCDialog: false });
  84. }
  85.  
  86. openTaCDialog() {
  87. this.setState({ showTaCDialog: true });
  88. }
  89.  
  90. renderCountryOptions() {
  91. return (
  92. <div>
  93. <SelectField
  94. value={this.state.countryIndex}
  95. onChange={this.setCountryIndex}
  96. fullWidth
  97. floatingLabelText="Select your country"
  98. errorText={this.state.countryError}
  99. maxHeight={200}
  100. >
  101. {this.renderCountries()}
  102. </SelectField>
  103. </div>
  104. );
  105. }
  106.  
  107. renderCountries() {
  108. return (
  109. _.map(countries, (country, key) => {
  110. return (
  111. <MenuItem key={key} value={key} primaryText={country.country} />
  112. );
  113. })
  114. );
  115. }
  116.  
  117. validateMail() {
  118. if (!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(this.state.email)) {
  119. return 'Not a valid email address';
  120. }
  121.  
  122. return '';
  123. }
  124.  
  125. validateUsername() {
  126. if (this.state.username.length === 0) {
  127. return 'Please enter a username';
  128. }
  129.  
  130. return '';
  131. }
  132.  
  133. validatePassword() {
  134. if (this.state.password.length < 6) {
  135. return 'Password must be at least 6 character';
  136. }
  137.  
  138. return '';
  139. }
  140.  
  141. validateCountry() {
  142. if (this.state.countryIndex === 0) {
  143. return 'Please select a Country';
  144. }
  145. if (
  146. countries[this.state.countryIndex].country !== 'Belgium'
  147. && countries[this.state.countryIndex].country !== 'Netherlands'
  148. ) {
  149. return 'Only players with Belgian or Dutch nationality are allowed.';
  150. }
  151.  
  152. return '';
  153. }
  154.  
  155. validateTac() {
  156. if (!this.state.Tac) {
  157. return 'Required';
  158. }
  159.  
  160. return '';
  161. }
  162.  
  163. checkAllFields() {
  164. if (
  165. this.validateMail()
  166. || this.validateUsername()
  167. || this.validatePassword()
  168. || this.validateCountry()
  169. || this.validateTac()
  170. ) {
  171. return true;
  172. }
  173.  
  174. return false;
  175. }
  176.  
  177. register() {
  178. this.setState(
  179. {
  180. emailError: this.validateMail(),
  181. usernameError: this.validateUsername(),
  182. passwordError: this.validatePassword(),
  183. countryError: this.validateCountry(),
  184. tacLabel: this.validateTac(),
  185. validationError: this.checkAllFields()
  186. },
  187. () => {
  188. const email = this.state.email;
  189. const username = this.state.username;
  190. const password = this.state.password;
  191. const profile = {};
  192.  
  193. if (!this.state.validationError) {
  194. Accounts.createUser(
  195. { username, email, password, profile }, (Error) => {
  196. if (Error) {
  197. Alert.error(
  198. 'Creating an account failed with the following message: '
  199. + `${Error.reason}`);
  200. } else {
  201. Alert.success(
  202. 'Your account has been successfully created. Good luck and have fun on BeSports!'
  203. );
  204. }
  205. }
  206. );
  207. }
  208. }
  209. );
  210. }
  211.  
  212. render() {
  213. console.log(this.state.showTaCDialog);
  214. return (
  215. <Dialog
  216. title="Register account"
  217. open={this.state.showTaCDialog ? false : this.props.showSignUp}
  218. onRequestClose={this.props.closeDialog}
  219. >
  220. <form onSubmit={this.register}>
  221. <TextField
  222. value={this.state.email}
  223. hintText="Email"
  224. fullWidth
  225. type="email"
  226. errorText={this.state.emailError}
  227. onChange={this.setEmail}
  228. />
  229. <TextField
  230. value={this.state.username}
  231. hintText="Username"
  232. fullWidth
  233. onChange={this.setUsername}
  234. errorText={this.state.usernameError}
  235. />
  236. <TextField
  237. value={this.state.password}
  238. hintText="Password"
  239. fullWidth
  240. onChange={this.setPassword}
  241. type="password"
  242. errorText={this.state.passwordError}
  243. />
  244. {this.renderCountryOptions()}
  245. <Checkbox
  246. label="Agree to the Terms and Conditions"
  247. onCheck={this.setTac}
  248. />
  249. <div onClick={this.openTaCDialog}>
  250. click
  251. </div>
  252. <div>
  253. {this.state.tacLabel}
  254. </div>
  255. <div style={styles.buttons.wrapper}>
  256. <Button
  257. text="Cancel"
  258. color={Colors.grey700}
  259. clickFunction={this.props.closeDialog}
  260. style={styles.buttons.nonLastButton}
  261. />
  262. <Button
  263. text="Register"
  264. color={this.context.color}
  265. clickFunction={this.register}
  266. />
  267. </div>
  268. </form>
  269. <TandCDialog close={this.closeTandCDialog} open={this.state.showTaCDialog} />
  270. </Dialog>
  271. );
  272. }
  273. }
  274.  
  275. SignUpDialog.propTypes = {
  276. showSignUp: React.PropTypes.bool,
  277. closeDialog: React.PropTypes.func
  278. };
  279.  
  280. SignUpDialog.contextTypes = {
  281. color: React.PropTypes.string
  282. };
  283.  
  284. export default SignUpDialog;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement