-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
163 lines (150 loc) · 5.06 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
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
import React from 'react';
import lodash from 'lodash';
import { View, ScrollView, Text, AsyncStorage } from 'react-native';
import Header from './components/header/index';
import TaskList from './components/task-list';
import ButtonAddTask from './components/button-add-task';
import MenuTask from './components/menu-task';
import { TASK } from './model';
import TextPrompt from './components/text-prompt';
import { style } from './styles/styles.js';
const taskList = [];
const storageKey = 'taskList';
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
myText: 'Mon premier Texte',
taskList,
isMenuTaskVisible: false,
currentTask: {},
isTextPromptVisible: false,
idGenerator: 0,
titleTextPrompt: '',
textTextPrompt: '',
modifyTextmode: false
};
}
componentWillMount = () => {
AsyncStorage.getItem(storageKey).then(storedTaskList => {
if (storedTaskList) {
this.setState({ taskList: JSON.parse(storedTaskList) }, () => {
this.setState({
idGenerator: this.state.taskList[this.state.taskList.length - 1].id + 1 });
});
}
});
};
onPressFunction = () => {
this.setState({ myText: 'Salut les gars' });
console.log('on press');
}
displayMenyTask = (taskcontent) => {
console.log('callback', taskcontent);
}
toggleMenuTaskVisibility = (task) => {
let currentTask = task;
if (this.state.isMenuTaskVisible) {
currentTask = {};
}
this.setState({ isMenuTaskVisible: !this.state.isMenuTaskVisible, currentTask });
};
deleteCurrentTask = () => {
const index = lodash.findIndex(this.state.taskList, { id: this.state.currentTask.id });
const list = this.state.taskList;
list.splice(index, 1);
this.setState({ taskList: list, currentTask: {} }, () => {
this.saveTaskList();
this.toggleMenuTaskVisibility();
});
};
changeStatusCurrentTask = () => {
const index = lodash.findIndex(this.state.taskList, { id: this.state.currentTask.id });
const updatedTask = this.state.currentTask;
updatedTask.status = (this.state.currentTask.status === TASK.todoStatus ?
TASK.doneStatus : TASK.todoStatus);
const updatedTaskList = this.state.taskList;
updatedTaskList[index] = updatedTask;
this.setState({
taskList: updatedTaskList,
currentTask: {} },
() => {
this.saveTaskList();
this.toggleMenuTaskVisibility();
});
};
toggleTextPromptVisibility = (task, titleTextPrompt, textTextPrompt, modifyTextmode) => {
console.log('par icic');
this.setState({
isTextPromptVisible: !this.state.isTextPromptVisible,
currentTask: task,
titleTextPrompt,
textTextPrompt,
modifyTextmode });
};
addTask = (value) => {
if (!value) {
return (this.setState({ titleTextPrompt: 'Saisir un texte !' }));
}
if (this.state.modifyTextmode) {
const index = lodash.findIndex(this.state.taskList, { id: this.state.currentTask.id });
const updatedTask = this.state.currentTask;
updatedTask.content = value;
const updatedTaskList = this.state.taskList;
updatedTaskList[index] = updatedTask;
this.setState({ taskList: updatedTaskList, currentTask: {} }, () => {
this.saveTaskList();
});
} else {
const addedTask = { id: this.state.idGenerator, content: value, status: TASK.todoStatus };
this.setState({ taskList: [...this.state.taskList, addedTask],
idGenerator: this.state.idGenerator + 1 }, () => {
this.saveTaskList();
});
}
this.toggleTextPromptVisibility();
};
saveTaskList = () => {
AsyncStorage.setItem(storageKey, JSON.stringify(this.state.taskList));
};
renderTaskList = () => {
if (this.state.taskList.length > 0) {
return (<TaskList
onPressCallBack={this.toggleMenuTaskVisibility}
taskList={this.state.taskList}
onLongPressCallBack={this.toggleTextPromptVisibility}
/>);
}
return (
<View style={style.textNoTask}>
<Text>
Cliquez sur le bouton + pour ajouter une tâche
</Text>
</View>
);
};
render() {
return (
<View style={{ flex: 1 }}>
<Header content="Liste des tâches" />
<ScrollView>
{this.renderTaskList()}
</ScrollView>
<MenuTask
isVisible={this.state.isMenuTaskVisible}
onDisapearCallBack={this.toggleMenuTaskVisibility}
onDeleteCallBack={this.deleteCurrentTask}
onChangeStatusCallBack={this.changeStatusCurrentTask}
/>
<ButtonAddTask onAddTaskClickCallBack={this.toggleTextPromptVisibility} />
<TextPrompt
titleTextPrompt={this.state.titleTextPrompt}
textTextPrompt={this.state.textTextPrompt}
isVisible={this.state.isTextPromptVisible}
onSubmitBtnCallBack={this.addTask}
onCancelBtnCallBack={this.toggleTextPromptVisibility}
/>
</View>
);
}
}