decide.nolog.cz/app/controllers/create/settings.js

147 lines
3.6 KiB
JavaScript
Raw Normal View History

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';
import { isPresent } from '@ember/utils';
import { action, computed } from '@ember/object';
import answersForAnswerType from 'croodle/utils/answers-for-answer-type';
import {
validator, buildValidations
}
from 'ember-cp-validations';
import moment from 'moment';
2014-10-30 21:44:22 +01:00
2016-01-28 22:49:14 +01:00
const Validations = buildValidations({
anonymousUser: validator('presence', {
presence: true,
dependentKeys: ['model.intl.locale']
}),
answerType: [
validator('presence', {
presence: true,
dependentKeys: ['model.intl.locale']
}),
validator('inclusion', {
in: ['YesNo', 'YesNoMaybe', 'FreeText'],
dependentKeys: ['model.intl.locale']
})
],
forceAnswer: validator('presence', true)
});
2014-07-06 17:37:54 +02:00
export default class CreateSettings extends Controller.extend(Validations) {
@service
encryption;
@service
intl;
@alias('model.anonymousUser')
anonymousUser;
@alias('model.answerType')
answerType;
@computed
get answerTypes() {
return [
{ 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
];
}
@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;
}
@computed
get expirationDurations() {
return [
{ 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' },
];
}
@alias('model.forceAnswer')
forceAnswer;
@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));
}
init() {
super.init(...arguments);
2018-12-29 01:27:37 +01:00
this.intl.locale;
}
}