Blame view

src/Services/ApiCalls.js 6.01 KB
6f7cf8cf5   Amber Dev   added discussion ...
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
  import http from "@/Services/http.js";
  import Rules from "@/pages/Common/rules.js"
  export default {
      mixins: [Rules],
      data() {
          return {
  
              // LOADER
              showLoader: false,
              loading: false,
              // SNACKBAR
              snackbar: false,
              snackbarColor: '',
              text: '',
              timeout: 4000,
              y: "top",
              x: "right",
              mode: "",
  
              // PAGINATION
              size: 10,
              elements: "",
              pageCount: "",
              pageNumber: 0
  
          }
      },
      methods: {
          // SNACKBAR
          seeSnackbar(message, color) {
              this.text = message;
              this.showLoader = false;
              this.snackbarColor = color;
              this.snackbar = true;
          },
  
          // SET PAGINATION
          setPagination(response) {
              this.elements = response.data.totalElements;
              var l = this.elements;
              var s = this.size;
              var floor = (l / s);
              this.pageCount = Math.floor(floor);
              if (this.pageCount == floor) {
                  this.pageCount -= 1
              }
              this.showLoader = false;
              this.loading = false;
          },
  
          generateError(error) {
              var customError
              const errorNo = error.response.status
              // console.log("satus code errorNo", errorNo)
              switch (errorNo) {
                  case 400:
                      let er = error
                      if (er.response.data.error) {
                          customError = error.response.data.error
                      }
                      if (er.response.data.errorMessage) {
                          customError = error.response.data.errorMessage
                      }
                      if (er.response.data.message) {
                          customError = error.response.data.message
                      }
                      return customError
                      break;
                  case 401:
                      customError = "Session expired"
                      return customError
                      break;
                  case 404:
                      customError = error.response.data.errorMessage
                      return customError
                      break;
                  case 409:
                      customError = error.response.data.errorMessage
                      return customError
                      break;
                  case 500:
                      customError = error.response.data.message
                      return customError
                      break;
                  default:
                      customError = "unknown error"
                      return customError
              }
  
          },
  
          // GET
          getMethod(url, params) {
              this.showLoader = true
              return http()
                  .get(url, {
                      params: params,
  
                      headers: {
                          // headers: { Authorization: "Bearer " + this.token }
                      }
                  })
                  .then(response => {
                      this.showLoader = false
                      return response
                  })
                  .catch(error => {
                      this.showLoader = false
                      // console.log("error in getMethod = ", error.response)
                      const message = this.generateError(error)
                      if (message == "Session expired") {
0a5256600   Amber Dev   added events
112
113
114
115
                          // this.$store.dispatch("setToken", null);
                          // this.$router.push({
                          //     name: 'Login'
                          // });
6f7cf8cf5   Amber Dev   added discussion ...
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
                      } else {
                          throw new Error(message);
                      }
  
                  });
          },
  
          // POST
          postMethod(url, payload) {
              this.showLoader = true
              return http()
                  .post(url, payload, {
                      // headers: { Authorization: "Bearer " + this.token }
                  })
                  .then(response => {
                      this.showLoader = false
                      return response
                  })
                  .catch(error => {
                      this.showLoader = false
                      console.log("error in postMethod = ", error.response)
                      const message = this.generateError(error)
                      if (message == "Session expired") {
                          this.$store.dispatch("setToken", null);
                          this.$router.push({
                              name: 'Login'
                          });
                      } else {
                          throw new Error(message);
                      }
  
                  });
          },
  
          // PUT
          putMethod(url, payload) {
              return http()
                  .put(url, payload, {
                      // headers: {
                      //     // Authorization: 'Bearer ' + this.token
                      // }
                  })
                  .then(response => {
                      return response
                  })
                  .catch(error => {
                      console.log("error in putMethod = ", error.response)
                      const message = this.generateError(error)
                      if (message == "Session expired") {
                          this.$store.dispatch("setToken", null);
                          this.$router.push({
                              name: 'Login'
                          });
                      } else {
                          throw new Error(message);
                      }
  
                  })
          }
  
      },
      computed: {
          displayedPageNumber() {
              return this.pageNumber + 1;
          },
          // managerIsADMIN_VIEW: {
          //     get() {
          //         if (this.$store.state.roleName == "ADMIN_VIEW") {
          //             return true;
          //         } else {
          //             return false
          //         }
          //     },
          //     set(newValue) {
          //         this.managerIsADMIN_VIEW = newValue;
          //     }
  
          // }
      },
  }