1
|
import {
|
2
|
VStack,
|
3
|
Box,
|
4
|
Text,
|
5
|
HStack,
|
6
|
ScrollView,
|
7
|
Flex,
|
8
|
Button,
|
9
|
CloseIcon,
|
10
|
IconButton,
|
11
|
TextArea,
|
12
|
useToast,
|
13
|
KeyboardAvoidingView
|
14
|
} from "native-base";
|
15
|
import { Note } from "../../types/note";
|
16
|
import NoteView from "./NoteView";
|
17
|
import { MessageIcon } from "../general/Icons";
|
18
|
import React, { useCallback, useState } from "react";
|
19
|
import { updateNote, deleteNote } from "../../stores/actions/notesThunks";
|
20
|
import { ConfirmDialog } from "../general/Dialogs";
|
21
|
import { AppDispatch, RootState } from "../../stores/store";
|
22
|
import { useDispatch, useSelector } from "react-redux";
|
23
|
import { InfoToast } from "../toast/InfoToast";
|
24
|
import { UserRole } from "../../stores/reducers/userSlice";
|
25
|
import {FlatList, Platform} from "react-native"
|
26
|
|
27
|
|
28
|
|
29
|
const NotesListView = (props: { notes: Note[], navigation: any, handleCreateComment: any, requestPending: boolean }) => {
|
30
|
const notes = props.notes;
|
31
|
|
32
|
// const userRole = useSelector((state: RootState) => state.user.role)
|
33
|
const userId = useSelector((state: RootState) => state.user.userId)
|
34
|
const userRole = useSelector((state: RootState) => state.user.role)
|
35
|
|
36
|
console.log(userRole)
|
37
|
|
38
|
const [newComment, setNewComment] = useState<string>("");
|
39
|
const [replyingTo, setReplyingTo] = useState<{ messageId: string; userName: string } | null>(null);
|
40
|
|
41
|
const cancelRef = React.useRef(null);
|
42
|
|
43
|
const dispatch = useDispatch<AppDispatch>();
|
44
|
|
45
|
const [confirmDialog, setConfirmDialog] = React.useState<{
|
46
|
headerText?: string;
|
47
|
bodyText?: string;
|
48
|
confirmText?: string;
|
49
|
confirmColor?: string;
|
50
|
cancelRef?: any;
|
51
|
onClose?: () => void;
|
52
|
onSubmit?: () => void;
|
53
|
} | null>(null);
|
54
|
|
55
|
const handleReply = useCallback((messageId: string, userName: string) => {
|
56
|
setReplyingTo({ messageId, userName });
|
57
|
}, [setReplyingTo]);
|
58
|
|
59
|
const handleEdit = useCallback((note: Note) => {
|
60
|
if (!props.requestPending) {
|
61
|
dispatch(updateNote(note));
|
62
|
}
|
63
|
}, [dispatch, props.requestPending]);
|
64
|
|
65
|
const handleDelete = useCallback((messageId: string) => {
|
66
|
if (!props.requestPending) {
|
67
|
dispatch(deleteNote({ noteId: messageId }));
|
68
|
}
|
69
|
}, [dispatch, props.requestPending]);
|
70
|
|
71
|
return (
|
72
|
|
73
|
<VStack flex={1} mx={3} >
|
74
|
{/* Notes */}
|
75
|
<FlatList data={notes}
|
76
|
style={{ flex: 1 }}
|
77
|
renderItem={note => (<NoteView higlighted={replyingTo?.messageId == note.item.uuid} key={note.index} note={note.item}
|
78
|
handleReply={
|
79
|
handleReply
|
80
|
}
|
81
|
|
82
|
handleDelete={
|
83
|
(note.item.username == userId || userRole == UserRole.Admin) ?
|
84
|
handleDelete
|
85
|
:
|
86
|
undefined
|
87
|
}
|
88
|
|
89
|
handleEdit={
|
90
|
(note.item.username == userId || userRole == UserRole.Admin) ?
|
91
|
handleEdit
|
92
|
:
|
93
|
undefined
|
94
|
}
|
95
|
setConfirmDialog={setConfirmDialog} navigation={props.navigation} />)}
|
96
|
/>
|
97
|
|
98
|
{/* Create comment */}
|
99
|
{
|
100
|
props.handleCreateComment != null &&
|
101
|
|
102
|
<VStack >
|
103
|
|
104
|
{/* Replying to */}
|
105
|
{replyingTo != null &&
|
106
|
<Flex direction="row" alignItems="center" justify="flex-end" height={10} backgroundColor={"secondary.50"}>
|
107
|
<Text>Replying to {replyingTo.userName}</Text>
|
108
|
<IconButton onPress={() => setReplyingTo(null)} size="sm" icon={<CloseIcon />} marginLeft={-1} marginRight={20} />
|
109
|
</Flex>
|
110
|
}
|
111
|
|
112
|
|
113
|
{/* Add comment */}
|
114
|
|
115
|
<HStack marginLeft={1.5} marginRight={1.5}>
|
116
|
<TextArea
|
117
|
flex={1}
|
118
|
placeholder={userRole == UserRole.Unauthorized || userRole == UserRole.User ? "Unathorized to add comment" : "Add comment"}
|
119
|
isDisabled={userRole == UserRole.Unauthorized || userRole == UserRole.User}
|
120
|
fontSize={14}
|
121
|
value={newComment}
|
122
|
onChangeText={setNewComment}
|
123
|
autoCompleteType={undefined}
|
124
|
/>
|
125
|
|
126
|
<Button size={"md"} disabled={userRole == UserRole.Unauthorized || userRole == UserRole.User} onPress={() => props.handleCreateComment(newComment, replyingTo)} startIcon={<MessageIcon color="#FFF" />} />
|
127
|
|
128
|
</HStack>
|
129
|
</VStack>
|
130
|
|
131
|
}
|
132
|
<ConfirmDialog {...confirmDialog} isShown={confirmDialog != null} cancelRef={cancelRef} />
|
133
|
</VStack >
|
134
|
);
|
135
|
}
|
136
|
|
137
|
export default NotesListView
|