-
Notifications
You must be signed in to change notification settings - Fork 33
/
UserProfile.vue
215 lines (214 loc) · 9.44 KB
/
UserProfile.vue
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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<template>
<div class="user-profile">
<div class="nav-bar">
<router-link :to="{ name: 'settings' }" class="setting-icon">
<img class="setting-icon" src="./../../public/icons/settings.png">
</router-link>
<img class="profile-icon" src="./../../public/icons/profile blue.png" style="width: 6em; height: 5.5em;">
<router-link :to="{ name: 'swiping' }" class="logo">
<img class="logo-image" src="./../../public/icons/logo.png">
</router-link>
</div>
<div class="profile-container">
<h1>My Dog's Profile</h1>
<div class="my-pictures">
<img :src="image">
<form action="#" class="image-form">
<h4>Update my picture</h4>
<b-form-group label="" label-for="file-small" label-cols-sm="1" label-size="sm">
<b-form-file
@change="onFileChanged"
id="file-small"
size="sm">
</b-form-file>
</b-form-group>
<b-button type="button" @click.prevent="onUpload">Upload</b-button>
</form>
</div>
<div class="user-form">
<div class="age-name">
<div>
<h3>Name</h3>
<textarea v-model="dogName" rows="1" :placeholder="this.user.profile.dogInfo.name"></textarea>
</div>
<div>
<h3>Age</h3>
<textarea v-model="dogAge" rows="1" :placeholder="this.user.profile.dogInfo.age"></textarea>
</div>
<div>
<h3>Sex</h3>
<textarea v-model="dogSex" rows="1" :placeholder="this.user.profile.dogInfo.sex"></textarea>
</div>
</div>
<div>
<h3>Likes</h3>
<textarea v-model="likes" rows="2" :placeholder="this.user.profile.dogInfo.likes"></textarea>
</div>
<div>
<h3>Dislikes</h3>
<textarea v-model="dislikes" rows="2" :placeholder="this.user.profile.dogInfo.dislikes"></textarea>
</div>
<div>
<h3>Fun Facts</h3>
<textarea v-model="bio" rows="4" :placeholder="this.user.profile.dogInfo.fun_facts"></textarea>
</div>
<div class="save-logout">
<div class="submit">
<input type="submit" value="Save my changes" v-on:click="submit"/>
</div>
<div class="submit">
<router-link :to="{ name: 'login' }"><input value="Log Out" v-on:click="signOut"/></router-link>
</div>
</div>
</div>
<div class="myspinner" v-if="spinnerOn">
<b-spinner label="Loading..."></b-spinner>
</div>
</div>
</div>
</template>
<script>
import firebase from "firebase";
import {db} from "../main";
import {mapGetters} from "vuex";
export default {
data() {
return {
dogName: "",
dogAge: "",
dogSex: "",
likes: "",
dislikes: "",
bio: "",
spinnerOn: false,
selectedFile: null,
newImage: null
}
},
computed: {
// This gets the current logged in user from the store
...mapGetters({
user: "user"
}),
image() {
// Select the first image of the images list for the current logged in user
return this.user.profile.images[0];
}
},
methods: {
// Fires when the user clicks "save my changes"
// Updates their info in the store and in the db
submit() {
let that = this;
let uid = this.user.data.localId;
this.spinnerOn = true;
return db.collection("users")
.doc(uid)
.update({
"dogInfo.name": (this.dogName || this.user.profile.dogInfo.name),
"dogInfo.age": (this.dogAge || this.user.profile.dogInfo.age),
"dogInfo.sex": (this.dogSex || this.user.profile.dogInfo.sex),
"dogInfo.likes": (this.likes || this.user.profile.dogInfo.likes),
"dogInfo.dislikes": (this.dislikes || this.user.profile.dogInfo.dislikes),
"dogInfo.fun_facts": (this.bio || this.user.profile.dogInfo.fun_facts)
})
.then(function () {
// Set the db and then set the store
that.$store.dispatch("fetchProfile", that.user.data.localId);
that.spinnerOn = false;
that.$router.replace({name: "swiping"});
})
.catch(function (error) {
console.error("Error writing document: ", error);
that.spinnerOn = false;
});
},
// Signing out
signOut() {
firebase
.auth()
.signOut()
.then(() => {
console.log("Logged Out");
});
},
// Sets a limit on the uploaded image size
onFileChanged(event) {
if (event.target.files[0].size < 4000000) {
this.selectedFile = event.target.files[0];
} else {
alert("Image size must be 4MB or smaller");
}
},
// This uploads the selected image to the db
onUpload() {
const that = this;
let storageRef = firebase.storage().ref();
let uploadTask = storageRef
.child(this.user.data.localId + "/" + this.selectedFile.name)
.put(this.selectedFile);
// Register three observers:
// 1. 'state_changed' observer, called any time the state changes
// 2. Error observer, called on failure
// 3. Completion observer, called on successful completion
uploadTask.on(
"state_changed",
function (snapshot) {
// Observe state change events such as progress, pause, and resume
// Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
let progress =
(snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log("Upload is " + progress + "% done");
that.spinnerOn = true;
switch (snapshot.state) {
case firebase.storage.TaskState.PAUSED: // or 'paused'
console.log("Upload is paused");
break;
case firebase.storage.TaskState.RUNNING: // or 'running'
console.log("Upload is running");
break;
}
},
function (error) {
// Handle unsuccessful uploads
that.spinnerOn = false;
alert(error);
},
function () {
// Handle successful uploads on complete
// For instance, get the download URL: https://firebasestorage.googleapis.com/...
uploadTask.snapshot.ref.getDownloadURL().then(function (downloadURL) {
// api call to store DownloadURL to user profile
console.log("File available at", downloadURL);
that.newImage = downloadURL;
let userRef = db.collection("users").doc(that.user.data.localId);
return userRef
.update({
images:
[downloadURL]
})
.then(function () {
// Success of the upload
that.spinnerOn = false;
that.$store.dispatch("fetchProfile", that.user.data.localId)
})
.catch(function (error) {
// The document probably doesn't exist.
that.spinnerOn = false;
console.error("Error updating document: ", error);
});
});
}
);
}
}
}
</script>
<style scoped>
.myspinner {
position: fixed;
top: 50%;
left: 50%;
z-index: 100;
}
</style>