summaryrefslogtreecommitdiff
path: root/src/store.js
blob: 51dfc91246008e113c746ad269638c626c63b35b (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    api: process.env.VUE_APP_BACKEND_URL,
    loading: false,
    loading_top_authors: false,
    commits: [],
    top_authors: [],
    emails: {},
    commit_data: {},
    author_avatar: "",
    committer_avatar: "",
  },
  getters: {
    loading: state => {
      return state.loading
    },
    loading_top_authors: state => {
      return state.loading_top_authors
    },
    commits: state => {
      return state.commits
    },
    top_authors: state => {
      return state.top_authors
    },
    emails: state => {
      return state.emails
    },
    commit: state => {
      return state.commit_data
    },
    author_avatar: state => {
      return state.author_avatar
    },
    committer_avatar: state => {
      return state.committer_avatar
    },
  },
  mutations: {
    loading_state: (state, value) => {
      state.loading = value
    },
    loading_top_authors_state: (state, value) => {
      state.loading_top_authors = value
    },
    load_commits: (state, value) => {
      state.commits = value
    },
    load_top_authors: (state, value) => {
      state.top_authors = value
    },
    load_emails: (state, value) => {
      state.emails = value
    },
    load_commit: (state, value) => {
      state.commit_data = value
    },
    load_author_avatar: (state, value) => {
      state.author_avatar = value.hash_md5
    },
    load_committer_avatar: (state, value) => {
      state.committer_avatar = value.hash_md5
    },
  },
  actions: {
    // Get all commits from the api backend
    async get_commits({commit}, query) {
      commit('loading_state', true)
      let path = `${this.state.api}/commit/`
      if(query) {
        path += `?q=${query}`
      }
      await fetch(path)
        .then(async response => {
          commit('load_commits', await response.json());
        })
      commit('loading_state', false)
    },
    // Get all emails and map them like an hash
    async get_emails({commit}) {
      await fetch(`${this.state.api}/email/`)
        .then(async response => {
          const emails_list = await response.json();
          const emails_obj = emails_list
                                .reduce((dict, elem) => {
                                  dict[elem['email']] = elem['hash_md5']
                                  return dict;
                                }, {})

          commit('load_emails', emails_obj);
        })
    },
    /// Get the ranking of commits authors
    async get_top_authors({commit}) {
      commit('loading_top_authors_state', true)
      await fetch(`${this.state.api}/commit/top/`)
        .then(async response => {
          commit('load_top_authors', await response.json());
        })
      commit('loading_top_authors_state', false)
    },
    // Get commit by hash
    async get_commit({commit}, hash) {
      await fetch(`${this.state.api}/commit/${hash}/`)
        .then(async response => {
          commit('load_commit', await response.json());
        })
    },
    // Get all commits from a repository
    async get_repo_commits({commit}, data) {
      commit('loading_state', true)
      let path = `${this.state.api}/commit/?repository_user=${data.user}&repository_name=${data.name}`
      await fetch(path)
        .then(async response => {
          commit('load_commits', await response.json());
        })
      commit('loading_state', false)
    },
    // Get email
    async get_email({commit}, data) {
      await fetch(`${this.state.api}/email/search/?q=${data.email}`)
        .then(async response => {
          if(await response.status == 200) {
            commit(`load_${data.type}_avatar`, await response.json());
          }
        })
    },
    // Set committer avatar
    async set_committer({commit}, avatar) {
      commit('load_committer_avatar', avatar);
    },
    // Add new repository
    async add_repo({commit}, payload) {
      commit('loading_state', true)
      let res
      await fetch(`${this.state.api}/repo/`, {
          method: "POST",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify(payload),
        })
        .then(async response => {
          res = await response.json()
        })
      commit('loading_state', false)
      return res
    },
    // Set loading state
    async set_loading({commit}, status) {
      commit('loading_state', status);
    }
  },
  modules: {
  }
})