-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionStore.js
More file actions
187 lines (165 loc) · 4.82 KB
/
SessionStore.js
File metadata and controls
187 lines (165 loc) · 4.82 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
import {
AsyncStorage
} from 'react-native';
import Constants from './constants';
import axios from 'axios';
import urls from './URLS';
const {
SET_UP_STATUS,
TOKEN,
SESSION_ID,
USER_DATA,
LOGS,
CONFIG,
TRACKS,
APP_USAGE_TIME
} = Constants;
export default class SessionStore {
constructor() {
const instance = this.constructor.instance;
if (instance) {
return instance;
}
this.constructor.instance = this;
}
state = {
[TOKEN] : 'something',
[CONFIG] : {},
[SET_UP_STATUS] : false,
[SESSION_ID] : '',
[USER_DATA] : {}
}
temp = {
}
getValueFromStore = async (key)=>{
let value = await AsyncStorage.getItem(key);
return value;
}
putInStore = async (key, value) =>{
await AsyncStorage.setItem(key, '' + value);
}
deleteFromStore = async (key) =>{
await AsyncStorage.removeItem(key);
}
getValue = (key) =>{
return this.state[key];
}
putValue = (key, value) =>{
this.state[key] = value;
}
getValueTemp = (key) =>{
return this.temp[key];
}
putValueTemp = (key, value) =>{
this.temp[key] = value;
}
format = (obj, callback) =>{
let res = [];
let keys = Object.keys(obj);
for(var i=0; i<keys.length; i++){
res.push({_id : keys[i], count : obj[keys[i]].length})
if(i==keys.length - 1){
return callback(res);
}
}
}
reset = async () =>{
this.state = {
[TOKEN] : 'something',
[CONFIG] : {},
[SET_UP_STATUS] : false,
[SESSION_ID] : '',
[USER_DATA] : {}
}
/* CLEAN TEMP HOLDER MISSING */
await AsyncStorage.clear();
}
getValueBulk = async () =>{
let keys = Object.keys(this.state);
let resArrays = await AsyncStorage.multiGet(keys);
for(var i=0; i< resArrays.length; i++){
this.state['' + resArrays[i][0]] = JSON.parse(resArrays[i][1]);
}
this.temp[APP_USAGE_TIME] = new Date().getTime();
}
setSessionId = async () => {
let session = this.state[SESSION_ID];
const config = this.state[CONFIG] === null ? {} : this.state[CONFIG];
const id = config._id;
if(session === null || session === ''){
if(this.state.temp === null){
this.state.temp = {}
}
this.state.temp[UPDATES] = [];
this.state.temp[VIEWS] = [];
this.state.temp[VISITS] = [];
this.state[SESSION_ID] = id === undefined || id === null ? new Date().getTime().toString() + '@temp' : new Date().getTime().toString() + '@' + id
} else {
let cs = new Date().getTime();
let ts = parseInt(session.split('@')[0]);
let diff = (cs - ts);
if(diff > (24 * 3600 * 1000)){
await this.deleteFromStore(SESSION_ID);
if(this.state.temp === null){
this.state.temp = {}
}
this.state.temp[UPDATES] = [];
this.state.temp[VIEWS] = [];
this.state.temp[VISITS] = [];
this.state[SESSION_ID] = id === undefined || id === null ? new Date().getTime().toString() + '@temp' : new Date().getTime().toString() + '@' + id
}
}
}
setValueBulk = async () =>{
let arr = [];
let keys = Object.keys(this.state);
for(var i=0; i<keys.length; i++){
arr.push([keys[i], JSON.stringify(this.state[keys[i]])]);
}
await AsyncStorage.multiSet(arr);
}
publishLogs = () =>{
if(this.temp[LOGS].length > 0){
const formData = new FormData();
this.temp[LOGS] = [];
formData.append('logs', JSON.stringify(this.temp[LOGS]));
formData.append('session_id', JSON.stringify(this.state[SESSION_ID]));
formData.append('dummy', [{_id : 'something'}]);
axios.post(urls.PUT_LOGS, formData, {
headers: {
'Content-Type': 'multipart/form-data',
'x-access-token': this.state[TOKEN]
}
}).then((response) => {
console.log('LOGS',response);
}).catch(err => console.log(err));
}
}
publishTracks = () =>{
if(this.temp[TRACKS].length > 0){
const formData = new FormData();
this.temp[TRACKS] = [];
formData.append('logs', JSON.stringify(this.temp[TRACKS]));
formData.append('session_id', JSON.stringify(this.state[SESSION_ID]));
formData.append('dummy', [{_id : 'something'}]);
axios.post(urls.PUT_TRACKS, formData, {
headers: {
'Content-Type': 'multipart/form-data',
'x-access-token': this.state[TOKEN]
}
}).then((response) => {
console.log('TRACKS', response);
}).catch(err => console.log(err));
}
}
pushLogs = (element) =>{
let logs = this.temp[LOGS] === null || this.temp[LOGS] === undefined ? [] : this.temp[LOGS];
logs.push(element);
this.temp[LOGS] = logs;
}
pushTrack = (element) =>{
let logs = this.temp[TRACKS] === null || this.temp[TRACKS] === undefined ? [] : this.temp[TRACKS];
logs.push(element);
this.temp[TRACKS] = logs;
}
};