NewestAppCode

This commit is contained in:
2021-03-29 18:55:13 +02:00
parent ef5095dd74
commit 241e8617b9
6 changed files with 63 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
import { StyleSheet, TouchableOpacity, View, Text, TextInput } from 'react-native'; import { StyleSheet, TouchableOpacity, Alert} from 'react-native';
//themes //themes
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons'; import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
import { COLORS } from '../../themes/Colors'; import { COLORS } from '../../themes/Colors';
@@ -54,7 +54,16 @@ export const RemoveItemsButton = () => {
const dispatch = useDispatch(); const dispatch = useDispatch();
return ( return (
<TouchableOpacity style={[styles.buttonContainer, styles.RemoveItemsButton, { backgroundColor: COLORS.actionCancel }]} <TouchableOpacity style={[styles.buttonContainer, styles.RemoveItemsButton, { backgroundColor: COLORS.actionCancel }]}
onPress={() => dispatch(removeCheckedItems())}> onPress={() =>
Alert.alert(
"Warning",
"Are you sure you want to remove these items?",
[
{ text: "Cancel", },
{ text: "Remove", onPress: () => dispatch(removeCheckedItems()) }
],
{cancelable: true,}
)}>
<MaterialCommunityIcons name="delete-forever-outline" color={COLORS.dp00} size={40} /> <MaterialCommunityIcons name="delete-forever-outline" color={COLORS.dp00} size={40} />
</TouchableOpacity> </TouchableOpacity>
); );

View File

@@ -1,5 +1,5 @@
import React from 'react'; import React from 'react';
import { StyleSheet, TouchableOpacity, View, Text, TextInput, Image } from 'react-native'; import { StyleSheet, TouchableOpacity, View, Text, TextInput, Image, Alert } from 'react-native';
//components //components
import AddProductModal from './modals/AddProductModal'; import AddProductModal from './modals/AddProductModal';
@@ -11,6 +11,7 @@ import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityI
//redux //redux
import { useDispatch } from 'react-redux'; import { useDispatch } from 'react-redux';
import { toggleVisibility } from '../../redux/slices/groceryList/toggleSlice'; import { toggleVisibility } from '../../redux/slices/groceryList/toggleSlice';
import { removeCheckedProducts } from '../../redux/slices/groceryList/productsSlice';
export const AddProductButton = () => { export const AddProductButton = () => {
@@ -25,6 +26,28 @@ export const AddProductButton = () => {
</TouchableOpacity> </TouchableOpacity>
); );
} }
export const RemoveProductsButton = () => {
const dispatch = useDispatch();
return (
<TouchableOpacity style={[styles.buttonContainer, styles.RemoveProducts]}
onPress={() =>
Alert.alert(
"Warning",
"Are you sure you want to remove these products?",
[
{ text: "Cancel", },
{ text: "Remove", onPress: () => dispatch(removeCheckedProducts()) }
],
{cancelable: true,}
)
}>
<MaterialCommunityIcons name="delete-forever-outline" color={COLORS.dp00} size={40} />
{/* pop-up screen */}
<AddProductModal />
</TouchableOpacity >
);
}
const styles = StyleSheet.create({ const styles = StyleSheet.create({
buttonContainer: { buttonContainer: {
@@ -38,5 +61,8 @@ const styles = StyleSheet.create({
AddNewProduct: { AddNewProduct: {
backgroundColor: COLORS.primary, backgroundColor: COLORS.primary,
}, },
RemoveProducts: {
backgroundColor: COLORS.actionCancel
}
}); });

View File

@@ -10,7 +10,13 @@ import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityI
import { useSelector, useDispatch } from 'react-redux'; import { useSelector, useDispatch } from 'react-redux';
export default StorageList = (props) => { export default StorageList = (props) => {
const products = useSelector(state => state.products) const products = useSelector(state => state.products)
const storageFiltered = useSelector(state => state.storage.filter((item) => products.find((product) => product.id === item.id).tag === props.tag)) const findProductTag = (item) => {
let product = products.find((product) => product.id === item.id)
let tag
product ? tag = product.tag : 0
return tag
}
const storageFiltered = useSelector(state => state.storage.filter((item) =>findProductTag(item) === props.tag))
const tag = useSelector(state => state.tags.find((tag) => tag.tagName === props.tag)) const tag = useSelector(state => state.tags.find((tag) => tag.tagName === props.tag))
const [visible, setVisible] = useState(false); const [visible, setVisible] = useState(false);

View File

@@ -48,7 +48,7 @@ export default AddProductModal = () => {
//tags //tags
const toggleModal = () => { const toggleModal = () => {
if (modalVisible && product !== '' && tags.find((listTag) => listTag.tagName === tag)) { if (modalVisible && product !== '' && tags.find((listTag) => listTag.tagName === tag)) {
dispatch(productAdded(product, tag, price, image)); dispatch(productAdded(product, tag, price, image? image : 'https://www.redlobster.be/en/assets/img/255x370blank.png'));
setTagsList(tags.map((listTag) => listTag.tagName)); setTagsList(tags.map((listTag) => listTag.tagName));
dispatch(toggleVisibility('addProductModalVisible')); dispatch(toggleVisibility('addProductModalVisible'));
setProduct(''); setProduct('');
@@ -64,7 +64,7 @@ export default AddProductModal = () => {
const closeModal = () => { const closeModal = () => {
setTagsList(tags.map((listTag) => listTag.tagName)); setTagsList(tags.map((listTag) => listTag.tagName));
setProduct(''); setProduct('');
setTag(); setTag('');
setPrice(); setPrice();
setImage(''); setImage('');
setFocussed(false) setFocussed(false)

View File

@@ -138,6 +138,9 @@ const productsSlice = createSlice({
} }
} }
}, },
removeCheckedProducts(state) {
return state.filter(product => product.checked === false)
},
checkToggle: { checkToggle: {
reducer(state, action) { reducer(state, action) {
let product = state.find((product) => product.id === action.payload.id) let product = state.find((product) => product.id === action.payload.id)
@@ -169,6 +172,6 @@ const productsSlice = createSlice({
}, },
}); });
export const { productAdded, editProduct, checkToggle, modalToggle } = productsSlice.actions; export const { productAdded, editProduct, checkToggle, modalToggle , removeCheckedProducts} = productsSlice.actions;
export default productsSlice.reducer; export default productsSlice.reducer;

View File

@@ -4,7 +4,7 @@ import { useHeaderHeight } from '@react-navigation/stack';
//dependencies\ //dependencies\
//components //components
import { AddProductButton } from '../../components/groceryComponents/ProductButtons'; import { AddProductButton, RemoveProductsButton } from '../../components/groceryComponents/ProductButtons';
import ListedProduct from '../../components/groceryComponents/ListedProduct'; import ListedProduct from '../../components/groceryComponents/ListedProduct';
import { COLORS } from '../../themes/Colors'; import { COLORS } from '../../themes/Colors';
@@ -36,6 +36,9 @@ function Products() {
<View style={styles.addProductView} > <View style={styles.addProductView} >
<AddProductButton /> <AddProductButton />
</View> </View>
<View style={styles.removeProductsView} >
<RemoveProductsButton />
</View>
</View> </View>
); );
} }
@@ -55,6 +58,11 @@ const styles = StyleSheet.create({
position: 'absolute', position: 'absolute',
right: 8, right: 8,
bottom: 10, bottom: 10,
},
removeProductsView:{
position: 'absolute',
left: 8,
bottom: 10,
} }
}); });
export default React.memo(Products); export default React.memo(Products);