-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.js
142 lines (136 loc) · 3.98 KB
/
store.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
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
import {
AppRegistry,
StyleSheet,
Text,
View,
DeviceEventEmitter,
Alert,
AsyncStorage
} from 'react-native';
import Beacons from 'react-native-beacons-manager';
import { createStore } from 'redux';
import { connect, Provider } from 'react-redux';
var defaultState = { // Initially empty
"beacons" : [],
"projects" : [],
"rooms" : [],
"nearbyRooms": []
}
async function updatePersistentStore(id, todo){
// If we're saving or unsaving
if(todo === "save" || todo === "unsave"){
let savedProjects = [];
try {
s = await AsyncStorage.getItem('savedProjects');
if(s != null){
savedProjects = JSON.parse(s);
}
} catch(error) { /* If error, just use the blank */ }
if( todo === "save" && !savedProjects.includes(id) ){
savedProjects.push(id);
} else if(todo === "unsave" && savedProjects.includes(id)){
let index = savedProjects.indexOf(id);
savedProjects.splice(index, 1); // JS to remove from array!
}
try {
AsyncStorage.setItem('savedProjects', JSON.stringify(savedProjects));
} catch(error) { /* If error, just ignore */ }
}
//as above
if(todo === "done" || todo === "undone"){
let doneProjects = [];
try {
s = await AsyncStorage.getItem('doneProjects');
if(s != null){
doneProjects = JSON.parse(s);
}
} catch(error) {}
if( todo === "done" && !doneProjects.includes(id) ){
doneProjects.push(id);
} else if(todo === "undone" && doneProjects.includes(id)){
let index = doneProjects.indexOf(id);
doneProjects.splice(index, 1);
}
try {
AsyncStorage.setItem('doneProjects', JSON.stringify(doneProjects));
} catch(error) {}
}
}
// Reducer to build Redux state
// Refer to report for how this works
function state(state = defaultState, action) {
switch (action.type) {
case 'BEACONS_UPDATE':
action.projects = state.projects;
action.rooms = state.rooms;
action.nearbyRooms = [];
for(i in action.beacons){
for(j in action.rooms){
if(action.rooms[j].minor_number === action.beacons[i].minor){
if(!action.nearbyRooms.includes(action.rooms[j])){
action.nearbyRooms.push(action.rooms[j]);
}
break;
}
}
}
return action
case 'PROJECT_CHANGE_STATUS':
id = action.projectID;
todo = action.todo;
action = state;
for(i in action.projects){
if(action.projects[i].id === id){
switch(todo){
case 'save':
action.projects[i].saved = true;
break;
case 'unsave':
action.projects[i].saved = false;
break;
case 'done':
action.projects[i].done = true;
break;
case 'undone':
action.projects[i].done = false;
break;
default:
break;
}
break;
}
}
setTimeout(function(){updatePersistentStore(id, todo)}, 1);
return action
case 'PROJECTS_UPDATE':
for(i in action.projects){
action.projects[i].changeStatus = function(todo){
store.dispatch({type:'PROJECT_CHANGE_STATUS', projectID: this.id, todo:todo});
}
}
action.beacons = state.beacons;
action.rooms = state.rooms;
action.nearbyRooms = state.nearbyRooms;
return action;
case 'ROOMS_UPDATE':
action.beacons = state.beacons;
action.projects = state.projects;
action.nearbyRooms = state.nearbyRooms;
for(i in action.beacons){
for(j in action.rooms){
if(action.rooms[j].minor_number === action.beacons[i].minor){
if(!action.nearbyRooms.includes(action.rooms[j])){
action.nearbyRooms.push(action.rooms[j]);
}
break;
}
}
}
return action
default:
return state
}
}
// Create a redux store and pass to index
store = createStore(state);
export default store;