-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathtransfer.js
112 lines (102 loc) · 3.4 KB
/
transfer.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
(function() {
const utils = window.expenseManager.utils;
// Cached DOM bindings
const byID = document.getElementById.bind(document);
const transferFrom = byID("transfer-form");
const descriptionEl = byID("transfer-description");
const dateEl = byID("transfer-date");
const fromAccountEl = byID("transfer-from-account");
const toAccountEl = byID("transfer-to-account");
const amountEl = byID("transfer-amount");
const saveBtn = byID("save");
const snackbarContainer = byID("toast-container");
/**
* Append transfer log to the expense sheet
*/
function save(event) {
if (!transferFrom.checkValidity()) return false;
event.preventDefault();
utils.showLoader();
const expenseDate = dateEl.value;
const descriptionVal = descriptionEl.value;
const fromAccountVal = fromAccountEl.value;
const toAccountVal = toAccountEl.value;
const amountVal = amountEl.value;
const dateObj = {
yyyy: expenseDate.substr(0, 4),
mm: expenseDate.substr(5, 2),
dd: expenseDate.substr(-2)
};
gapi.client.sheets.spreadsheets.values
.append(
utils.appendRequestObj([
[
`=DATE(${dateObj.yyyy}, ${dateObj.mm}, ${dateObj.dd})`,
descriptionVal,
fromAccountVal,
"Transfers", // category
amountVal, // expense
0, // income
true // is internal transfer?
],
[
`=DATE(${dateObj.yyyy}, ${dateObj.mm}, ${dateObj.dd})`,
descriptionVal,
toAccountVal,
"Transfers", // category
0, // expense
amountVal, // income
true // is internal transfer?
]
])
)
.then(
response => {
// reset fileds
descriptionEl.value = "";
amountEl.value = "";
snackbarContainer.MaterialSnackbar.showSnackbar({
message: "Expense added!"
});
utils.hideLoader();
},
response => {
utils.hideLoader();
let message = "Sorry, something went wrong";
if (response.status === 403) {
message = "Please copy the sheet in your drive";
}
console.log(response);
snackbarContainer.MaterialSnackbar.showSnackbar({
message,
actionHandler: () => {
window.open(
"https://github.com/mitul45/expense-manager/blob/master/README.md#how-to-get-started",
"_blank"
);
},
actionText: "Details",
timeout: 5 * 60 * 1000
});
}
);
}
function init(sheetID, accounts) {
// set date picker's defalt value as today
dateEl.value = new Date().toISOString().substr(0, 10);
accounts = accounts.sort();
// initialize accounts and categories dropdown
fromAccountEl.innerHTML = accounts.map(utils.wrapInOption).join();
toAccountEl.innerHTML = accounts.map(utils.wrapInOption).join();
// In MDL - `required` input fields are invalid on page load by default (which looks bad).
// Fix: https://github.com/google/material-design-lite/issues/1502#issuecomment-257405822
document
.querySelectorAll("*[data-required]")
.forEach(e => (e.required = true));
// set lister for `Save` button
saveBtn.onclick = save.bind(null);
}
window.expenseManager.transferForm = {
init
};
})();