-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalStorage.js
150 lines (131 loc) · 5.37 KB
/
localStorage.js
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
/*let Users=[
{
name:"xyz",
email: "xyz@gmail.com",
password: "xyz123",
phoneNumber: "9020504012",
expenseData: [{
expenseId:"",
expenseDate:"",
expenseName:"",
expenseAmount:"",
expenseCategory:"",
expenseDescription:""
}]
}
];*/
let Users=[
{
name:"xyz",
email: "abc@gmail.com",
password: "xyz123",
phoneNumber: "9020504012",
expenseData: []
}
]
initialSetUsers();
function getUserIndex(){
let userIndex;
let currentEmail= JSON.parse(localStorage.getItem('currentEmail'));
Users=JSON.parse(localStorage.getItem("Users"));
for(let i=0;i<Users.length;i++){
if(Users[i].email===currentEmail){
userIndex=i;
break;
}
}
return userIndex;
}
function storeExpense(id,date,name,amount,category,description){
let userIndex=getUserIndex();
Users=JSON.parse(localStorage.getItem("Users"));
let expenseData= {
expenseId:id,
expenseDate:date,
expenseName: name.toString(),
expenseAmount: amount.toString(),
expenseCategory: category.toString(),
expenseDescription: description.toString()
}
Users[userIndex].expenseData.push(expenseData);
localStorage.setItem("Users",JSON.stringify(Users));
//For setting redDot
changeCalendarLeft();
changeCalendarRight();
/*for (let i = 0; i < Users[userIndex].expenseData.length; i++) {
console.log(JSON.stringify(Users[userIndex].expenseData[i]));
}*/
}
function retrieveExpense(UserSelectDate){
let userIndex=getUserIndex();
Users=JSON.parse(localStorage.getItem("Users"));
for(let i=0;i<Users[userIndex].expenseData.length;i++){
if(Users[userIndex].expenseData[i].expenseDate===UserSelectDate){
addExpense(Users[userIndex].expenseData[i].expenseId,Users[userIndex].expenseData[i].expenseDate,Users[userIndex].expenseData[i].expenseName,Users[userIndex].expenseData[i].expenseAmount,Users[userIndex].expenseData[i].expenseCategory,Users[userIndex].expenseData[i].expenseDescription);
}
}
}
function deleteStoredExpense(ExpenseId){
let userIndex=getUserIndex();
Users=JSON.parse(localStorage.getItem("Users"));
for(let i=0;i<Users[userIndex].expenseData.length;i++){
if(Users[userIndex].expenseData[i].expenseId===ExpenseId){
Users[userIndex].expenseData.splice(i,1);
}
}
//console.log(ExpenseId);
localStorage.setItem("Users",JSON.stringify(Users));
//For setting redDot
changeCalendarLeft();
changeCalendarRight();
}
function retrieveComputedExpense(startDate,endDate,Category){
clearContainer2();
let userIndex=getUserIndex();
Users=JSON.parse(localStorage.getItem("Users"));
let startDateObj=new Date(startDate);
let endDateObj=new Date(endDate);
let totalExpenseAmount=0;
if(Category==="") {
for (let i = 0; i < Users[userIndex].expenseData.length; i++) {
let expenseDateObj = new Date(Users[userIndex].expenseData[i].expenseDate);
if ((expenseDateObj >= startDateObj) && (expenseDateObj <= endDateObj)) {
addExpense(Users[userIndex].expenseData[i].expenseId, Users[userIndex].expenseData[i].expenseDate, Users[userIndex].expenseData[i].expenseName, Users[userIndex].expenseData[i].expenseAmount, Users[userIndex].expenseData[i].expenseCategory, Users[userIndex].expenseData[i].expenseDescription);
totalExpenseAmount+=Number(Users[userIndex].expenseData[i].expenseAmount);
}
}
}
else{
for (let i = 0; i < Users[userIndex].expenseData.length; i++) {
let expenseDateObj = new Date(Users[userIndex].expenseData[i].expenseDate);
if ((expenseDateObj >= startDateObj) && (expenseDateObj <= endDateObj) && (Users[userIndex].expenseData[i].expenseCategory.toLowerCase()===Category.toLowerCase())) {
addExpense(Users[userIndex].expenseData[i].expenseId, Users[userIndex].expenseData[i].expenseDate, Users[userIndex].expenseData[i].expenseName, Users[userIndex].expenseData[i].expenseAmount, Users[userIndex].expenseData[i].expenseCategory, Users[userIndex].expenseData[i].expenseDescription);
totalExpenseAmount+=Number(Users[userIndex].expenseData[i].expenseAmount);
}
}
}
return totalExpenseAmount;
}
function updateAlertDate(id){
let userIndex=getUserIndex();
Users=JSON.parse(localStorage.getItem("Users"));
for (let i = 0; i < Users[userIndex].expenseData.length; i++) {
if (Users[userIndex].expenseData[i].expenseId===id){
let alertDate = document.getElementById('alertDate');
alertDate.textContent=Users[userIndex].expenseData[i].expenseDate;
break;
}
}
}
function redDotRequired(day,currentMonth,currentYear){
let str=['JANUARY','FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER']
let currentDay=day.toString()+" "+str[currentMonth-1]+" "+currentYear.toString();
let userIndex=getUserIndex();
Users=JSON.parse(localStorage.getItem("Users"));
for (let i = 0; i < Users[userIndex].expenseData.length; i++) {
if (Users[userIndex].expenseData[i].expenseDate===currentDay){
return true;
}
}
return false;
}