-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandleData.js
55 lines (53 loc) · 1.78 KB
/
handleData.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
/**
* Function that loads data from JSON in GameStorage to localStorage.
*/
export function loadGameData() {
fetch('./GameStorage/Items.json')
.then(response => response.json())
.then(data => {
localStorage.setItem('Items', JSON.stringify(data))})
fetch('./GameStorage/NPCs.json')
.then(response => response.json())
.then(data => {
localStorage.setItem('NPCs', JSON.stringify(data))})
fetch('./GameStorage/PlacePossibilities.json')
.then(response => response.json())
.then(data => {
localStorage.setItem('PlacePossibilities', JSON.stringify(data))})
fetch('./GameStorage/Player.json')
.then(response => response.json())
.then(data => {
localStorage.setItem('Player', JSON.stringify(data))})
fetch('./GameStorage/Settings.json')
.then(response => response.json())
.then(data => {
localStorage.setItem('Settings', JSON.stringify(data))})
}
/**
* Helper function that retrieves data from objects in localStorage.
*
* @param {string} dataSetTarget
* @param {string} attribute
*/
export function getData(dataSetTarget, attribute) {
let data = localStorage.getItem(`${dataSetTarget}`);
data = JSON.parse(data);
if (attribute) data = data[`${attribute}`];
return data;
}
/**
* Helper function that writes data to objects in localStorage.
*
* @param {*} newValue
* @param {string} dataSetTarget
* @param {string} attribute
*/
export function setData(newValue, dataSetTarget, attribute) {
let dataSet = typeof newValue == "object" ? newValue : localStorage.getItem(`${dataSetTarget}`);
if (attribute) {
dataSet = JSON.parse(dataSet);
dataSet[`${attribute}`] = newValue;
}
dataSet = JSON.stringify(dataSet);
localStorage.setItem(`${dataSetTarget}`, `${dataSet}`);
}