forked from DevBramz/drf-shopping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.html
130 lines (128 loc) · 4.41 KB
/
client.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Awesome shopping app</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" />
<style>
.strikethrough {
text-decoration: line-through;
}
.clickable {
cursor: pointer;
}
</style>
</head>
<body>
<div id="app">
<div class="container m-auto mt-4" style="max-width: 700px">
<ul class="nav nav-tabs">
<li
class="nav-item"
v-for="(shopping_list, index) in all_shopping_lists"
:key="'nav_' + shopping_list.id"
>
<a
class="nav-link"
data-bs-toggle="tab"
:href="'#content_' + shopping_list.id"
:class="{ active: index === 0 }"
>
{{shopping_list.name}}
</a>
</li>
</ul>
<div class="tab-content mt-4">
<div
:id="'content_' + shopping_list.id"
:class="{ active: index === 0, show: index === 0 }"
v-for="(shopping_list, index) in all_shopping_lists"
:key="'content_' + shopping_list.id"
class="tab-pane fade in"
>
<ul class="list-group">
<li
class="clickable list-group-item"
v-for="shopping_item in shopping_list.shopping_items"
:key="shopping_item.id"
:class="{strikethrough: shopping_item.purchased}"
@click.prevent="toggleShoppingItemStatus(shopping_list.id, shopping_item)"
>
{{shopping_item.name}}
</li>
</ul>
<div>
<form @submit.prevent="addShoppingItem(shopping_list, new_shopping_item)">
<input type="text"
v-model="new_shopping_item"
placeholder="Add a new item"
class="list-group-item border-top-0"
style="width: 100%"
>
</form>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<script>
const vm = new Vue({
el: "#app",
data: {
all_shopping_lists: [],
new_shopping_item: "",
},
methods: {
addShoppingItem(shopping_list, new_shopping_item) {
if (new_shopping_item.length < 1) return;
const postData = { name: new_shopping_item, purchased: false };
const postUrl =
"http://127.0.0.1:8000/api/shopping-lists/" +
shopping_list.id +
"/shopping-items/";
axios.post(postUrl, postData).then(
(res) => {
this.all_shopping_lists
.find((x) => x.id === shopping_list.id)
.shopping_items.push({
name: new_shopping_item,
purchased: false,
id: res.data.id,
});
this.new_shopping_item = "";
},
(res) => {
console.log("Connection unavailable at the moment", res);
}
);
},
toggleShoppingItemStatus(shopping_list_id, shopping_item) {
const url =
"http://127.0.0.1:8000/api/shopping-lists/" +
shopping_list_id +
"/shopping-items/" +
shopping_item.id +
"/";
const postData = { purchased: !shopping_item.purchased };
axios.patch(url, postData).then(
() => {
shopping_item.purchased = !shopping_item.purchased;
},
(res) => {
console.log("Connection unavailable at the moment", res.errors);
}
);
},
},
mounted() {
axios.get("http://127.0.0.1:8000/api/shopping-lists/").then((response) => {
this.all_shopping_lists = response.data;
});
},
});
</script>
</body>
</html>