-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
107 lines (91 loc) · 2.93 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/
import React from 'react';
import {ScrollView, View} from 'react-native';
import Input from './components/form/Input';
import Header from './components/templates/Header';
import Button from './components/buttons/Button';
import TodoList from './components/todo/TodoList';
import TabBar from './components/tab/TabBar';
import {styles} from './AppStyles'
const Component = React.Component;
let todoIndex = 0;
class App extends Component {
constructor(props) {
super(props)
this.state = {
inputValue: '',
todos: [],
type: 'All',
}
this.setType = this.setType.bind(this);
this.submitTodo = this.submitTodo.bind(this);
this.toggleComplete = this.toggleComplete.bind(this);
this.deleteTodo = this.deleteTodo.bind(this);
}
inputChange(inputValue) {
console.log(' Input Value: ', inputValue)
this.setState({inputValue})
}
setType(type) {
this.setState({type})
}
submitTodo() {
if (this.state.inputValue.match(/^\s*$/)) {
return
}
const todo = {
title: this.state.inputValue,
todoIndex,
complete: false
}
todoIndex++
const todos = [...this.state.todos, todo]
this.setState({todos, inputValue: ''}, () => {
console.log('State: ', this.state);
})
}
toggleComplete(todoIndex) {
let todos = this.state.todos
todos.forEach((todo) => {
if (todo.todoIndex === todoIndex) {
todo.complete = !todo.complete
}
})
this.setState({todos})
}
deleteTodo(todoIndex) {
let {todos} = this.state
todos = todos.filter((todo) => todo.todoIndex !== todoIndex)
this.setState({todos})
}
render() {
// deconstructing
const {todos, inputValue, type} = this.state,
title = "My Fasting App", placeholderText = "How did your fast go?", buttonText = "Submit";
return (
<View style={styles.container}>
<ScrollView
keyboardShouldPersistTaps='always'
style={styles.content}>
<Header title={title}/>
<Input inputValue={inputValue} placeholderText={placeholderText}
inputChange={(text) => this.inputChange(text)}/>
<TodoList
type={type}
toggleComplete={this.toggleComplete}
deleteTodo={this.deleteTodo}
todos={todos}/>
<Button buttonText={buttonText} submitTodo={this.submitTodo}/>
<TabBar type={type} setType={this.setType}/>
</ScrollView>
</View>
);
}
}
export default App;