1 |
8b1106f4
|
Fantič
|
import { VStack, Box, Text, HStack, ScrollView, Flex, Button, CloseIcon, IconButton, TextArea, useToast } from "native-base";
|
2 |
cb25df10
|
Fantič
|
import { Note } from "../../types/note";
|
3 |
|
|
import NoteView from "./NoteView";
|
4 |
2135f53d
|
Fantič
|
import { MessageIcon } from "../general/Icons";
|
5 |
56626a63
|
Fantič
|
import React, { useCallback, useState } from "react";
|
6 |
2135f53d
|
Fantič
|
import { updateNote, deleteNote } from "../../stores/actions/notesThunks";
|
7 |
|
|
import { ConfirmDialog } from "../general/Dialogs";
|
8 |
b56c1e7d
|
Fantič
|
import { AppDispatch, RootState } from "../../stores/store";
|
9 |
|
|
import { useDispatch, useSelector } from "react-redux";
|
10 |
f386a4fe
|
Fantič
|
import { InfoToast } from "../toast/InfoToast";
|
11 |
b56c1e7d
|
Fantič
|
import { UserRole } from "../../stores/reducers/userSlice";
|
12 |
cb25df10
|
Fantič
|
|
13 |
8b1106f4
|
Fantič
|
|
14 |
|
|
|
15 |
293f99f8
|
Fantič
|
const NotesListView = (props: { notes: Note[], navigation: any, handleCreateComment: any, requestPending: boolean, height: number }) => {
|
16 |
cb25df10
|
Fantič
|
const notes = props.notes;
|
17 |
2135f53d
|
Fantič
|
|
18 |
b56c1e7d
|
Fantič
|
// const userRole = useSelector((state: RootState) => state.user.role)
|
19 |
|
|
const userId = useSelector((state: RootState) => state.user.userId)
|
20 |
|
|
const userRole = useSelector((state: RootState) => state.user.role)
|
21 |
|
|
|
22 |
|
|
console.log(userRole)
|
23 |
|
|
|
24 |
2135f53d
|
Fantič
|
const [newComment, setNewComment] = useState<string>("");
|
25 |
|
|
const [replyingTo, setReplyingTo] = useState<{ messageId: string; userName: string } | null>(null);
|
26 |
|
|
|
27 |
f386a4fe
|
Fantič
|
const toast = useToast();
|
28 |
|
|
|
29 |
bc25708c
|
Fantič
|
const [renderedNotes, setRenderedNotes] = useState(6);
|
30 |
|
|
|
31 |
2135f53d
|
Fantič
|
const cancelRef = React.useRef(null);
|
32 |
|
|
|
33 |
|
|
const dispatch = useDispatch<AppDispatch>();
|
34 |
|
|
|
35 |
|
|
const [confirmDialog, setConfirmDialog] = React.useState<{
|
36 |
|
|
headerText?: string;
|
37 |
|
|
bodyText?: string;
|
38 |
|
|
confirmText?: string;
|
39 |
|
|
confirmColor?: string;
|
40 |
|
|
cancelRef?: any;
|
41 |
|
|
onClose?: () => void;
|
42 |
|
|
onSubmit?: () => void;
|
43 |
|
|
} | null>(null);
|
44 |
|
|
|
45 |
56626a63
|
Fantič
|
const handleReply = useCallback((messageId: string, userName: string) => {
|
46 |
|
|
setReplyingTo({ messageId, userName });
|
47 |
|
|
}, [setReplyingTo]);
|
48 |
b5ac7218
|
Fantič
|
|
49 |
56626a63
|
Fantič
|
const handleEdit = useCallback((note: Note) => {
|
50 |
|
|
if (!props.requestPending) {
|
51 |
2135f53d
|
Fantič
|
dispatch(updateNote(note));
|
52 |
56626a63
|
Fantič
|
}
|
53 |
|
|
}, [dispatch, props.requestPending]);
|
54 |
b5ac7218
|
Fantič
|
|
55 |
56626a63
|
Fantič
|
const handleDelete = useCallback((messageId: string) => {
|
56 |
|
|
if (!props.requestPending) {
|
57 |
|
|
dispatch(deleteNote({ noteId: messageId }));
|
58 |
|
|
}
|
59 |
|
|
}, [dispatch, props.requestPending]);
|
60 |
2135f53d
|
Fantič
|
|
61 |
f386a4fe
|
Fantič
|
|
62 |
|
|
|
63 |
8b1106f4
|
Fantič
|
const handleScrollEnd = (event: any) => {
|
64 |
bc25708c
|
Fantič
|
const offsetY = event.nativeEvent.contentOffset.y;
|
65 |
|
|
const contentHeight = event.nativeEvent.contentSize.height;
|
66 |
|
|
const screenHeight = event.nativeEvent.layoutMeasurement.height;
|
67 |
|
|
|
68 |
|
|
if (offsetY + screenHeight >= contentHeight - 20) {
|
69 |
|
|
// Load more notes when the user is near the end of the list
|
70 |
|
|
const nextBatchSize = 5; // Number of notes to render in the next batch
|
71 |
|
|
|
72 |
8b1106f4
|
Fantič
|
if ((renderedNotes + nextBatchSize) > notes.length && (renderedNotes + nextBatchSize) != notes.length) {
|
73 |
f386a4fe
|
Fantič
|
toast.closeAll()
|
74 |
|
|
toast.show({
|
75 |
|
|
render: ({
|
76 |
|
|
id
|
77 |
|
|
}) => {
|
78 |
|
|
return <InfoToast text={"All notes loaded..."} onClose={() => toast.close(id)} />;
|
79 |
|
|
},
|
80 |
|
|
duration: 3000
|
81 |
|
|
});
|
82 |
bc25708c
|
Fantič
|
setRenderedNotes(notes.length);
|
83 |
f386a4fe
|
Fantič
|
|
84 |
bc25708c
|
Fantič
|
}
|
85 |
8b1106f4
|
Fantič
|
else if ((renderedNotes + nextBatchSize) != notes.length) {
|
86 |
f386a4fe
|
Fantič
|
toast.closeAll()
|
87 |
|
|
toast.show({
|
88 |
|
|
render: ({
|
89 |
|
|
id
|
90 |
|
|
}) => {
|
91 |
|
|
return <InfoToast text={"Loading more notes..."} onClose={() => toast.close(id)} />;
|
92 |
|
|
},
|
93 |
|
|
duration: 3000
|
94 |
|
|
});
|
95 |
bc25708c
|
Fantič
|
setRenderedNotes(renderedNotes + nextBatchSize);
|
96 |
f386a4fe
|
Fantič
|
|
97 |
bc25708c
|
Fantič
|
}
|
98 |
|
|
}
|
99 |
|
|
};
|
100 |
|
|
|
101 |
cb25df10
|
Fantič
|
return (
|
102 |
9814562f
|
Fantič
|
<VStack >
|
103 |
293f99f8
|
Fantič
|
<Box height={props.height - (replyingTo ? 40 : 0) ?? undefined} marginBottom={2}>
|
104 |
b5ac7218
|
Fantič
|
{/* Notes */}
|
105 |
9814562f
|
Fantič
|
<ScrollView contentContainerStyle={{ flexGrow: 1 }} onScrollEndDrag={handleScrollEnd} paddingLeft={2.5} paddingRight={2.5}>
|
106 |
2135f53d
|
Fantič
|
{
|
107 |
|
|
notes && notes.length > 0 ? (
|
108 |
|
|
<VStack>
|
109 |
bc25708c
|
Fantič
|
{
|
110 |
8b1106f4
|
Fantič
|
notes.slice(0, renderedNotes).map((note, index) => {
|
111 |
f386a4fe
|
Fantič
|
|
112 |
b56c1e7d
|
Fantič
|
return <NoteView higlighted={replyingTo?.messageId == note.uuid} key={index} note={note}
|
113 |
|
|
handleReply={
|
114 |
|
|
handleReply
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
handleDelete={
|
118 |
|
|
(note.username == userId || userRole == UserRole.Admin) ?
|
119 |
|
|
handleDelete
|
120 |
|
|
:
|
121 |
|
|
undefined
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
handleEdit={
|
125 |
|
|
(note.username == userId || userRole == UserRole.Admin) ?
|
126 |
|
|
handleEdit
|
127 |
|
|
:
|
128 |
|
|
undefined
|
129 |
|
|
}
|
130 |
|
|
setConfirmDialog={setConfirmDialog} navigation={props.navigation} />;
|
131 |
f386a4fe
|
Fantič
|
|
132 |
8b1106f4
|
Fantič
|
})
|
133 |
bc25708c
|
Fantič
|
}
|
134 |
2135f53d
|
Fantič
|
</VStack>
|
135 |
|
|
) : (
|
136 |
|
|
<Text>There are no notes.</Text>
|
137 |
|
|
)
|
138 |
|
|
}
|
139 |
|
|
</ScrollView>
|
140 |
|
|
</Box>
|
141 |
|
|
|
142 |
b5ac7218
|
Fantič
|
{/* Create comment */}
|
143 |
cb25df10
|
Fantič
|
{
|
144 |
2135f53d
|
Fantič
|
props.handleCreateComment != null &&
|
145 |
b5ac7218
|
Fantič
|
<VStack>
|
146 |
|
|
|
147 |
|
|
{/* Replying to */}
|
148 |
|
|
{replyingTo != null &&
|
149 |
f386a4fe
|
Fantič
|
<Flex direction="row" alignItems="center" justify="flex-end" height={10} style={{ backgroundColor: "#FFF8E1" }}>
|
150 |
b5ac7218
|
Fantič
|
<Text>Replying to {replyingTo.userName}</Text>
|
151 |
f386a4fe
|
Fantič
|
<IconButton onPress={() => setReplyingTo(null)} size="sm" icon={<CloseIcon />} marginLeft="-1%" marginRight={20} />
|
152 |
b5ac7218
|
Fantič
|
</Flex>
|
153 |
|
|
}
|
154 |
bc25708c
|
Fantič
|
|
155 |
b5ac7218
|
Fantič
|
|
156 |
|
|
{/* Add comment */}
|
157 |
b56c1e7d
|
Fantič
|
<HStack height={90} marginLeft={1.5} marginRight={1.5}>
|
158 |
b5ac7218
|
Fantič
|
<TextArea
|
159 |
|
|
flex={1}
|
160 |
b56c1e7d
|
Fantič
|
placeholder={userRole == UserRole.Unauthorized || userRole == UserRole.User ? "Unathorized to add comment" : "Add comment"}
|
161 |
|
|
isDisabled={userRole == UserRole.Unauthorized || userRole == UserRole.User}
|
162 |
b5ac7218
|
Fantič
|
fontSize={14}
|
163 |
|
|
value={newComment}
|
164 |
|
|
onChangeText={setNewComment}
|
165 |
|
|
autoCompleteType={undefined}
|
166 |
b56c1e7d
|
Fantič
|
|
167 |
|
|
|
168 |
b5ac7218
|
Fantič
|
/>
|
169 |
|
|
<Flex marginLeft={2} marginBottom={8}>
|
170 |
b56c1e7d
|
Fantič
|
<Button disabled={userRole == UserRole.Unauthorized || userRole == UserRole.User} onPress={() => props.handleCreateComment(newComment, replyingTo)} startIcon={<MessageIcon color="#FFF" />} />
|
171 |
b5ac7218
|
Fantič
|
</Flex>
|
172 |
|
|
</HStack>
|
173 |
|
|
</VStack>
|
174 |
cb25df10
|
Fantič
|
}
|
175 |
2135f53d
|
Fantič
|
<ConfirmDialog {...confirmDialog} isShown={confirmDialog != null} cancelRef={cancelRef} />
|
176 |
b5ac7218
|
Fantič
|
</VStack >
|
177 |
cb25df10
|
Fantič
|
);
|
178 |
|
|
}
|
179 |
|
|
|
180 |
|
|
export default NotesListView
|