forked from HACC-Hui/HACC-Hui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateTeam.jsx
188 lines (169 loc) · 5.91 KB
/
CreateTeam.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import React from 'react';
import { Meteor } from 'meteor/meteor';
import { Grid, Segment, Header, Loader } from 'semantic-ui-react';
import {
AutoForm,
ErrorsField,
SubmitField,
TextField,
LongTextField,
} from 'uniforms-semantic';
import swal from 'sweetalert';
import PropTypes from 'prop-types';
import { withTracker } from 'meteor/react-meteor-data';
import { _ } from 'lodash';
import { SimpleSchema2Bridge } from 'uniforms-bridge-simple-schema-2';
import SimpleSchema from 'simpl-schema';
import MultiSelectField from '../controllers/MultiSelectField';
import RadioField from '../controllers/RadioField';
import { Teams } from '../../api/team/TeamCollection';
import { defineMethod } from '../../api/base/BaseCollection.methods';
import { Skills } from '../../api/skill/SkillCollection';
import { Tools } from '../../api/tool/ToolCollection';
import { Challenges } from '../../api/challenge/ChallengeCollection';
import { Developers } from '../../api/user/DeveloperCollection';
// Create a schema to specify the structure of the data to appear in the form.
const schema = new SimpleSchema({
name: String,
challenges: { type: Array, label: 'Challenges' },
'challenges.$': { type: String },
skills: { type: Array, label: 'Skills' },
'skills.$': { type: String },
tools: { type: Array, label: 'Tools' },
'tools.$': { type: String },
description: String,
open: {
type: String,
allowedValues: ['Open', 'Closed'],
label: 'Availability',
},
});
/**
* Renders the Page for adding teams.
* @memberOf ui/pages
*/
class CreateTeam extends React.Component {
/** On submit, insert the data.
* @param data {Object} the results from the form.
* @param formRef {FormRef} reference to the form.
*/
submit(definitionData, formRef) {
console.log('CreateTeam.submit', definitionData);
const challengesArray = this.props.challenges;
const challengesObject = [];
const skillsArray = this.props.skills;
const skillsObject = [];
const toolsArray = this.props.tools;
const toolsObject = [];
const owner = this.props.developer.find(currUser => currUser.username === Meteor.user().username);
// Pulls only the first developer from collection instead of the logged in developer who made the team.
let {
name, description, open, challenges, skills, tools,
} = definitionData;
if (open === 'Yes') {
open = true;
} else {
open = false;
console.log('False');
}
for (let i = 0; i < challengesArray.length; i++) {
for (let j = 0; j < challenges.length; j++) {
if (challengesArray[i].name === challenges[j]) {
challengesObject.push(challengesArray[i].slugID);
}
}
}
for (let i = 0; i < skillsArray.length; i++) {
for (let j = 0; j < skills.length; j++) {
if (skillsArray[i].name === skills[j]) {
skillsObject.push(skillsArray[i].slugID);
}
}
}
for (let i = 0; i < toolsArray.length; i++) {
for (let j = 0; j < tools.length; j++) {
if (toolsArray[i].name === tools[j]) {
toolsObject.push(toolsArray[i].slugID);
}
}
}
defineMethod.call({
collectionName: Teams.getCollectionName(),
definitionData: {
name,
description,
owner,
open,
challengesObject,
skillsObject,
toolsObject,
// challenges, skills, and tools don't seem to get added to teamChallenges, teamSkills, teamTools collections.
},
},
(error) => {
if (error) {
swal('Error', error.message, 'error');
console.error(error.message);
} else {
swal('Success', 'success', 'Team made successfully');
formRef.reset();
console.log('Success');
}
});
}
/** Render the form. Use Uniforms: https://github.com/vazco/uniforms */
render() {
return (this.props.ready) ? this.renderPage() : <Loader active>Getting data</Loader>;
}
renderPage() {
let fRef = null;
const formSchema = new SimpleSchema2Bridge(schema);
const challengesArray = _.map(this.props.challenges, 'title');
const skillsArray = _.map(this.props.skills, 'name');
const toolsArray = _.map(this.props.tools, 'name');
return (
<div>
<Grid container centered>
<Grid.Column>
<Header as="h2" textAlign="center">Create a Team</Header>
<AutoForm ref={ref => {fRef = ref;}} schema={formSchema} onSubmit={data => this.submit(data, fRef)}>
<Segment>
<TextField name='name'/>
<MultiSelectField name='challenges' placeholder={'Challenges'}
allowedValues={challengesArray} required/>
<MultiSelectField name='skills' placeholder={'Skills'}
allowedValues={skillsArray} required/>
<MultiSelectField name='tools' placeholder={'Tools'}
allowedValues={toolsArray} required/>
<RadioField name='open'/>
<LongTextField name='description'/>
<SubmitField value='Submit'/>
<ErrorsField/>
</Segment>
</AutoForm>
</Grid.Column>
</Grid>
</div>
);
}
}
CreateTeam.propTypes = {
challenges: PropTypes.array.isRequired,
skills: PropTypes.array.isRequired,
tools: PropTypes.array.isRequired,
developer: PropTypes.array.isRequired,
ready: PropTypes.bool.isRequired,
};
export default withTracker(() => {
const sub1 = Challenges.subscribe();
const sub2 = Skills.subscribe();
const sub3 = Tools.subscribe();
const sub4 = Developers.subscribe();
return {
challenges: Challenges.find({}).fetch(),
skills: Skills.find({}).fetch(),
tools: Tools.find({}).fetch(),
developer: Developers.find({}).fetch(),
ready: sub1.ready() && sub2.ready() && sub3.ready() && sub4.ready(),
};
})(CreateTeam);