-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
87 lines (71 loc) · 2.05 KB
/
App.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
console.ignoredYellowBox = ['Remote debugger']; //Temp fix for annoying warning
import React, { Component } from 'react';
import { View, StatusBar, AsyncStorage } from 'react-native';
//import base from './src/base';
import sampleItems from './src/temp-data/sample-fishes';
import {Root} from './src/components/routing/router';
class App extends Component {
state = {
inventory: {},
buyList: {},
userID: 'jonathan'
}
componentDidMount() {
this.getLocalStorage();
}
saveToLocal = (objName, obj) => {
AsyncStorage.setItem(objName, JSON.stringify(obj))
.then(console.log(AsyncStorage.getItem('inventory')))
}
getLocalStorage = async () => {
try {
let inventoryString = await AsyncStorage.getItem('inventory');
let inventory = JSON.parse(inventoryString);
let buyListString = await AsyncStorage.getItem('buyList');
let buyList = JSON.parse(inventoryString);
if (inventoryString) {
this.setState({
inventory: JSON.parse(inventoryString)
})
}
if (buyListString) {
this.setState({
buyList: JSON.parse(buyListString)
})
}
}
catch(error) {
console.log(error);
}
}
addToBuyList = (key, item) => {
const buyList = {...this.state.buyList}
buyList[key] = item;
this.setState({ buyList }, function() {
this.saveToLocal('buyList', this.state.buyList);
});
};
addToInventory = (item) => {
const inventory = {...this.state.inventory}
inventory[item.name] = item;
this.setState({ inventory }, function() {
this.saveToLocal('inventory', this.state.inventory);
});
};
render() {
return (
<View style={{ flex: 1 }}>
<StatusBar backgroundColor="blue" barStyle="dark-content"/>
<Root
screenProps={{
inventory: this.state.inventory,
buyList: this.state.buyList,
addToBuyList: this.addToBuyList,
addToInventory: this.addToInventory
}}
/>
</View>
);
}
}
export default App;