-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
107 lines (91 loc) · 2.86 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
import { StatusBar } from 'expo-status-bar';
import Constants from 'expo-constants';
import React, { useState, useEffect } from 'react';
import {
Platform,
StyleSheet,
View,
Modal,
} from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import Feed from './screens/Feed';
import Comments from './screens/Comments';
const ASYNC_STORAGE_COMMENTS_KEY = 'ASYNC_STORAGE_COMMENTS_KEY';
export default function App() {
const [commentsForItem, setCommentsForItem] = useState({});
const [showModal, setShowModal] = useState(false);
const [selectedItemId, setSelectedItemId] = useState(null);
const openCommentScreen = (id) => {
setShowModal(true);
setSelectedItemId(id);
};
const closeCommentScreen = () => {
setShowModal(false);
setSelectedItemId(null);
};
const onSubmitComment = (text) => {
const comments = commentsForItem[selectedItemId] || [];
const updated = {
...commentsForItem,
[selectedItemId]: [...comments, text],
};
setCommentsForItem(updated);
try {
AsyncStorage.setItem(
ASYNC_STORAGE_COMMENTS_KEY,
JSON.stringify(updated),
);
} catch {
console.log('Failed to save comment', text, 'for', selectedItemId);
}
};
const loadComments = async () => {
const comments = await AsyncStorage.getItem(ASYNC_STORAGE_COMMENTS_KEY);
setCommentsForItem(comments ? JSON.parse(comments) : {});
};
useEffect(() => {
loadComments();
}, []);
return (
<View style={styles.container}>
<Feed
style={styles.feed}
commentsForItem={commentsForItem}
onPressComments={openCommentScreen}
/>
<Modal
visible={showModal}
animationType='slide'
onRequestClose={closeCommentScreen}
>
<Comments
style={styles.comments}
comments={commentsForItem[selectedItemId] || []}
onClose={closeCommentScreen}
onSubmitComment={onSubmitComment}
/>
</Modal>
</View>
);
};
const platformVersion = Platform.OS === 'ios' ? parseInt(Platform.Version, 10) : Platform.Version;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
},
feed: {
flex: 1,
marginTop:
Platform.OS === 'android' || platformVersion < 11
? Constants.statusBarHeight
: 0,
},
comments: {
flex: 1,
marginTop:
Platform.OS === 'android' || platformVersion < 11
? Constants.statusBarHeight
: 0,
},
});