summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/boards/services/board_service.js.es6
blob: 1ee0a9ead00154307c5b9e7d0f2b185437ff849e (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
class BoardService {
  constructor (root) {
    Vue.http.options.root = root;

    this.lists = Vue.resource(`${root}{/id}.json`, {});
    this.list = Vue.resource(`${root}/lists{/id}.json`, {}, {
      generate: {
        method: 'POST',
        url: `${root}/lists/generate.json`
      }
    });
    this.issue = Vue.resource(`${root}/issues{/id}.json`, {});
    this.issues = Vue.resource(`${root}/lists{/id}/issues.json`, {});
  }

  setCSRF () {
    Vue.http.headers.common['X-CSRF-Token'] = $.rails.csrfToken();
  }

  all () {
    this.setCSRF();
    return this.lists.get();
  }

  generateDefaultLists () {
    this.setCSRF();

    return this.list.generate({});
  }

  createList (labelId) {
    this.setCSRF();

    return this.list.save({}, {
      list: {
        label_id: labelId
      }
    });
  }

  updateList (list) {
    this.setCSRF();

    return this.list.update({ id: list.id }, {
      list: {
        position: list.position
      }
    });
  }

  destroyList (id) {
    this.setCSRF();

    return this.list.delete({ id });
  }

  getIssuesForList (id, filter = {}) {
    let data = { id };
    Object.keys(filter).forEach((key) => { data[key] = filter[key]; });
    this.setCSRF();

    return this.issues.get(data);
  }

  moveIssue (id, from, to) {
    return this.issue.update({ id }, {
      issue: {
        from,
        to
      }
    });
  }
};