Skip to content

Commit e6d33f0

Browse files
committed
[PROJ-18.5/compl] loading-saving-data
Realization of data "load/save" func's. Adding "demo.json" file. Worth noting: - self-addition of data to "localStorage/Application". FS-dev: B-3 / JS basic
1 parent 19f3215 commit e6d33f0

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[
2+
{
3+
"id": 1,
4+
"icon": "gantel-icon",
5+
"name": "Отжимания",
6+
"target": 10,
7+
"days": [
8+
{ "comment": "Первый подход всегда даётся тяжело.." },
9+
{ "comment": "Второй день немного легче!" }
10+
]
11+
},
12+
{
13+
"id": 2,
14+
"icon": "food-icon",
15+
"name": "Правильное питание",
16+
"target": 10,
17+
"days": [{ "comment": "Круто!" }]
18+
}
19+
]

full-stack-dev/3-js-basic/18-proj-habit-tracker/18-5-loading-saving-data/images/gantle-icon.svg renamed to full-stack-dev/3-js-basic/18-proj-habit-tracker/18-5-loading-saving-data/images/gantel-icon.svg

File renamed without changes.

full-stack-dev/3-js-basic/18-proj-habit-tracker/18-5-loading-saving-data/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<li class="sidebar__nav-item">
2828
<button class="sidebar__nav-btn sidebar__nav-btn_active" type="button" aria-label="Выбрать привычку"
2929
title="Отжимания">
30-
<img class="sidebar__nav-icon" src="./images/gantle-icon.svg" width="23" height="23"
30+
<img class="sidebar__nav-icon" src="./images/gantel-icon.svg" width="23" height="23"
3131
alt="Иконка: Гантеля">
3232
</button>
3333
</li>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
let habbits = [];
4+
const HABBITS_KEY = 'HABBITS';
5+
6+
// ** utility **
7+
function loadData() {
8+
const habbitsStr = localStorage.getItem(HABBITS_KEY);
9+
const habbitsArr = JSON.parse(habbitsStr);
10+
11+
if (Array.isArray(habbitsArr)) {
12+
habbits = habbitsArr;
13+
}
14+
}
15+
16+
function saveData() {
17+
localStorage.setItem(HABBITS_KEY, JSON.stringify(habbits));
18+
}
19+
20+
// загрузка данных.. по сути автоматическая/сразу (соответственно через IIFE)
21+
(() => {
22+
loadData();
23+
})();

0 commit comments

Comments
 (0)