Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Meteor } from 'meteor/meteor';
- import { Mongo } from 'meteor/mongo';
- import { SimpleSchema } from 'meteor/aldeed:simple-schema';
- UserProfiles = new Mongo.Collection('UserProfiles');
- // Deny all client-side updates since we will be using methods to manage this collection
- UserProfiles.deny({
- insert() { return true; },
- update() { return true; },
- remove() { return true; },
- });
- UserProfiles.schema = new SimpleSchema({
- userId: {
- type: String,
- regEx: SimpleSchema.RegEx.Id,
- denyUpdate: true,
- },
- firstName: {
- type: String,
- max: 50,
- },
- lastName: {
- type: String,
- max: 50,
- },
- teamSize: {
- type: String,
- },
- role: {
- type: String,
- max: 16,
- },
- industry: {
- type: String,
- },
- phone: {
- type: String,
- },
- createdAt: {
- type: Date,
- denyUpdate: true,
- }
- });
- UserProfiles.attachSchema(UserProfiles.schema);
- import { Meteor } from 'meteor/meteor';
- import { _ } from 'meteor/underscore';
- import { ValidatedMethod } from 'meteor/mdg:validated-method';
- import { SimpleSchema } from 'meteor/aldeed:simple-schema';
- import { DDPRateLimiter } from 'meteor/ddp-rate-limiter';
- import { Accounts } from 'meteor/accounts-base';
- import { UserProfiles } from './userprofiles.js';
- export const insert = new ValidatedMethod({
- name: 'userprofiles.insert',
- validate: new SimpleSchema({
- userId: { type: String },
- firstName: { type: String },
- lastName: { type: String},
- teamSize: { type: String },
- role: { type: String },
- industry: { type: String },
- phone: { type: String },
- createdAt: { type: Date },
- }).validator(),
- run({userId, firstName, lastName, teamSize, role, phone, industry}) {
- const userProfile = {
- userId,
- firstName,
- lastName,
- teamSize,
- role,
- industry,
- phone,
- createdAt: new Date(),
- };
- UserProfiles.insert(userProfile);
- }
- });
- export const remove = new ValidatedMethod({
- name: 'userprofiles.remove',
- validate: new SimpleSchema({
- userProfileId: { type: String },
- }).validator(),
- run({ userProfileId }) {
- const userProfile = UserProfiles.findOne(userProfileId);
- },
- });
- import { Accounts } from 'meteor/accounts-base';
- import { insert } from '../methods.js';
- // Generate a UserProfile for each new user
- Accounts.onCreateUser(function(options, user){
- // Generate a user ID
- user._id = Random.id();
- userProfile = {
- userId: user._id,
- firstName: options.firstName,
- lastName: options.lastName,
- teamSize: options.teamSize,
- role: options.role,
- industry: options.industry,
- phone: options.phone,
- createdAt: new Date(),
- };
- console.log(userProfile);
- console.log(user);
- insert.call(userProfile, (error) => {
- if (error) {
- alert(error.reason);
- }
- });
- return user;
- });
- handleNewUser(event) {
- event.preventDefault();
- const firstName = this.refs.first_name.value;
- const lastName = this.refs.last_name.value;
- const email = this.refs.email.value;
- const password = this.refs.password.value;
- const teamSize = this.refs.team_size.value;
- const role = this.refs.role.value;
- const industry = this.refs.industry.value;
- const phoneNumber = this.refs.phone.value;
- const errors = {};
- if (!firstName) {
- errors.firstName = 'First name required';
- }
- if (!lastName) {
- errors.lastName = 'Last name required';
- }
- if (!email) {
- errors.email = 'Email required';
- }
- if (!password) {
- errors.password = 'Password required';
- }
- if (!teamSize) {
- errors.teamSize = 'Team size required';
- }
- if (!role) {
- errors.role = 'Role required';
- }
- if (!industry) {
- errors.industry = 'Industry required';
- }
- if (!phoneNumber) {
- errors.phoneNumber = 'Phone number required';
- }
- this.setState({errors});
- if (Object.keys(errors).length) {
- return;
- }
- const user = Accounts.createUser({
- email,
- password,
- }, err => {
- if (err) {
- this.setState({
- errors: { none: err.reason },
- });
- }
- });
- insert.call({
- userId: Meteor.userId(),
- firstName: firstName,
- lastName: lastName,
- teamSize: teamSize,
- role: role,
- industry: industry,
- phone: phoneNumber,
- }, (err, res) => {
- if (err) {
- this.setState({
- errors: { none: err.reason },
- });
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement