-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient_chapter_9.html
More file actions
168 lines (155 loc) · 5.28 KB
/
Copy pathclient_chapter_9.html
File metadata and controls
168 lines (155 loc) · 5.28 KB
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
160
161
162
163
164
165
166
167
168
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Awesome Shopping App (Chapter 9)</title>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
/>
<style>
.strikethrough {
text-decoration: line-through;
opacity: 0.55;
}
.clickable {
cursor: pointer;
}
[v-cloak] {
display: none;
}
</style>
<script type="importmap">
{
"imports": {
"vue": "https://cdn.jsdelivr.net/npm/vue@3/dist/vue.esm-browser.prod.js"
}
}
</script>
</head>
<body class="bg-body-tertiary">
<div id="app" v-cloak>
<div class="container py-4" style="max-width: 720px">
<header class="mb-4">
<h1 class="h4 m-0">🛒 Awesome Shopping App</h1>
</header>
<p v-if="lists.length === 0" class="text-body-secondary">
You have no shopping lists yet. Add some in the Django admin.
</p>
<template v-else>
<!-- Vue-driven tabs (no Bootstrap JS needed). -->
<ul class="nav nav-tabs">
<li v-for="list in lists" :key="list.id" class="nav-item">
<button
class="nav-link"
type="button"
:class="{ active: list.id === activeListId }"
@click="activeListId = list.id"
>
{{ list.name }}
</button>
</li>
</ul>
<div class="border border-top-0 rounded-bottom p-3 bg-body">
<ul class="list-group mb-3">
<li
v-for="item in activeList.shopping_items"
:key="item.id"
class="clickable list-group-item d-flex align-items-center"
:class="{ strikethrough: item.purchased }"
@click="toggleItem(item)"
>
<input
class="form-check-input me-2"
type="checkbox"
:checked="item.purchased"
@click.prevent
/>
{{ item.name }}
</li>
<li
v-if="activeList.shopping_items.length === 0"
class="list-group-item text-body-secondary"
>
This list is empty.
</li>
</ul>
<form @submit.prevent="addItem">
<input
v-model="newItemName"
type="text"
class="form-control"
placeholder="Add a new item and press Enter"
/>
</form>
</div>
</template>
</div>
</div>
<script type="module">
import { createApp, ref, computed } from "vue";
// Point this at wherever your Django dev server is running.
const API_BASE = "http://127.0.0.1:8000";
createApp({
setup() {
const lists = ref([]);
const activeListId = ref(null);
const newItemName = ref("");
const activeList = computed(() =>
lists.value.find((l) => l.id === activeListId.value) ?? {
shopping_items: [],
}
);
// Retrieve all shopping lists. Each list already includes its items
// via the nested serializer, so a single request is enough.
async function loadLists() {
// A bare fetch defaults to the GET method.
const response = await fetch(`${API_BASE}/api/shopping-lists/`);
lists.value = await response.json();
if (lists.value.length) activeListId.value = lists.value[0].id;
}
// Add a shopping item to the active list.
async function addItem() {
const name = newItemName.value.trim();
if (!name) return;
const response = await fetch(
`${API_BASE}/api/shopping-lists/${activeListId.value}/shopping-items/`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name, purchased: false }),
}
);
const created = await response.json();
activeList.value.shopping_items.push(created);
newItemName.value = "";
}
// Toggle an item's purchased status. We have the parent list's id
// from the surrounding list, so we can build the detail URL.
async function toggleItem(item) {
const response = await fetch(
`${API_BASE}/api/shopping-lists/${activeListId.value}/shopping-items/${item.id}/`,
{
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ purchased: !item.purchased }),
}
);
const updated = await response.json();
item.purchased = updated.purchased;
}
loadLists();
return {
lists,
activeListId,
activeList,
newItemName,
addItem,
toggleItem,
};
},
}).mount("#app");
</script>
</body>
</html>