forked from jzpz/inventoryapp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddInventory.js
More file actions
167 lines (160 loc) · 6.36 KB
/
AddInventory.js
File metadata and controls
167 lines (160 loc) · 6.36 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
import { database, auth } from './Database'
import { addInventory, editInventory, removeInventory } from './database_functions/InventoryData';
import { changeUserData } from './database_functions/UserData'
import React, { useState, useEffect, useContext } from 'react';
import { StyleSheet, Text, View, KeyboardAvoidingView } from 'react-native';
import { useTheme, useFocusEffect } from '@react-navigation/native';
import { increment } from "firebase/database";
import { ColorPicker } from './components/ColorPicker';
import { SolidButton } from './components/SolidButton';
import { StyledInput } from './components/StyledInput';
import { updateInvalidInputsList } from './functions/updateInvalidInputsList';
import { Divider, Dialog, CheckBox } from 'react-native-elements';
import { styles } from './Styles';
import { UserContext } from './AppContext';
export const AddInventory = ({ navigation, route }) => {
const colors = useTheme().colors;
const {user, setUser} = useContext(UserContext)
const [values, setValues] = useState({
...route.params?.inventory, creator: user.displayName
} ?? {
shared: false, creator: user.displayName
})
const [error, setError] = useState('');
const [visibleDialog, setVisibleDialog] = useState('');
const [checkInputValues, setCheckInputValues] = useState(false);
let invalidInputsList = [];
const add = () => {
setError('')
addInventory(values.name, values)
.then(result => {
changeUserData({inventoriesCreated: increment(1)})
.catch(e => console.log("Increment failed: " + (e.code)))
navigation.navigate('Inventories', {
inventory: {...values}
})
})
.catch(e => setError(`Error (${e})`))
}
const edit = () => {
setError('')
editInventory(route.params.inventory.name, values)
.then(result => {
navigation.navigate('Inventories', {
inventory: {...values}
})
})
.catch(e => setError(`Error (${e})`))
}
useEffect(() => {
if(checkInputValues) {
if(values.edit)
edit()
else
add()
}
}, [checkInputValues])
// Function called by Styled Input
const handleInvalidValue = (inputName, valueIsInvalid) => {
invalidInputsList =
updateInvalidInputsList(
invalidInputsList, inputName, valueIsInvalid
)
}
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}]}>
<View style={{width:"100%"}}>
<Text onPress={()=> console.log(refs("inventories").user.child) } style={{color:colors.primary3, fontSize:22, fontWeight:"bold"}}>Inventory details</Text>
<Divider color={colors.reverse.card} style={{marginVertical:10}} />
<View>
<StyledInput
label="Inventory name"
style={{width:50}}
matchType="text"
onChangeText={name => {setValues({...values, name: name}); setError('')}}
value={values.name}
placeholder="Name for inventory"
icon="tag"
iconColor={values.color === null ? colors.reverse.card : values.color}
checkValue={checkInputValues}
handleInvalidValue={handleInvalidValue}
/>
<View style={{alignItems:"center"}}>
<ColorPicker
onColorChange={(color) => setValues({...values, color: color})}
/>
</View>
</View>
</View>
</View>
</View>
</View>
{/* Footer */}
<View style={[{width:"100%", alignItems:"center", position:"absolute", bottom:0}]}>
{/* Save button */}
<View style={{alignItems:"center", marginVertical: 20, width: "90%"}}>
<Text style={styles.error}>{error}</Text>
<View style={{flexDirection:"row"}}>
{!!route.params?.edit === true &&
<SolidButton
style={{width:"50%", marginRight: 5}}
onPress={() => setVisibleDialog("delete")}
title="Delete inventory"
color="error"
icon="trash"
/>
}
<CheckBox
center
title="Shared Inventory"
checked={values.shared}
onPress={() => setValues({...values, shared: !values.shared})}
/>
<SolidButton
style={{width:"50%", marginLeft: 5}}
onPress={() => setCheckInputValues(checkInputValues + 1)}
title="Confirm"
icon="check"
/>
</View>
</View>
</View>
</KeyboardAvoidingView>
<Dialog
isVisible={visibleDialog === "delete" ? true : false}
onBackdropPress={() => setVisibleDialog("")}
overlayStyle={{backgroundColor: colors.card}}
>
<Dialog.Title titleStyle={{color: colors.text}} title="Confirm deletion" />
<Text style={{color: colors.text}}>Are you sure you want
to delete inventory {route.params?.inventory.name}?
</Text>
<View style={{width:"100%"}}>
<Dialog.Actions>
<SolidButton
style={{width: "30%", marginLeft:10}}
title="Cancel"
color="error"
onPress={() => setVisibleDialog("")}
/>
<SolidButton
style={{width: "30%"}}
title="Confirm"
color="success"
onPress={() => {
removeInventory(route.params.inventory)
.then(res => navigation.navigate('Inventories'))
.catch(e => setError(`Error (${e.code})`))
}}
/>
</Dialog.Actions>
</View>
</Dialog>
</>
)
}