Skip to content

Commit f69dcea

Browse files
committed
webstorage is up
1 parent 32eaddf commit f69dcea

File tree

3 files changed

+82
-27
lines changed

3 files changed

+82
-27
lines changed

Diff for: LICENSE renamed to gatsby_docs/LICENSE

File renamed without changes.

Diff for: src/components/timetable/eventsComponent.js

+1-11
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,7 @@ class EventsComponent extends Component {
3434
return this.timeArrayToHours(this.timeToIntArray(time));
3535
}
3636

37-
/*
38-
Returns an unique hexadecimal string
39-
*/
40-
get_unique_identifier(){
41-
const to_hex = n => n.toString(16);
42-
let time_stamp = Date.now();
43-
time_stamp = to_hex(time_stamp);
44-
let random_suffix = Math.floor(Math.random()*1000);
45-
random_suffix = to_hex(random_suffix).padStart(4,"0");
46-
return time_stamp+random_suffix;
47-
}
37+
4838

4939

5040

Diff for: src/pages/timetable/timetable.js

+81-16
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ class Timetable extends Component {
3333
//colSetting: "col-12 col-xs-2 col-sm-4 col-lg"
3434
//this.initState = this.initState.bind(this);
3535

36-
37-
this.state = this.emptyState();
36+
this.json_format_version = "timetable_0.1";
37+
this.state = this.initState();
3838
//console.log(JSON.stringify(this.state));
3939

4040
/*
@@ -52,6 +52,60 @@ class Timetable extends Component {
5252
}
5353

5454

55+
56+
57+
/*
58+
Returns contents of local webstorage for the current json_format_version.
59+
Returns null, if there is no data for current json_format_version in webstorage.
60+
*/
61+
getWebStorage() {
62+
const WEB_STORAGE = window.localStorage;
63+
let STORAGE_CONTENTS = null;
64+
try {
65+
STORAGE_CONTENTS = WEB_STORAGE.getItem(this.json_format_version);
66+
} catch (error) {
67+
console.error(error);
68+
return null;
69+
}
70+
//console.log(STORAGE_CONTENTS);
71+
return JSON.parse(STORAGE_CONTENTS);
72+
73+
}
74+
75+
/*
76+
Saves state to local webstorage
77+
*/
78+
setWebStorage() {
79+
const WEB_STORAGE = window.localStorage;
80+
try {
81+
WEB_STORAGE.setItem(this.json_format_version, JSON.stringify(this.state));
82+
} catch(error){
83+
console.error(error);
84+
}
85+
}
86+
87+
88+
89+
90+
91+
92+
/*
93+
Initializes state with saved settings if these exist.
94+
If no saved settings exist its initalized with an empty default state.
95+
*/
96+
initState() {
97+
const STORAGE_CONTENTS = this.getWebStorage();
98+
if (STORAGE_CONTENTS === null) {
99+
return this.emptyState();
100+
}
101+
else {
102+
return STORAGE_CONTENTS;
103+
}
104+
}
105+
106+
107+
108+
55109
/*
56110
Returns an unique hexadecimal string
57111
*/
@@ -65,29 +119,32 @@ class Timetable extends Component {
65119
}
66120

67121

68-
createEvent(day_id, evt_name, evt_color, evt_time_start, evt_time_end){
69-
const EVENTS = {...this.state.events};
70-
const EVT_OBJ = {name:evt_name, color: evt_color, time_start: evt_time_start, time_end: evt_time_end};
122+
/*
123+
Creates an event and adds it to the state.
124+
*/
125+
createEvent(day_id, evt_name, evt_color, evt_time_start, evt_time_end) {
126+
const EVENTS = { ...this.state.events };
127+
const EVT_OBJ = { name: evt_name, color: evt_color, time_start: evt_time_start, time_end: evt_time_end };
71128
EVENTS[day_id][this.get_unique_identifier().toString()] = EVT_OBJ;
72-
this.setState({events: EVENTS});
129+
this.setState({ events: EVENTS });
73130
//"16fa3d39f0a0331": {"name": "Mathematics", "color": "event-blue","time_start": "12:15", "time_end": "14:35"},
74131
}
75132

76-
133+
77134
/*
78135
Deletes an event specified by an uid (unique identifier)
79136
from the state.
80137
81138
*/
82-
deleteEvent(uid){
83-
const EVENTS = {...this.state.events};
84-
Object.keys(EVENTS).forEach((day)=>{
85-
if (uid in EVENTS[day]){
139+
deleteEvent(uid) {
140+
const EVENTS = { ...this.state.events };
141+
Object.keys(EVENTS).forEach((day) => {
142+
if (uid in EVENTS[day]) {
86143
console.log(JSON.stringify(EVENTS[day][uid]));
87-
delete EVENTS[day][uid];
144+
delete EVENTS[day][uid];
88145
}
89-
});
90-
this.setState({events: EVENTS});
146+
});
147+
this.setState({ events: EVENTS });
91148
console.log(JSON.stringify(this.state));
92149

93150
}
@@ -119,6 +176,10 @@ class Timetable extends Component {
119176

120177
}
121178

179+
180+
/*
181+
Clears all events inside state.
182+
*/
122183
clearEvents() {
123184

124185
const EMPTY_EVENTS = `{
@@ -134,6 +195,9 @@ class Timetable extends Component {
134195
this.setState({ events: JSON.parse(EMPTY_EVENTS) });
135196
}
136197

198+
/*
199+
Replaces all events in state with example data.
200+
*/
137201
exampleEvents() {
138202
const EXAMPLE_EVENTS = `{
139203
"day_1": {
@@ -187,15 +251,16 @@ class Timetable extends Component {
187251
<Nav>
188252
<NavItem>
189253
<ButtonGroup>
190-
<Button size="sm" onClick={() => this.createEvent("day_6", "Created", "evt-orange", "16:00", "17:00" )}>Add event</Button>
254+
<Button size="sm" onClick={() => this.createEvent("day_6", "Created", "evt-orange", "16:00", "17:00")}>Add event</Button>
191255

192256
<Button size="sm" >Edit event</Button>
193257
<Button size="sm" onClick={() => this.exampleEvents()}>Example events</Button>
194258
<Button size="sm" onClick={() => this.clearEvents()}>Clear events</Button>
259+
<Button size="sm" onClick={() => this.setWebStorage()}>Save events</Button>
195260
<Button size="sm" onClick={() => this.deleteEvent("16fa3d39f0a0338")}>Delete test</Button>
196261

197262

198-
263+
199264
</ButtonGroup>
200265
</NavItem>
201266

0 commit comments

Comments
 (0)