-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters