-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.tsx
More file actions
80 lines (67 loc) · 1.92 KB
/
Copy pathDatabase.tsx
File metadata and controls
80 lines (67 loc) · 1.92 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
import React from 'react';
import {db} from './src/config';
import firebase from 'firebase';
class Database
{
static username = "Default";
static recipeName = "Default";
static recipe = "";
static totalkCal = 0;
}
//Rudimentary login function; simply using a username
Database.login = function(username)
{
this.username = username;
}
//And a Rudimentary logout function
Database.logout = function()
{
this.username = "Default";
}
//Takes a recipe name and starts a new recipe string
Database.addRecipe = function(recipeName)
{
if(this.username == "Default")
throw("Error: not logged in");
this.recipeName = recipeName;
this.recipe = recipeName + ":\n";
}
//Concatenates ingredient data to the recipe string
Database.addIngredient = function(name, kCals)
{
if(this.username == "Default")
throw("Error: not logged in");
if(this.recipeName == "Default")
throw("Error: no working recipe");
this.recipe += name + "\n\t" + kCals + "\n";
this.totalkCal += Number(kCals);
}
//Finishes off the recipe total calorie count and uploads it to firebase
Database.uploadRecipe = function()
{
this.recipe += "Total:\n\t" +this.totalkCal;
db.ref('recipes/' + this.username + this.recipeName).set({recipe: this.recipe});
this.recipeName = "Default";
this.recipe = "";
this.totalkCal = 0;
}
//Downloads recipe Json from firebase and extracts the recipe string
Database.readRecipe = function(recipeName)
{
this.recipe = "Error: Still Loading Recipe";
db.ref('recipes/' + this.username + recipeName).get().then((snapshot) => {
if(!snapshot.exists())
this.recipe = "Error: Invalid Recipe";
else
this.recipe = snapshot.val().recipe;
});
}
//Returns the current state of the recipe, either while being built or
//after being downloaded and parsed from firebase
Database.getRecipe = function()
{
return this.recipe;
}
module.exports = {
functions: Database
};