This repository was archived by the owner on Mar 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuggList.js
More file actions
76 lines (72 loc) · 2.55 KB
/
Copy pathSuggList.js
File metadata and controls
76 lines (72 loc) · 2.55 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
import React from 'react';
import { Text, View, Alert, ScrollView, } from 'react-native';
import { MaterialIcons } from '@expo/vector-icons';
import Touchable from './Touchable';
import styles from './StyleSheet.js';
/*
* This list takes from the main one what Alfred thinks may be a useful
* advice to put in the shopping list, it takes only items that are not
* in the shopping list already (position:{none|fridge}) and display
* them only if the duration is closed to the current date - the last
* saved (statistically it's time to buy them)
*/
export default class SuggList extends React.Component{
/*
* data is what will be added to the ShopList, items are received
*/
constructor(props){
super(props);
this.state={data:{},items:this.props.data()};
}
/*
* Called when selecting an element, if the element is in the fridge,
* it also asks to remove it from there, in any case it sends it to
* be added and it updates it's position
*/
addElement(value,isFridge){
var tmp = this.state.data;
tmp[value]={name:value};
this.props.changePos(value,"toBuy");
if(isFridge){
Alert.alert(
"Element from the fridge",
"Alfred still thinks "+value+" is in the fridge\n"+
"Are you running out of it?\n"+
"If you press Yes, Alfred will remove it from the fridge list",
[
{text:"Yes", onPress:()=>this.props.sendRem(value)},
{text: "No", onPress:()=>this.props.changePos(value,"both")},
]
);
}
this.setState({data:tmp});
this.props.sendBack(value); //send each time
}
/*
* Here all the element (properly filtered) are displayed in a
* Touchable element that calls the addElement on press,
* passing also if the element was in the fridge
*/
render(){
return <View style={styles().cont}>
<ScrollView>
{Object.keys(this.state.items)
.filter(e=>(new Date()-this.state.items[e].lastDate)>
this.state.items[e].duration)
.filter(e=>(!this.state.data[e]))
.filter(e=>(this.state.items[e].position=="none" ||
this.state.items[e].position=="fridge"))
.map(e=>(<View style={styles().item} key={e}>
<Touchable
onPress={()=>this.addElement(e,
this.state.items[e].position=="fridge")}>
<View style={{flexDirection:"row"}}>
<MaterialIcons name="add-shopping-cart"
style={styles().icon}/>
<Text style={styles().txt}>{e}</Text>
</View>
</Touchable></View>))}
</ScrollView>
</View>;
}
}