summaryrefslogtreecommitdiffstats
path: root/ui/imports/ui/components/configuration/configuration.js
blob: a3582df3684bd97ab34be68942265ff825186dc6 (plain)
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
/*
 * Template Component: Configuration 
 */
    
//import { Meteor } from 'meteor/meteor'; 
import { Template } from 'meteor/templating';
import { ReactiveDict } from 'meteor/reactive-dict';
//import { SimpleSchema } from 'meteor/aldeed:simple-schema';
import * as R from 'ramda';

import { save } from '/imports/api/configurations/methods';
import { Configurations } from '/imports/api/configurations/configurations';
        
import './configuration.html';     
    
/*  
 * Lifecycles
 */   
  
Template.Configuration.onCreated(function() {
  let instance = this;
  instance.state = new ReactiveDict();
  instance.state.setDefault({
    model: Configurations.schema.clean({}),
    actionResult: 'none',
    message: null,
  });

  /*
  instance.autorun(function () {
    let data = Template.currentData();

    new SimpleSchema({
    }).validate(data);
  });
  */

  instance.autorun(function () {
    instance.subscribe('configurations?user');
    Configurations.find({user_id: Meteor.userId()}).forEach((conf) => {
      instance.state.set('model', conf);
    });
  });
});  

/*
Template.Configuration.rendered = function() {
};  
*/

/*
 * Events
 */

Template.Configuration.events({
  'click .js-submit-button': function (event, instance) {
    event.preventDefault(); 
    let msgsViewBackDelta = Number.parseInt(instance.$('.sm-msgs-view-back-delta')[0].value);
    saveForm(instance, msgsViewBackDelta);
  },

  'input .sm-msgs-view-back-delta': function (_e, instance) {
    let msgsViewBackDelta = Number.parseInt(instance.$('.sm-msgs-view-back-delta')[0].value);
    let model = instance.state.get('model');
    model = R.assoc('messages_view_backward_delta', msgsViewBackDelta, model);
    instance.state.set('model', model);
  },
});
   
/*  
 * Helpers
 */

Template.Configuration.helpers({    
  getModelField: function (fieldName) {
    let instance = Template.instance();
    return R.path([fieldName], instance.state.get('model'));
  },

  getState: function (key) {
    let instance = Template.instance();
    return instance.state.get(key);
  },

  isActionError: function () {
    let instance = Template.instance();
    return instance.state.get('actionResult') === 'error';
  },

  isActionSuccess: function () {
    let instance = Template.instance();
    return instance.state.get('actionResult') === 'success';
  },

  durationAsText: function (delta) {
    let duration = moment.duration(delta);
    let text = `${duration.years()} years, ${duration.months()} months, ${duration.days()} days, ${duration.hours()} hours and ${duration.minutes()} minutes from current time.`;
    return text;
  },
}); // end: helpers

function saveForm(instance, msgsViewBackDelta) {
  instance.state.set('actionResult', 'none');
  instance.state.set('message', null);

  save.call({
    messages_view_backward_delta: msgsViewBackDelta
  }, (error) => {
    if (error) {
      instance.state.set('actionResult', 'error');
      if (typeof error === 'string') {
        instance.state.set('message', error);
      } else {
        instance.state.set('message', error.message);
      }

      return;
    } 

    instance.state.set('actionResult', 'success');
    instance.state.set('message', 'record has been updated succesfuly');
  });
}