-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
205 lines (186 loc) · 6.54 KB
/
index.html
File metadata and controls
205 lines (186 loc) · 6.54 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="manifest" href="manifest.json">
<title>Reduxstagram</title>
<link rel="shortcut icon" type="image/png" href="http://wes.io/ehRe/my-favourite-icon.png"/>
<link rel="stylesheet" href="https://code.getmdl.io/1.1.3/material.indigo-pink.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
</head>
<body>
<div class="mdl-layout mdl-js-layout mdl-layout--no-drawer-button mdl-layout--fixed-header mdl-color--grey-100">
<header class="mdl-layout__header">
<div class="mdl-layout__header-row">
<span class="mdl-layout__title">Reduxstagram</span>
<div class="mdl-layout-spacer"></div>
</div>
</header>
<div class="mdl-layout__drawer">
<span class="mdl-layout__title">Reduxstagram</span>
</div>
<main class="mdl-layout__content">
<div id="root" ></div>
</main>
</div>
<div id="connectionStatus">You are currently offline. Any requests made will be queued and synced as soon as you are
connected again.
</div>
<script src="/static/bundle.js"></script>
<script defer src="https://code.getmdl.io/1.1.3/material.min.js"></script>
<script src="https://cdn.jsdelivr.net/umbrella/2.0.1/umbrella.min.js"></script>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript" src="node_modules/axios/dist/axios.js"></script>
<script src="offline.min.js"></script>
<script>
function isOnline() {
var connectionStatus = document.getElementById('connectionStatus');
var reload = false;
if (navigator.onLine) {
connectionStatus.innerHTML = 'You are currently online!';
//console.log("Queue offline are sync...");
window.networkstatus = true;
//var postURI = 'http://localhost:8080/api/goals';
var postURI = 'https://localhost:8444/api/goals';
if (localStorage.getItem("deleteOfflineGoals")){
var offlineIds = localStorage.getItem("deleteOfflineGoals").split(',');
$.each(offlineIds, function (index, id) {
var Object = JSON.parse(localStorage.getItem("goals"));
var newObject = {"goals":[]};
$.each(Object.goals, function (indexGoal, value) {
if (value._links.search(id) == -1){
newObject.goals.push(value);
}
})
if (newObject.goals.length != 0){
localStorage.setItem("goals", JSON.stringify(newObject));
}
else localStorage.removeItem("goals");
})
localStorage.removeItem("deleteOfflineGoals");
}
if (localStorage.getItem("updatedGoals")){
var updatedGoalArray = localStorage.getItem("updatedGoals").split(',');
$.each(updatedGoalArray, function (index, value) {
var updatedValue = JSON.parse(localStorage.getItem(value));
var data = {
_links: "",
createdAt: null,
description: "",
failures: updatedValue.failures,
successes: updatedValue.successes,
updatedAt:null
};
axios({
method: 'post',
url: postURI+'/update/'+value,
json: true,
data: data
})
.then(function(response) {
console.log("Success");
})
.catch(function(response){
console.log("error");
})
localStorage.removeItem(value);
})
localStorage.removeItem("updatedGoals");
reload = true;
}
if (localStorage.getItem("goals")){
if (localStorage.getItem("updatedOfflineGoals")){
var goals = JSON.parse(localStorage.getItem("goals"));
var newObject = {"goals":[]};
// kondisi goals tidak kosong
$.each(goals.goals, function (index, value) {
if (localStorage.getItem(value._links)){
var updatedValue = JSON.parse(localStorage.getItem(value._links));
newObject.goals.push(updatedValue);
}
else newObject.goals.push(value);
localStorage.removeItem(value._links);
})
if (newObject.goals.length != 0){
localStorage.setItem("goals", JSON.stringify(newObject));
}
else localStorage.removeItem("goals");
localStorage.removeItem("updatedOfflineGoals");
}
// add goals
var goals = JSON.parse(localStorage.getItem("goals"));
$.each(goals.goals, function (index, value) {
axios({
method: 'post',
url: postURI,
json: true,
data: {
description: value.description,
createdAt: new Date(value.createdAt),
successes: value.successes,
failures: value.failures
}
})
.then(function(response) {
console.log("Success");
})
.catch(function(response){
console.log("error");
})
})
localStorage.removeItem("goals");
// shortcut to update Redux State
reload = true;
}
if (localStorage.getItem("deleteGoals")){
var ids = localStorage.getItem("deleteGoals").split(',');
$.each(ids, function (index, id) {
axios({
method: 'delete',
url: postURI + '/' + id,
data: null,
withCredentials: true,
params: {
}
})
})
localStorage.removeItem("deleteGoals");
}
if (reload){
//location.reload(reload);
reload = false;
}
} else {
//console.log('offline');
window.networkstatus = false;
connectionStatus.innerHTML =
'You are currently offline. Any requests made will be queued and synced as soon as you are connected again.';
}
}
window.addEventListener('online', isOnline);
window.addEventListener('offline', isOnline);
isOnline();
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/service-worker.js', {
scope: '/'
}).then(function (reg) {
if (reg.installing) {
console.log('installing');
} else if (reg.waiting) {
console.log('installed');
} else if (reg.active) {
console.log('active');
}
}).catch(function (error) {
console.log(error);
});
}
// Then later, request a one-off sync:
navigator.serviceWorker.ready.then(function (swRegistration) {
return swRegistration.sync.register('myFirstSync');
});
</script>
</script>
</body>
</html>