Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
216 lines (215 loc) · 7.23 KB
/
Copy pathapp.js
File metadata and controls
216 lines (215 loc) · 7.23 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
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
216
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const config = {
apiKey: "AIzaSyDnjUoBf8WKrvlufZvlxeS20iu_8rj4hmc",
authDomain: "databases-project-872ac.firebaseapp.com",
databaseURL: "https://databases-project-872ac.firebaseio.com",
projectId: "databases-project-872ac",
storageBucket: "databases-project-872ac.appspot.com",
messagingSenderId: "96462352688",
appId: "1:96462352688:web:db9dcb71d3b2d3ef4a58f5"
};
// global access to initialized app database
var db = firebase.initializeApp(config).database();
// global reference to remote data
var usersRef = db.ref("users");
var loggedInRef = db.ref("loggedinUser");
var bgColRef = db.ref("backgroundColor");
var bgImgRef = db.ref("backgroundImage");
var ingredientsRef = db.ref("ingredientsList");
var recipesRef = db.ref("recipeList");
// connect Firebase to Vue
Vue.use(VueFire);
//Main Vue instance for the app
new Vue({
el: "#my-app",
data: {
//these two are not doubling the firebase backgroundColor or backgroundImage bc CSS binding only works on the event.target.value, not the snapshot
backgroundColorVal: "",
backgroundImageVal: "",
searchQuery: "",
recipeSearch: "",
showSignIn: false,
showRegister: false,
showUserProfile: false,
showCreateRecipe: false,
numberOfColumns: 4,
checkedIngredients: [],
filteredRecipes: []
},
created() {
this.filteredRecipes = this.recipes;
},
firebase: {
users: usersRef,
// binds the below three items as objects instead of as arrays, the firebase default
loggedInUser: { source: loggedInRef, asObject: true },
backgroundColor: { source: bgColRef, asObject: true },
backgroundImage: { source: bgImgRef, asObject: true },
ingredients: ingredientsRef,
recipes: recipesRef
},
components: {
login: httpVueLoader("./components/Login.vue"),
register: httpVueLoader("./components/Register.vue"),
userprofile: httpVueLoader("./components/UserProfile.vue"),
recipe: httpVueLoader("./components/Recipe.vue"),
createrecipe: httpVueLoader("./components/CreateRecipe.vue")
},
computed: {
gridStyle() {
let numberOfColumns = Math.floor(window.screen.width / 450);
return {
gridTemplateColumns: `repeat(${numberOfColumns}, minmax(200px, 1fr))`
};
},
filteredIngredients() {
if (this.searchQuery) {
return this.ingredients.filter((item) => {
return (
item.name
.toLowerCase()
.startsWith(this.searchQuery.toLowerCase()) ||
item.category
.toLowerCase()
.startsWith(this.searchQuery.toLowerCase())
);
});
} else {
return this.ingredients;
}
},
searchRecipes() {
if (this.recipeSearch) {
return this.filteredRecipes.filter((item) => {
return (
item.RecipeName.toLowerCase().startsWith(
this.recipeSearch.toLowerCase()
) ||
item.Author.toLowerCase().startsWith(
this.recipeSearch.toLowerCase()
)
);
});
} else {
return this.filteredRecipes;
}
}
},
watch: {
checkedIngredients: function (newI, oldI) {
if (newI.length == 0) {
this.filteredRecipes = this.recipes;
} else {
let filtered = [];
let recipes = this.recipes;
for (i = 0; i < recipes.length; i++) {
if (
recipes[i].rawFood &&
recipes[i].rawFood.some(function (item) {
return newI.indexOf(item) !== -1;
})
) {
filtered.push(recipes[i]);
}
}
this.filteredRecipes = filtered;
}
}
},
methods: {
//These methods are pretty straightforward based on the name, just updating firebase values based on user changes
createUser(user) {
this.showRegister = false;
this.$firebaseRefs.users.push(user);
this.$firebaseRefs.loggedInUser.child("name").set(user.name);
this.$firebaseRefs.loggedInUser.child("email").set(user.email);
this.$firebaseRefs.loggedInUser.child("image").set(user.image);
},
openSignIn() {
this.showSignIn = true;
},
closeSignIn() {
this.showSignIn = false;
},
openRegister() {
this.showRegister = true;
},
closeRegister() {
this.showRegister = false;
},
openUserProfile() {
this.showUserProfile = true;
},
closeUserProfile() {
this.showUserProfile = false;
},
editInfo(newInfo) {
let [newString, type] = newInfo;
let currName = this.loggedInUser.name;
let currEmail = this.loggedInUser.email;
let currPic = this.loggedInUser.image;
usersRef.once('value', function(snapshot){
snapshot.forEach(function(itemSnapshot) {
if (itemSnapshot.val().name == currName && itemSnapshot.val().email == currEmail && itemSnapshot.val().image == currPic)
itemSnapshot.ref.child(type).set(newString);
loggedInRef.child(type).set(newString);
})
})
},
openCreateRecipe() {
this.showCreateRecipe = true;
},
closeCreateRecipe() {
this.showCreateRecipe = false;
},
addRecipe(rec) {
recipesRef.push(rec);
},
login(user) {
this.$firebaseRefs.loggedInUser.child("name").set(user.name);
this.$firebaseRefs.loggedInUser.child("email").set(user.email);
this.$firebaseRefs.loggedInUser.child("image").set(user.image);
this.showSignIn = false;
},
logout() {
this.$firebaseRefs.loggedInUser.child("name").set("");
this.$firebaseRefs.loggedInUser.child("email").set("");
this.$firebaseRefs.loggedInUser.child("image").set("");
this.checkedIngredients = []
this.filteredRecipes = []
this.searchQuery = ""
this.recipeSearch = ""
},
backgroundImageUpload(event) {
//FileReader handles the uploading of the file, when it loads it sets the Firebase background image
var file = event.target.files[0];
var vm = this;
var reader = new FileReader();
reader.onload = function (e) {
vm.$firebaseRefs.backgroundImage.set(e.target.result);
vm.$firebaseRefs.backgroundColor.set("");
vm.backgroundColorVal = "";
var image = db.ref("backgroundImage");
var imageVal;
image.on("value", function (snapshot) {
imageVal = snapshot.val();
});
vm.backgroundImageVal = imageVal;
//this.$firebaseRefs.backgroundImage.set(imageVal) doesn't work because the v-style binding doesn't take objects
};
reader.readAsDataURL(file);
},
setColor(event) {
// sets the background color of the project page
this.$firebaseRefs.backgroundColor.set(event.target.value);
var color = db.ref("backgroundColor");
var colorVal;
color.on("value", function (snapshot) {
colorVal = snapshot.val();
});
this.backgroundColorVal = colorVal;
this.backgroundImageVal = "";
this.$firebaseRefs.backgroundImage.set("");
}
}
});