Skip to content

Commit 83510dd

Browse files
authored
Move vue-resource to axios and consolidate (#1157)
* Move all js and vue to axios * run yarn install * fix lint * Fix archive pricelists & make laungue clearer * update yarrn.lock * fix ai suggestions
1 parent b66fba4 commit 83510dd

19 files changed

Lines changed: 89 additions & 323 deletions

app/javascript/activities.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import Vue from 'vue/dist/vue.esm';
2-
import VueResource from 'vue-resource';
3-
4-
Vue.use(VueResource);
52

63
document.addEventListener('turbo:load', () => {
7-
Vue.http.headers.common['X-CSRF-TOKEN'] = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
84
const element = document.getElementById('new_activity_modal');
95
if (element) {
106
const priceLists = JSON.parse(element.dataset.priceLists);

app/javascript/activity.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import Vue from 'vue/dist/vue.esm';
2-
import VueResource from 'vue-resource';
32

43
import ProductTotals from './components/activity/ProductTotals.vue';
54

6-
Vue.use(VueResource);
75
let vueInstance = null;
86

97
document.addEventListener('turbo:before-cache', () => {
@@ -14,7 +12,6 @@ document.addEventListener('turbo:before-cache', () => {
1412
});
1513

1614
document.addEventListener('turbo:load', () => {
17-
Vue.http.headers.common['X-CSRF-TOKEN'] = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
1815
const element = document.getElementById('activity');
1916
if (element) {
2017
vueInstance = new Vue({
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import axios from 'axios';
2+
3+
/**
4+
* Get CSRF token from meta tag
5+
* @returns {string} CSRF token
6+
*/
7+
function getCsrfToken() {
8+
const token = document.querySelector('meta[name="csrf-token"]');
9+
return token ? token.getAttribute('content') : '';
10+
}
11+
12+
/**
13+
* Create and configure axios instance.
14+
* CSRF is injected via a request interceptor so the latest token is used.
15+
* @returns {AxiosInstance} Configured axios instance
16+
*/
17+
function createAxiosInstance() {
18+
const instance = axios.create();
19+
20+
// Always ask for JSON responses
21+
instance.defaults.headers.common['Accept'] = 'application/json';
22+
23+
instance.interceptors.request.use((config) => {
24+
const csrfToken = getCsrfToken();
25+
if (csrfToken) {
26+
config.headers['X-CSRF-Token'] = csrfToken;
27+
}
28+
return config;
29+
});
30+
31+
return instance;
32+
}
33+
34+
const axiosInstance = createAxiosInstance();
35+
36+
export default axiosInstance;
37+
export { getCsrfToken, axiosInstance };

app/javascript/components/ProductTable.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
</template>
4343

4444
<script>
45-
import axios from 'axios';
45+
import api from '../api/axiosInstance';
4646
4747
export default {
4848
props: {
@@ -85,7 +85,7 @@ export default {
8585
order: orderData
8686
};
8787
88-
axios.patch(`/orders/${this.order.id}`, payload).then((response) => {
88+
api.patch(`/orders/${this.order.id}`, payload).then((response) => {
8989
const updatedOrder = response.data;
9090
9191
this.$emit('updateordertotal', this.order, updatedOrder.order_total);

app/javascript/components/UserInput.vue

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template lang="html">
22
<div class="user-input position-relative">
33
<input type="text" class="d-none" :name="name" :value="selectedSuggestion.id">
4-
<input type="text" class="form-control bg-white" v-model="query" placeholder="Begin met typen..."
4+
<input type="text" class="form-control bg-white" v-model="query" placeholder="Zoek op gebruiker"
55
@input="updateValue"
66
@keydown.enter="selectFirstSuggestion"
77
aria-haspopup="true" v-bind:aria-expanded="dropdownOpened"
@@ -19,6 +19,8 @@
1919
</template>
2020

2121
<script>
22+
import api from '../api/axiosInstance';
23+
2224
export default {
2325
props: {
2426
name: {
@@ -76,8 +78,8 @@ export default {
7678
return;
7779
}
7880
79-
this.$http.post('/users/search.json', { query: this.query }).then( (response) => {
80-
let results = response.body || [];
81+
api.post('/users/search.json', { query: this.query }).then( (response) => {
82+
let results = response.data || [];
8183
8284
if (this.includePin && 'gepind'.indexOf(this.query.toLowerCase()) >= 0) {
8385
results.push({ name: 'Gepind', paid_with_pin: true });

app/javascript/components/activity/ProductTotals.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
</template>
5151

5252
<script>
53+
import api from '../../api/axiosInstance';
5354
import UserInput from '../UserInput.vue';
5455
5556
export default {
@@ -77,8 +78,8 @@ export default {
7778
this.isLoading = true;
7879
7980
let params = {user: this.user.id, paid_with_cash: this.user.paid_with_cash, paid_with_pin: this.user.paid_with_pin};
80-
this.$http.get(`/activities/${this.activity}/product_totals`, { params }).then((response) => {
81-
this.orderTotals = response.body;
81+
api.get(`/activities/${this.activity}/product_totals`, { params }).then((response) => {
82+
this.orderTotals = response.data;
8283
this.totalAmount = this.orderTotals.reduce((a, b) => a + parseFloat(b.price), 0.0);
8384
this.isLoading = false;
8485
}).catch((error) => {

app/javascript/components/orderscreen/ActivityOrders.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
</template>
6262

6363
<script>
64-
import axios from 'axios';
64+
import api from '../../api/axiosInstance';
6565
import moment from 'moment';
6666
import ProductTable from '../ProductTable.vue';
6767
@@ -97,7 +97,7 @@ export default {
9797
params = { user_id: this.user.id };
9898
}
9999
100-
let promise = axios.get('/orders', { params });
100+
let promise = api.get('/orders', { params });
101101
102102
return promise.then((response) => {
103103
const orders = response.data;

app/javascript/components/user/ActivityOrderHistory.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
<script>
1919
import ProductTable from '../ProductTable.vue';
20-
import axios from 'axios';
20+
import api from '../../api/axiosInstance';
2121
import moment from 'moment';
2222
2323
export default {
@@ -42,7 +42,7 @@ export default {
4242
created() {
4343
let params = { user_id: this.user.id, activity_id: this.activity.id };
4444
45-
let promise = axios.get('/orders', { params });
45+
let promise = api.get('/orders', { params });
4646
4747
promise.then((response) => {
4848
this.orders = response.data.sort((a, b) => (a.created_at > b.created_at) ? 1 : -1);

app/javascript/components/user/OrderHistory.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555

5656
<script>
5757
import ActivityOrderHistory from './ActivityOrderHistory.vue';
58-
import axios from 'axios';
58+
import api from '../../api/axiosInstance';
5959
import moment from 'moment';
6060
6161
export default {
@@ -76,7 +76,7 @@ export default {
7676
methods: {
7777
activityProvider() {
7878
this.isLoading = true;
79-
let promise = axios.get('/users/'+this.user.id+'/activities');
79+
let promise = api.get('/users/'+this.user.id+'/activities');
8080
8181
promise.then((response) => {
8282
let activities = response.data;

app/javascript/credit_mutations.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import Vue from 'vue/dist/vue.esm';
2-
import VueResource from 'vue-resource';
32

43
import UserInput from './components/UserInput.vue';
54

6-
Vue.use(VueResource);
7-
85
document.addEventListener('turbo:load', () => {
9-
Vue.http.headers.common['X-CSRF-TOKEN'] = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
106

117
const element = document.getElementById('new_mutation_modal');
128
if (element != null) {

0 commit comments

Comments
 (0)