2018-12-29 01:27:37 +01:00
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import { alias } from '@ember/object/computed';
|
|
|
|
import Controller from '@ember/controller';
|
2018-12-31 11:47:36 +01:00
|
|
|
import { isPresent } from '@ember/utils';
|
2020-01-18 10:13:50 +01:00
|
|
|
import { action, computed } from '@ember/object';
|
2018-12-31 11:47:36 +01:00
|
|
|
import answersForAnswerType from 'croodle/utils/answers-for-answer-type';
|
2015-11-19 21:51:49 +01:00
|
|
|
import {
|
|
|
|
validator, buildValidations
|
|
|
|
}
|
|
|
|
from 'ember-cp-validations';
|
2016-02-22 23:55:59 +01:00
|
|
|
import moment from 'moment';
|
2014-10-30 21:44:22 +01:00
|
|
|
|
2016-01-28 22:49:14 +01:00
|
|
|
const Validations = buildValidations({
|
2016-05-23 12:42:47 +02:00
|
|
|
anonymousUser: validator('presence', {
|
|
|
|
presence: true,
|
2020-01-30 00:23:12 +01:00
|
|
|
dependentKeys: ['model.intl.locale']
|
2016-05-23 12:42:47 +02:00
|
|
|
}),
|
2015-11-19 21:51:49 +01:00
|
|
|
answerType: [
|
2016-05-23 12:42:47 +02:00
|
|
|
validator('presence', {
|
|
|
|
presence: true,
|
2020-01-30 00:23:12 +01:00
|
|
|
dependentKeys: ['model.intl.locale']
|
2016-05-23 12:42:47 +02:00
|
|
|
}),
|
2015-11-19 21:51:49 +01:00
|
|
|
validator('inclusion', {
|
2016-05-23 12:42:47 +02:00
|
|
|
in: ['YesNo', 'YesNoMaybe', 'FreeText'],
|
2020-01-30 00:23:12 +01:00
|
|
|
dependentKeys: ['model.intl.locale']
|
2015-11-19 21:51:49 +01:00
|
|
|
})
|
|
|
|
],
|
|
|
|
forceAnswer: validator('presence', true)
|
|
|
|
});
|
2014-07-06 17:37:54 +02:00
|
|
|
|
2020-01-18 10:13:50 +01:00
|
|
|
export default class CreateSettings extends Controller.extend(Validations) {
|
|
|
|
@service
|
|
|
|
encryption;
|
|
|
|
|
|
|
|
@service
|
2020-01-30 00:23:12 +01:00
|
|
|
intl;
|
2015-07-01 16:21:18 +02:00
|
|
|
|
2020-01-18 10:13:50 +01:00
|
|
|
@alias('model.anonymousUser')
|
|
|
|
anonymousUser;
|
2015-11-19 21:51:49 +01:00
|
|
|
|
2020-01-18 10:13:50 +01:00
|
|
|
@alias('model.answerType')
|
|
|
|
answerType;
|
|
|
|
|
|
|
|
@computed
|
|
|
|
get answerTypes() {
|
2014-07-06 18:56:51 +02:00
|
|
|
return [
|
2018-12-31 11:47:36 +01:00
|
|
|
{ id: 'YesNo', labelTranslation: 'answerTypes.yesNo.label' },
|
|
|
|
{ id: 'YesNoMaybe', labelTranslation: 'answerTypes.yesNoMaybe.label' },
|
|
|
|
{ id: 'FreeText', labelTranslation: 'answerTypes.freeText.label' },
|
2014-07-06 19:22:38 +02:00
|
|
|
];
|
2020-01-18 10:13:50 +01:00
|
|
|
}
|
2015-07-01 16:21:18 +02:00
|
|
|
|
2020-01-18 10:13:50 +01:00
|
|
|
@computed('model.expirationDate')
|
|
|
|
get expirationDuration() {
|
|
|
|
// TODO: must be calculated based on model.expirationDate
|
|
|
|
return 'P3M';
|
|
|
|
}
|
|
|
|
set expirationDuration(value) {
|
|
|
|
this.set(
|
|
|
|
'model.expirationDate',
|
|
|
|
isPresent(value) ? moment().add(moment.duration(value)).toISOString(): ''
|
|
|
|
);
|
|
|
|
return value;
|
|
|
|
}
|
2015-07-26 19:41:59 +02:00
|
|
|
|
2020-01-18 10:13:50 +01:00
|
|
|
@computed
|
|
|
|
get expirationDurations() {
|
2015-07-26 19:41:59 +02:00
|
|
|
return [
|
2018-12-31 11:47:36 +01:00
|
|
|
{ id: 'P7D', labelTranslation: 'create.settings.expirationDurations.P7D' },
|
|
|
|
{ id: 'P1M', labelTranslation: 'create.settings.expirationDurations.P1M' },
|
|
|
|
{ id: 'P3M', labelTranslation: 'create.settings.expirationDurations.P3M' },
|
|
|
|
{ id: 'P6M', labelTranslation: 'create.settings.expirationDurations.P6M' },
|
|
|
|
{ id: 'P1Y', labelTranslation: 'create.settings.expirationDurations.P1Y' },
|
|
|
|
{ id: '', labelTranslation: 'create.settings.expirationDurations.never' },
|
2015-07-26 19:41:59 +02:00
|
|
|
];
|
2020-01-18 10:13:50 +01:00
|
|
|
}
|
2015-07-26 19:41:59 +02:00
|
|
|
|
2020-01-18 10:13:50 +01:00
|
|
|
@alias('model.forceAnswer')
|
|
|
|
forceAnswer;
|
2015-11-19 21:51:49 +01:00
|
|
|
|
2020-01-18 10:13:50 +01:00
|
|
|
@action
|
|
|
|
previousPage() {
|
|
|
|
let { isFindADate } = this.model;
|
|
|
|
|
|
|
|
if (isFindADate) {
|
|
|
|
this.transitionToRoute('create.options-datetime');
|
|
|
|
} else {
|
|
|
|
this.transitionToRoute('create.options');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
async submit() {
|
|
|
|
if (!this.validations.isValid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let poll = this.model;
|
|
|
|
|
|
|
|
// set timezone if there is atleast one option with time
|
|
|
|
if (
|
|
|
|
poll.isFindADate &&
|
|
|
|
poll.options.any(({ title }) => {
|
|
|
|
return !moment(title, 'YYYY-MM-DD', true).isValid();
|
|
|
|
})
|
|
|
|
) {
|
|
|
|
this.set('model.timezone', moment.tz.guess());
|
|
|
|
}
|
|
|
|
|
|
|
|
// save poll
|
|
|
|
try {
|
|
|
|
await poll.save();
|
|
|
|
} catch(err) {
|
|
|
|
this.flashMessages.danger('error.poll.savingFailed');
|
|
|
|
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// reload as workaround for bug: duplicated records after save
|
|
|
|
await poll.reload();
|
|
|
|
|
|
|
|
// redirect to new poll
|
|
|
|
await this.transitionToRoute('poll', poll, {
|
|
|
|
queryParams: {
|
|
|
|
encryptionKey: this.encryption.key,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} catch(err) {
|
|
|
|
// TODO: show feedback to user
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
updateAnswerType(answerType) {
|
|
|
|
this.set('model.answerType', answerType);
|
|
|
|
this.set('model.answers', answersForAnswerType(answerType));
|
|
|
|
}
|
2016-05-23 12:42:47 +02:00
|
|
|
|
|
|
|
init() {
|
2020-01-18 10:13:50 +01:00
|
|
|
super.init(...arguments);
|
2018-12-29 01:27:37 +01:00
|
|
|
|
2020-01-30 00:23:12 +01:00
|
|
|
this.intl.locale;
|
2018-12-31 11:47:36 +01:00
|
|
|
}
|
2020-01-18 10:13:50 +01:00
|
|
|
}
|