Skip to content

Commit

Permalink
income
Browse files Browse the repository at this point in the history
  • Loading branch information
oybek committed Nov 26, 2023
1 parent 64889fa commit 6f939f2
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
129 changes: 129 additions & 0 deletions spendbook/income/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script src="https://telegram.org/js/telegram-web-app.js"></script>
<script src="../js/dropDowns.js"></script>
<head>
<link rel="stylesheet" type="text/css" href="../../css/main.css" />
</head>
<script>
$(document).ready(function () {
const searchParams = new URLSearchParams(window.location.search);
const chatId = searchParams.get("chatId");
fetch(`https://spendbook.ru/income/categories?chatId=${chatId}`)
.then((response) => response.json())
.then((userCategories) => {
const categoriesFiltered = incomeCategories.filter(
(c) => !userCategories.includes(c)
);
const finalCategories = userCategories.concat(categoriesFiltered);
$.each(finalCategories, function (key, str) {
$("#category").append(`<option value="${str}">${str}</option>`);
});
})
.catch((error) =>
$.each(incomeCategories, function (key, str) {
$("#category").append(`<option value="${str}">${str}</option>`);
})
);

function collectData() {
var category = $("#category").val();
if (category === categories[categories.length-1]) {
category = $("#custom_input").val();
}
const rawAmountValue = parseInt($("#amount").val(), 10);
const absAmountValue = Math.abs(rawAmountValue)
$("#amount").val(absAmountValue);
const data = {
category: category,
amount: absAmountValue,
description: $("#text").val(),
date: $("#date_input").val(),
timeOffset: new Date().getTimezoneOffset(),
};
Object.keys(data).forEach(
(key) =>
(data[key] =
data[key] === "any" || data[key] === "" ? null : data[key])
);
if (data.category && data.amount) {
enableSubmit(true);
} else {
enableSubmit(false);
}
return data;
}

function formatDate(date) {
var d = new Date(date),
month = "" + (d.getMonth() + 1),
day = "" + d.getDate(),
year = d.getFullYear();

if (month.length < 2) month = "0" + month;
if (day.length < 2) day = "0" + day;

return [year, month, day].join("-");
}

$("#date_input").val(formatDate(new Date()));

$("#category").on("change", collectData);
$("#custom_input").on("input", collectData);
$("#amount").on("input", collectData);
$("#date_input").on("change", collectData);

$("#custom_label").hide();
$("#custom_input").hide();
$("#category").on("change", function() {
const category = $("#category").val();
if (category === categories[categories.length-1]) {
$("#custom_label").show();
$("#custom_input").show();
} else {
$("#custom_label").hide();
$("#custom_input").hide();
}
});

const mainButton = window.Telegram.WebApp.MainButton;
mainButton.text = "Отправить";
mainButton.enable();
mainButton.onClick(function () {
window.Telegram.WebApp.sendData(JSON.stringify(collectData()));
});

function enableSubmit(activate) {
if (activate) {
mainButton.show();
} else {
mainButton.hide();
}
}
});
</script>

<body>
<div>
<form>
<label for="category">Категория дохода</label>
<select id="category" name="category"></select>

<label id="custom_label" for="custom">Своя категория</label>
<input id="custom_input" type="text" name="custom" placeholder="Своя категория"/>

<label for="amount">Сумма дохода</label>
<input type="number" id="amount" name="amount" placeholder="Сумма" />
<textarea
id="text"
name="text"
placeholder="Комментарий"
rows="3"
></textarea>
<label for="amount">Дата</label>
<input type="date" id="date_input">
</form>
</div>
</body>
</html>
9 changes: 9 additions & 0 deletions spendbook/js/dropDowns.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ const categories = [
"Ввести свою категорию",
];

const incomeCategories = [
"Зарплата",
"Кэшбек",
"Депозит",

// Должно быть последним
"Ввести свою категорию",
];

const months = [
{ text: "Январь", value: "1" },
{ text: "Февраль", value: "2" },
Expand Down

0 comments on commit 6f939f2

Please sign in to comment.