Skip to content

Commit 6881639

Browse files
committed
🐕
1 parent 76aa06d commit 6881639

File tree

8 files changed

+221
-104
lines changed

8 files changed

+221
-104
lines changed

src/App.vue

+12-20
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,8 @@
1717
</template>
1818

1919
<script>
20-
import { getDatabase, ref, onValue } from "firebase/database";
2120
export default {
2221
name: "app",
23-
data() {
24-
return {
25-
name: "",
26-
pets: [],
27-
};
28-
},
29-
methods: {
30-
getPets() {
31-
const db = getDatabase();
32-
const petsRef = ref(db, "pets");
33-
onValue(petsRef, (snapshot) => {
34-
const data = snapshot.val();
35-
this.pets = Object.keys(data).map((key) => ({
36-
id: key,
37-
...data[key],
38-
}));
39-
});
40-
},
41-
},
4222
};
4323
</script>
4424

@@ -94,6 +74,18 @@ p {
9474
text-align: left;
9575
}
9676
77+
input,
78+
select {
79+
border: 1px solid #ff738c;
80+
border-radius: 4px;
81+
padding: 5px;
82+
box-sizing: border-box;
83+
84+
&:focus-visible {
85+
outline: 2px solid #ff738c;
86+
}
87+
}
88+
9789
.primary-btn {
9890
font-family: "Barlow Semi Condensed", sans-serif;
9991
background-color: #ff738c;

src/components/ChoosePet.vue

+10-6
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,21 @@
2020
<script>
2121
export default {
2222
name: "ChoosePet",
23-
data() {
24-
return {
25-
pets: this.$store.state.pets,
26-
};
23+
computed: {
24+
pets() {
25+
return this.$store.state.pets;
26+
},
27+
},
28+
beforeMount() {
29+
this.$store.dispatch("getPets");
2730
},
2831
methods: {
2932
goToBrowse(id) {
30-
this.$store.commit("setPetId", id);
33+
this.$store.dispatch("getPetData", id);
3134
this.$router.push({ name: "browse", params: { petId: id } });
3235
},
3336
goToAddPet() {
34-
this.$store.commit("clearPetId");
37+
this.$store.commit("clearPetData");
3538
this.$router.push({ name: "new" });
3639
},
3740
},
@@ -53,6 +56,7 @@ export default {
5356
column-gap: 20px;
5457
box-sizing: border-box;
5558
padding-inline: 20px;
59+
row-gap: 20px;
5660
}
5761
.pet {
5862
border: 2px solid #ff738c;

src/components/NewPet.vue

+11-16
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<input type="text" name="color" v-model="form.color" />
4343
</div>
4444
<div class="block">
45-
<label class="label" for="weight">Weight</label>
45+
<label class="label" for="weight">Weight (kg)</label>
4646
<input type="number" name="weight" v-model="form.weight" />
4747
</div>
4848
<div class="block">
@@ -53,6 +53,10 @@
5353
<label class="label" for="birthday">Birthday</label>
5454
<input type="date" name="birthday" v-model="form.birthday" />
5555
</div>
56+
<div class="block">
57+
<label class="label" for="image">Image url</label>
58+
<input type="text" name="image" v-model="form.image" />
59+
</div>
5660
<button type="button" @click="checkForm" class="primary-btn">Save</button>
5761
</form>
5862
</div>
@@ -80,13 +84,16 @@ export default {
8084
? this.$store.state.currentPet.color
8185
: "",
8286
weight: this.$store.state.currentPet
83-
? this.$store.state.currentPet.currentWeight
87+
? this.$store.state.currentPet.weight
8488
: "",
8589
chipNumber: this.$store.state.currentPet
8690
? this.$store.state.currentPet.chipNumber
8791
: "",
8892
birthday: this.$store.state.currentPet
89-
? this.$store.state.currentPet.birthDate
93+
? this.$store.state.currentPet.birthday
94+
: "",
95+
image: this.$store.state.currentPet
96+
? this.$store.state.currentPet.image
9097
: "",
9198
},
9299
error: "",
@@ -96,8 +103,7 @@ export default {
96103
checkForm() {
97104
if (this.form.name) {
98105
this.error = "";
99-
// save form
100-
console.log(this.form);
106+
this.$store.dispatch("savePet", this.form);
101107
this.$router.push({ name: "choose-pet" });
102108
} else {
103109
this.error = "Name is required.";
@@ -127,17 +133,6 @@ export default {
127133
inline-size: 100%;
128134
text-align: left;
129135
}
130-
input,
131-
select {
132-
border: 1px solid #ff738c;
133-
border-radius: 4px;
134-
padding: 5px;
135-
box-sizing: border-box;
136-
137-
&:focus-visible {
138-
outline: 2px solid #ff738c;
139-
}
140-
}
141136
.primary-btn {
142137
inline-size: 100%;
143138
}

src/components/PetInfo.vue

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div class="main-container">
3-
<ul>
3+
<ul v-if="currentPet">
44
<li>
55
<div class="label">Name</div>
66
<div class="value">{{ currentPet.name }}</div>
@@ -23,15 +23,15 @@
2323
</li>
2424
<li>
2525
<div class="label">Weight</div>
26-
<div class="value">{{ currentPet.currentWeight }}kg</div>
26+
<div class="value">{{ currentPet.weight }}kg</div>
2727
</li>
2828
<li>
2929
<div class="label">Chip number</div>
3030
<div class="value">{{ currentPet.chipNumber }}</div>
3131
</li>
3232
<li>
3333
<div class="label">Birthday</div>
34-
<div class="value">{{ currentPet.birthDate }}</div>
34+
<div class="value">{{ currentPet.birthday }}</div>
3535
</li>
3636
</ul>
3737
<router-link to="/new" class="primary-btn">Edit info</router-link>

src/components/WeightData.vue

+73-4
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,81 @@
33
<ul>
44
<li>
55
<div class="label">Date</div>
6-
<div class="value">Weight</div>
6+
<div class="label">Weight</div>
77
</li>
88
<li v-for="weight in weightData" :key="weight.id">
9-
<div class="label">{{ weight.date }}</div>
9+
<div class="value">{{ weight.date }}</div>
1010
<div class="value">{{ weight.weight }}kg</div>
1111
</li>
1212
</ul>
13-
<button type="button" class="primary-btn">Add weight</button>
13+
<form class="form" v-if="showForm">
14+
<div class="block">
15+
<label class="label hide" for="date">Birthday</label>
16+
<input type="date" name="date" v-model="form.date" />
17+
</div>
18+
<div class="block">
19+
<label class="label hide" for="weight">Weight *</label>
20+
<input type="number" name="weight" v-model="form.weight" required />
21+
<div v-if="error" class="error">
22+
<p>{{ error }}</p>
23+
</div>
24+
</div>
25+
</form>
26+
<button
27+
v-if="!showForm"
28+
type="button"
29+
class="primary-btn"
30+
@click="toggleForm()"
31+
>
32+
Add weight
33+
</button>
34+
<button
35+
v-if="showForm"
36+
type="button"
37+
class="primary-btn"
38+
@click="addWeight()"
39+
>
40+
Save
41+
</button>
1442
</div>
1543
</template>
1644

1745
<script>
1846
export default {
1947
name: "PetInfo",
48+
computed: {
49+
weightData() {
50+
return this.$store.state.currentPetWeight;
51+
},
52+
},
2053
data() {
2154
return {
22-
weightData: this.$store.state.weightData,
55+
form: {
56+
date: new Date().toISOString().slice(0, 10),
57+
weight: "",
58+
},
59+
showForm: false,
60+
error: "",
2361
};
2462
},
63+
methods: {
64+
toggleForm() {
65+
this.showForm = !this.showForm;
66+
},
67+
addWeight() {
68+
if (this.form.weight) {
69+
this.$store.dispatch("saveWeight", {
70+
date: this.form.date,
71+
weight: this.form.weight,
72+
});
73+
this.toggleForm();
74+
this.form.date = new Date().toISOString().slice(0, 10);
75+
this.form.weight = "";
76+
} else {
77+
this.error = "Weight is required";
78+
}
79+
},
80+
},
2581
};
2682
</script>
2783

@@ -47,6 +103,19 @@ li {
47103
}
48104
.label {
49105
font-weight: bold;
106+
107+
&.hide {
108+
position: absolute;
109+
top: -9999px;
110+
}
111+
}
112+
.form {
113+
display: flex;
114+
justify-content: space-between;
115+
inline-size: 100%;
116+
padding-inline: 20px;
117+
box-sizing: border-box;
118+
margin-block-start: 10px;
50119
}
51120
.primary-btn {
52121
inline-size: calc(100% - 40px);

src/firebaseInit.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import { initializeApp } from "firebase/app";
22

33
// Your web app's Firebase configuration
44
export const firebaseApp = initializeApp({
5-
apiKey: xx,
6-
authDomain: xx,
7-
projectId: xx,
8-
storageBucket: xx,
9-
messagingSenderId: xx,
10-
appId: xx,
5+
apiKey: XX,
6+
authDomain: XX,
7+
projectId: XX,
8+
storageBucket: XX,
9+
messagingSenderId: XX,
10+
appId: XX,
11+
databaseURL:
12+
XX,
1113
});

src/main.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ import store from "./store/index.ts";
55
import { VueFire } from "vuefire";
66
import { firebaseApp } from "./firebaseInit";
77

8-
const app = createApp(App)
9-
.use(VueFire, { firebaseApp })
10-
.use(store)
11-
.use(router)
12-
.mount("#app");
8+
const app = createApp(App).use(VueFire, { firebaseApp }).use(store).use(router);
139

1410
app.mount("#app");

0 commit comments

Comments
 (0)