forked from jzpz/inventoryapp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddItem.js
More file actions
161 lines (149 loc) · 6.48 KB
/
AddItem.js
File metadata and controls
161 lines (149 loc) · 6.48 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
import { database, auth } from './Database'
import { addItem, saveItem, saveItemInfo } from './database_functions/ItemData'
import React, { useState, useEffect, useCallback } from 'react';
import { Text, View, KeyboardAvoidingView, Keyboard } from 'react-native';
import { useFocusEffect, useTheme } from '@react-navigation/native';
import { ref, get } from "firebase/database";
import { styles } from './Styles';
import { StyledInput } from './components/StyledInput';
import { updateInvalidInputsList } from './functions/updateInvalidInputsList';
import { SolidButton } from './components/SolidButton';
import { Divider } from 'react-native-elements';
// Insert specified amount of specific item to your inventory
export const AddItem = ({ navigation, route }) => {
const [item, setItem] = useState({...route.params?.item, barcode: route.params?.barcode ?? null})
const [inventory, setInventory] = useState({...route.params?.inventory})
const [doInputValueCheck, setDoInputValueCheck] = useState(false)
const [keyboardStatus, setKeyboardStatus] = useState(false);
const [error, setError] = useState('')
let invalidInputsList = []
const colors = useTheme().colors;
useEffect(() => {
const showSubscription = Keyboard.addListener("keyboardDidShow", () => {
setKeyboardStatus(true);
});
const hideSubscription = Keyboard.addListener("keyboardDidHide", () => {
setKeyboardStatus(false);
});
return () => {
showSubscription.remove();
hideSubscription.remove();
};
}, []);
useEffect(() => {
if(doInputValueCheck && !invalidInputsList.length > 0) {
submit()
}
}, [doInputValueCheck])
// Function called by Styled Input
const handleInvalidValue = (inputName, valueIsInvalid) => {
invalidInputsList =
updateInvalidInputsList(
invalidInputsList, inputName, valueIsInvalid
)
}
const submit = () => {
setError('')
let errMsg
// Add new item
if(!route.params.edit) {
addItem(inventory, item)
.catch(e => errMsg = "Error while adding item (" + e.code + ")")
// Edit already existing item
} else {
saveItem(inventory, item)
.catch(e => errMsg = "Error while editing item (" + e.code + ")")
saveItemInfo(item)
.catch(e => errMsg = "Error while editing item (" + e.code + ")")
}
if(!errMsg) {
navigation.navigate('Item List', {
inventory: inventory,
item: item
})
} else setError(errMsg)
}
return(
<>
<KeyboardAvoidingView style={[{flex:1, alignItems:"center", backgroundColor:colors.background, padding: 10}]}>
<View style={{flex:1}}>
<View style={{height:"100%", justifyContent:"center"}}>
{/* Main container */}
<View style={[{width:"100%",flexDirection:"row",alignItems:"center", backgroundColor:colors.card, borderRadius:5, padding:20, marginTop: keyboardStatus ? -45 : null}]}>
<View style={{width:"100%"}}>
<Text onPress={() => console.log(auth.currentUser.uid)} style={{color:colors.primary3, fontSize:22, fontWeight:"bold", display: keyboardStatus ? "none" : null}}>Item details</Text>
<Divider color={colors.reverse.card} style={{marginVertical:10}} />
<View>
{/* Form */}
<StyledInput
label="Name"
matchType="text"
checkValue={doInputValueCheck}
handleInvalidValue={handleInvalidValue}
onChangeText={name => {setItem({...item, name: name}); setError('')}}
value={item.name}
placeholder="Name for item"
icon="tag"
inputContainerStyle={{margin: keyboardStatus ? -10 : null}}
/>
<StyledInput
label="Description"
keyboardStatus={keyboardStatus}
checkValue={doInputValueCheck}
handleInvalidValue={handleInvalidValue}
onChangeText={description => {setItem({...item, description: description}); setError('')}}
value={item.description}
placeholder="Item description"
icon="quote-right"
inputContainerStyle={{margin: keyboardStatus ? -10 : null}}
/>
<StyledInput
label="Amount"
keyboardStatus={keyboardStatus}
checkValue={doInputValueCheck}
handleInvalidValue={handleInvalidValue}
matchType="number"
keyboardType="numeric"
onChangeText={amount => {setItem({...item, amount: amount}); setError('')}}
value={item.amount}
placeholder="Amount"
icon="cubes"
inputContainerStyle={{marginBottom:-10, margin: keyboardStatus ? -10 : null}}
/>
</View>
</View>
</View>
</View>
</View>
{/* Footer */}
<View style={[{width:"100%", alignItems:"center", position:"absolute", bottom:0}]}>
{/* Save button */}
<View style={{alignItems:"center", marginVertical: keyboardStatus ? 0 : 20, width: "90%"}}>
<View style={{flexDirection:"row"}}>
{!route.params.edit &&
<SolidButton
color="warning"
style={{width:"50%", marginRight: 5}}
onPress={() =>
navigation.navigate('Barcode Scanner', {
inventory: inventory,
item: item
}
)}
title="Scan barcode"
icon="camera"
/>
}
<SolidButton
style={{width:"50%", marginLeft: 5}}
onPress={() => setDoInputValueCheck(doInputValueCheck + 1)}
title={route.params.edit ? "Save" : "Add item"}
icon={route.params.edit ? "check" : "plus"}
/>
</View>
</View>
</View>
</KeyboardAvoidingView>
</>
)
}