-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriteScreen.js
More file actions
99 lines (90 loc) · 2.47 KB
/
WriteScreen.js
File metadata and controls
99 lines (90 loc) · 2.47 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
import React, { useState, useEffect } from 'react';
import { View, Text, TextInput, StyleSheet } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import Toast from 'react-native-simple-toast';
import url from './url'
import MyButton from './MyButton';
function WriteScreen() {
const [text, setText] = useState('');
const savePost = async() => {
//is logged in check!
//check all fetch clearCookie senarios
let data = {};
let errorCode = '-1';
try {
await fetch(url.clearCookie);
const res = await fetch(url.addPost, {
method: 'POST',
body: JSON.stringify({
text: text
}),
headers: {
'Content-Type': 'application/json',
'Cookie': await AsyncStorage.getItem('@cookie')
}
});
data = await res.json();
if (data.ok != 1) {
if (data.msg == 'need to login') {
errorCode = '2a';
} else if (data.msg == 'invalid parameter'){
errorCode = '2b';
} else {
errorCode = '2c';
}
}
} catch (err) {
console.error(err);
errorCode = '1';
}
console.log('data -> ' + JSON.stringify(data));
console.log("error code -> " + errorCode);
if (errorCode == '-1') {
Toast.show('Saved✅');
} else if (errorCode == '1') {
Toast.show(`Network Error🔗❌`);
} else if (errorCode == '2a') {
Toast.show('Login Required🔑');
} else if (errorCode == '2b') {
Toast.show('Invalid Content📝❌');
} else {
Toast.show(`Error #${errorCode}❌`);
}
setText('');
}
return(
<View style={styles.container}>
<View style={styles.text_input_container}>
<TextInput placeholder="...What's interesting today?" multiline={true}
style={styles.text_input} value={text} onChangeText={(t) => setText(t)} />
</View>
<View style={styles.button_container}><MyButton title='Save' onPress={() => savePost()} /></View>
</View>
)
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
text_input_container: {
flex: 0.88,
width: '85%',
padding: 20,
backgroundColor: '#E2E2E2',
borderColor: '#E2E2E2',
borderRadius: 8,
borderWidth: 2,
},
text_input: {
width: '100%',
height:'100%',
fontSize: 15,
color: 'dimgray'
},
button_container: {
marginTop: 10
}
});
export default WriteScreen;