Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. import { Meteor } from 'meteor/meteor';
  2. import { Mongo } from 'meteor/mongo';
  3. import { SimpleSchema } from 'meteor/aldeed:simple-schema';
  4.  
  5. UserProfiles = new Mongo.Collection('UserProfiles');
  6.  
  7. // Deny all client-side updates since we will be using methods to manage this collection
  8. UserProfiles.deny({
  9. insert() { return true; },
  10. update() { return true; },
  11. remove() { return true; },
  12. });
  13.  
  14. UserProfiles.schema = new SimpleSchema({
  15. userId: {
  16. type: String,
  17. regEx: SimpleSchema.RegEx.Id,
  18. denyUpdate: true,
  19. },
  20. firstName: {
  21. type: String,
  22. max: 50,
  23. },
  24. lastName: {
  25. type: String,
  26. max: 50,
  27. },
  28. teamSize: {
  29. type: String,
  30. },
  31. role: {
  32. type: String,
  33. max: 16,
  34. },
  35. industry: {
  36. type: String,
  37. },
  38. phone: {
  39. type: String,
  40. },
  41. createdAt: {
  42. type: Date,
  43. denyUpdate: true,
  44. }
  45. });
  46.  
  47. UserProfiles.attachSchema(UserProfiles.schema);
  48.  
  49. import { Meteor } from 'meteor/meteor';
  50. import { _ } from 'meteor/underscore';
  51. import { ValidatedMethod } from 'meteor/mdg:validated-method';
  52. import { SimpleSchema } from 'meteor/aldeed:simple-schema';
  53. import { DDPRateLimiter } from 'meteor/ddp-rate-limiter';
  54. import { Accounts } from 'meteor/accounts-base';
  55.  
  56. import { UserProfiles } from './userprofiles.js';
  57.  
  58. export const insert = new ValidatedMethod({
  59. name: 'userprofiles.insert',
  60. validate: new SimpleSchema({
  61. userId: { type: String },
  62. firstName: { type: String },
  63. lastName: { type: String},
  64. teamSize: { type: String },
  65. role: { type: String },
  66. industry: { type: String },
  67. phone: { type: String },
  68. createdAt: { type: Date },
  69. }).validator(),
  70. run({userId, firstName, lastName, teamSize, role, phone, industry}) {
  71. const userProfile = {
  72. userId,
  73. firstName,
  74. lastName,
  75. teamSize,
  76. role,
  77. industry,
  78. phone,
  79. createdAt: new Date(),
  80. };
  81.  
  82. UserProfiles.insert(userProfile);
  83. }
  84. });
  85.  
  86. export const remove = new ValidatedMethod({
  87. name: 'userprofiles.remove',
  88. validate: new SimpleSchema({
  89. userProfileId: { type: String },
  90. }).validator(),
  91. run({ userProfileId }) {
  92. const userProfile = UserProfiles.findOne(userProfileId);
  93. },
  94. });
  95.  
  96. import { Accounts } from 'meteor/accounts-base';
  97. import { insert } from '../methods.js';
  98.  
  99. // Generate a UserProfile for each new user
  100. Accounts.onCreateUser(function(options, user){
  101. // Generate a user ID
  102. user._id = Random.id();
  103.  
  104. userProfile = {
  105. userId: user._id,
  106. firstName: options.firstName,
  107. lastName: options.lastName,
  108. teamSize: options.teamSize,
  109. role: options.role,
  110. industry: options.industry,
  111. phone: options.phone,
  112. createdAt: new Date(),
  113. };
  114.  
  115. console.log(userProfile);
  116. console.log(user);
  117.  
  118. insert.call(userProfile, (error) => {
  119. if (error) {
  120. alert(error.reason);
  121. }
  122. });
  123.  
  124. return user;
  125. });
  126.  
  127. handleNewUser(event) {
  128. event.preventDefault();
  129. const firstName = this.refs.first_name.value;
  130. const lastName = this.refs.last_name.value;
  131. const email = this.refs.email.value;
  132. const password = this.refs.password.value;
  133. const teamSize = this.refs.team_size.value;
  134. const role = this.refs.role.value;
  135. const industry = this.refs.industry.value;
  136. const phoneNumber = this.refs.phone.value;
  137. const errors = {};
  138.  
  139. if (!firstName) {
  140. errors.firstName = 'First name required';
  141. }
  142. if (!lastName) {
  143. errors.lastName = 'Last name required';
  144. }
  145. if (!email) {
  146. errors.email = 'Email required';
  147. }
  148. if (!password) {
  149. errors.password = 'Password required';
  150. }
  151. if (!teamSize) {
  152. errors.teamSize = 'Team size required';
  153. }
  154. if (!role) {
  155. errors.role = 'Role required';
  156. }
  157. if (!industry) {
  158. errors.industry = 'Industry required';
  159. }
  160. if (!phoneNumber) {
  161. errors.phoneNumber = 'Phone number required';
  162. }
  163.  
  164. this.setState({errors});
  165. if (Object.keys(errors).length) {
  166. return;
  167. }
  168.  
  169. const user = Accounts.createUser({
  170. email,
  171. password,
  172. }, err => {
  173. if (err) {
  174. this.setState({
  175. errors: { none: err.reason },
  176. });
  177. }
  178. });
  179.  
  180. insert.call({
  181. userId: Meteor.userId(),
  182. firstName: firstName,
  183. lastName: lastName,
  184. teamSize: teamSize,
  185. role: role,
  186. industry: industry,
  187. phone: phoneNumber,
  188. }, (err, res) => {
  189. if (err) {
  190. this.setState({
  191. errors: { none: err.reason },
  192. });
  193. }
  194. });
  195.  
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement