Загрузить файлы в «/»
This commit is contained in:
124
habit-manager.js
Normal file
124
habit-manager.js
Normal file
@@ -0,0 +1,124 @@
|
||||
const HabitManager = {
|
||||
habits: [], // список привычек
|
||||
settings: {
|
||||
dailyGoal: 3, // цель кол-ва привычек за день
|
||||
enableNotification: true, // включить уведомления
|
||||
theme: 'light' // тип темы дизайна приложения
|
||||
}, // настройки приложения
|
||||
stats: {
|
||||
totalCompletions: 0, // общее кол-во выполненных привычек
|
||||
currentStreak: 0, // текущая серия выполненных привычек
|
||||
longestStreak: 0, // максимальная серия выполненных привычек
|
||||
level: 1, // уровень пользователя
|
||||
experience: 0 // опыт пользователя
|
||||
} // статистика пользователя
|
||||
}
|
||||
|
||||
HabitManager.createHabit = function (name, description, targetCount = 1) {
|
||||
const habit = {
|
||||
id: Math.floor(Math.random() * 100000, 10000),
|
||||
name: name,
|
||||
description: description,
|
||||
targetCount: targetCount,
|
||||
completions: [],
|
||||
currentCount: 0,
|
||||
isCompleted: false
|
||||
}
|
||||
|
||||
this.habits.push(habit)
|
||||
|
||||
return habit
|
||||
}
|
||||
|
||||
HabitManager.markHabitDone = function (habitId) {
|
||||
console.log(this.habits);
|
||||
const habit = this.habits.find(h => h.id === habitId);
|
||||
|
||||
if (!habit) {
|
||||
console.error('Привычка не найдена!');
|
||||
return null;
|
||||
}
|
||||
|
||||
const today = new Date().toDateString();
|
||||
|
||||
if (habit.completions.includes(today)) {
|
||||
alert('Вы уже выполняли эту привычку сегодня!');
|
||||
return habit;
|
||||
}
|
||||
|
||||
habit.completions.push(today);
|
||||
habit.currentCount++;
|
||||
|
||||
if (habit.currentCount >= habit.targetCount) {
|
||||
habit.isCompleted = true;
|
||||
}
|
||||
|
||||
// todo далее доработать пересчет статистики
|
||||
|
||||
return habit;
|
||||
}
|
||||
|
||||
HabitManager.saveToLocalStorage = function () {
|
||||
const data = {
|
||||
habits: this.habits,
|
||||
settings: this.settings,
|
||||
stats: this.stats
|
||||
};
|
||||
localStorage.setItem('habitTrackerData', JSON.stringify(data));
|
||||
}
|
||||
|
||||
HabitManager.loadFromLocalStorage = function () {
|
||||
const data = localStorage.getItem('habitTrackerData');
|
||||
|
||||
if (data) {
|
||||
const parsedData = JSON.parse(data);
|
||||
this.habits = parsedData.habits;
|
||||
this.settings = parsedData.settings;
|
||||
this.stats = parsedData.stats;
|
||||
}
|
||||
}
|
||||
const response = await fetch('https://back2.iro.kosipov.ru/api/habits', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
"name": "Название привычки",
|
||||
"description": "Описание",
|
||||
"targetCount": 30
|
||||
})
|
||||
});
|
||||
HabitManager.getFormData = function() {
|
||||
const name = document.getElementById('habit-name').value;
|
||||
const description = document.getElementById('habit-description').value;
|
||||
const targetCount = parseInt(document.getElementById('habit-target').value);
|
||||
|
||||
// Верните объект
|
||||
return {
|
||||
name: name,
|
||||
description: description,
|
||||
targetCount: targetCount
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
HabitManager.validateFormData = function(data) {
|
||||
// Проверка названия
|
||||
if (!data.name || data.name.trim().length === 0) {
|
||||
alert('Введите название привычки!');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (data.name.trim().length < 3) {
|
||||
alert('Название должно содержать минимум 3 символа!');
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: Добавьте проверки для targetCount
|
||||
// 1. Проверьте, что это число (используйте isNaN())
|
||||
// 2. Проверьте, что число больше 0
|
||||
|
||||
// Если все проверки прошли
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user