Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add attendee custom form option #4480

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
'plugin:ember-suave/recommended'
],
env: {
es6: true,
browser: true
},
rules: {
Expand Down
12 changes: 12 additions & 0 deletions app/components/forms/orders/order-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default Component.extend(FormMixin, {
attendee.set('firstname', '');
attendee.set('lastname', '');
attendee.set('email', '');
attendee.set('complexFieldValues', {});
});
return this.data.attendees;
}),
Expand Down Expand Up @@ -471,6 +472,17 @@ export default Component.extend(FormMixin, {
validationRules.fields[`facebook_required_${ index}`] = facebookRequiredValidation;
validationRules.fields[`github_${ index}`] = githubValidation;
validationRules.fields[`github_required_${ index}`] = githubRequiredValidation;
this.allFields.attendee.filter(field => field.isComplex && field.isRequired).forEach(field => {
validationRules.fields[`${field.fieldIdentifier}_required_${index}`] = {
rules: [
{
type : 'empty',
prompt : this.l10n.t('Please enter ' + field.name)
}
]
};
});

});
return validationRules;
},
Expand Down
33 changes: 33 additions & 0 deletions app/components/forms/wizard/attendee-step.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
import Component from '@ember/component';
import FormMixin from 'open-event-frontend/mixins/form';
import { slugify } from 'open-event-frontend/utils/text';
import { computed } from '@ember/object';

function getIdentifier(name, fields) {
const fieldIdentifiers = new Set(fields.map(field => field.fieldIdentifier));
let identifier = slugify(name, '_');
while (fieldIdentifiers.has(identifier)) {
identifier += '_';
}

return identifier;
}

export default Component.extend(FormMixin, {
identifier: computed('data.newFormField.name', 'data.customForms', function() {
return getIdentifier(this.data.newFormField.name, this.data.customForms);
}),
validIdentifier: computed('data.newFormField.name', 'identifier', function() {
return this.identifier.trim().length > 0 && this.data.newFormField.name.trim().length > 0;
}),
actions: {
saveDraft() {
this.onValid(() => {
Expand All @@ -19,6 +37,21 @@ export default Component.extend(FormMixin, {
this.set('data.event.state', 'published');
this.sendAction('save', this.data);
});
},
addFormField() {
if (!this.validIdentifier) {
return;
}
this.data.customForms.pushObject(this.store.createRecord('custom-form', {
fieldIdentifier : this.identifier,
name : this.data.newFormField.name,
form : 'attendee',
type : this.data.newFormField.type,
isRequired : false,
isIncluded : false,
event : this.data.event
}));
this.set('data.newFormField.name', '');
}
}
});
15 changes: 3 additions & 12 deletions app/models/custom-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default ModelBase.extend({
fieldIdentifier : attr('string'),
form : attr('string'),
type : attr('string', { defaultValue: 'text' }),
name : attr('string'),
isRequired : attr('boolean', { defaultValue: false }),
isIncluded : attr('boolean', { defaultValue: false }),
isFixed : attr('boolean', { defaultValue: false }),
Expand Down Expand Up @@ -83,18 +84,8 @@ export default ModelBase.extend({
ageGroup : 'Age Group'
},

name: computed('fieldIdentifier', 'form', function() {
let name = this.fieldIdentifier;
if (!this.isComplex) {
if (this.form === 'session') {
name = this.get(`session.${name}`);
} else if (this.form === 'speaker') {
name = this.get(`speaker.${name}`);
} else {
name = this.get(`attendee.${name}`);
}
}
return name;
identifierPath: computed('isComplex', function() {
return !this.isComplex ? this.fieldIdentifier : 'complexFieldValues.' + this.fieldIdentifier;
}),

isLongText: computed('type', function() {
Expand Down
23 changes: 14 additions & 9 deletions app/routes/events/view/edit/attendee.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,25 @@ export default class AttendeeRoute extends Route.extend(CustomFormMixin) {
}

async model() {
let filterOptions = [{
const filterOptions = [{
name : 'form',
op : 'eq',
val : 'attendee'
}];

let data = {
event: this.modelFor('events.view')
const event = this.modelFor('events.view');
const data = {
event,
customForms: await event.query('customForms', {
filter : filterOptions,
sort : 'id',
'page[size]' : 50
}),
newFormField: {
name : '',
type : 'text'
}
};
data.customForms = await data.event.query('customForms', {
filter : filterOptions,
sort : 'id',
'page[size]' : 50
});

return data;
}
Expand All @@ -32,7 +37,7 @@ export default class AttendeeRoute extends Route.extend(CustomFormMixin) {
* Create the additional custom forms if only the compulsory forms exist.
*/
if (data.customForms.length === 3) {
let customForms = A();
const customForms = A();
for (const customForm of data.customForms ? data.customForms.toArray() : []) {
customForms.pushObject(customForm);
}
Expand Down
9 changes: 8 additions & 1 deletion app/routes/orders/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ export default class NewRoute extends Route {
event : eventDetails,
tickets,
form : await eventDetails.query('customForms', {
'page[size]' : 50,
filter: [
{
name : 'form',
op : 'eq',
val : 'attendee'
}
],
'page[size]' : 0,
sort : 'id'
})
};
Expand Down
4 changes: 2 additions & 2 deletions app/templates/components/forms/orders/order-form.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@
{{#if (and this.sameAsBuyer (eq index 0) (or (eq field.fieldIdentifier 'firstname') (eq field.fieldIdentifier 'lastname') (eq field.fieldIdentifier 'email')))}}
<Input
@type={{field.type}}
@value={{mut (get holder field.fieldIdentifier)}}
@value={{mut (get holder field.identifierPath)}}
@name={{if field.isRequired (concat field.fieldIdentifier "_required_" index) (concat field.fieldIdentifier "_" index)}}
@readonly="" />
{{else}}
<Input
@type={{field.type}}
@value={{mut (get holder field.fieldIdentifier)}}
@value={{mut (get holder field.identifierPath)}}
@name={{if field.isRequired (concat field.fieldIdentifier "_required_" index) (concat field.fieldIdentifier "_" index)}} />
{{/if}}
{{/if}}
Expand Down
27 changes: 27 additions & 0 deletions app/templates/components/forms/wizard/attendee-step.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
<th class="right aligned">
{{t 'Option'}}
</th>
<th class="center aligned">
{{t 'Type'}}
</th>
<th class="center aligned">
{{t 'Include'}}
</th>
Expand All @@ -63,6 +66,9 @@
{{field.name}}
</label>
</td>
<td class="center aligned">
{{field.type}}
</td>
<td class="center aligned">
<UiCheckbox
@class="slider"
Expand All @@ -86,6 +92,27 @@
</div>
</div>
{{/if}}
<div class="ui hidden divider"></div>
<h3 class="ui dividing header">
<i class="checkmark box icon"></i>
<div class="content">
{{t 'Add Custom Form Field'}}
</div>
</h3>
<div class="ui action input">
<Input type="text" placeholder="Field Name" @value={{this.data.newFormField.name}} />
<UiDropdown class="ui selection dropdown" @selected={{this.data.newFormField.type}} @onChange={{action (mut this.data.newFormField.type)}}>
<div class="default text">
{{ this.data.newFormField.type}}
</div>
<i class="dropdown icon"></i>
<div class="menu">
<div class="item" data-value="text">Text</div>
<div class="item" data-value="number">Number</div>
</div>
</UiDropdown>
<button class="ui button" {{action 'addFormField'}} disabled={{not this.validIdentifier}}>Add</button>
</div>
<div class="spacer-50"></div>
<div class="{{if this.device.isMobile 'mini four' 'right floated large'}} ui fields buttons">
<button class="ui three field left labeled icon button {{if this.isLoading 'disabled'}}" type="button" {{action 'move' 'backwards'}}>
Expand Down
6 changes: 6 additions & 0 deletions app/utils/text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function slugify(string: string, sep: string = '-'): string {
return string.toLowerCase()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.replace(/[^\w\s-]/g, '') // remove non-word [a-z0-9_], non-whitespace, non-hyphen characters
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.replace(/[\s_-]+/g, sep) // swap any length of whitespace, underscore, hyphen characters with a single -
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.replace(/^-+|-+$/g, ''); // remove leading, trailing -
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
10 changes: 10 additions & 0 deletions tests/unit/utils/text-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { slugify } from 'open-event-frontend/utils/text';
import { module, test } from 'qunit';

module('Unit | Text | Slugify', function() {
test('test slugify', function(assert) {
assert.equal(slugify('Hello World'), 'hello-world');
assert.equal(slugify('Name and Age', '_'), 'name_and_age');
assert.equal(slugify('What is Suo Moto?'), 'what-is-suo-moto');
});
});