Projekt

Obecné

Profil

Stáhnout (7.69 KB) Statistiky
| Větev: | Tag: | Revize:
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

    
26

    
27

    
28
const NotesListView = (props: { notes: Note[], navigation: any, handleCreateComment: any, requestPending: boolean, height: number }) => {
29
    const notes = props.notes;
30

    
31
    // const userRole = useSelector((state: RootState) => state.user.role)
32
    const userId = useSelector((state: RootState) => state.user.userId)
33
    const userRole = useSelector((state: RootState) => state.user.role)
34

    
35
    console.log(userRole)
36

    
37
    const [newComment, setNewComment] = useState<string>("");
38
    const [replyingTo, setReplyingTo] = useState<{ messageId: string; userName: string } | null>(null);
39

    
40
    const toast = useToast();
41

    
42
    const [renderedNotes, setRenderedNotes] = useState(6);
43

    
44
    const cancelRef = React.useRef(null);
45

    
46
    const dispatch = useDispatch<AppDispatch>();
47

    
48
    const [confirmDialog, setConfirmDialog] = React.useState<{
49
        headerText?: string;
50
        bodyText?: string;
51
        confirmText?: string;
52
        confirmColor?: string;
53
        cancelRef?: any;
54
        onClose?: () => void;
55
        onSubmit?: () => void;
56
    } | null>(null);
57

    
58
    const handleReply = useCallback((messageId: string, userName: string) => {
59
        setReplyingTo({ messageId, userName });
60
    }, [setReplyingTo]);
61

    
62
    const handleEdit = useCallback((note: Note) => {
63
        if (!props.requestPending) {
64
            dispatch(updateNote(note));
65
        }
66
    }, [dispatch, props.requestPending]);
67

    
68
    const handleDelete = useCallback((messageId: string) => {
69
        if (!props.requestPending) {
70
            dispatch(deleteNote({ noteId: messageId }));
71
        }
72
    }, [dispatch, props.requestPending]);
73

    
74

    
75

    
76
    const handleScrollEnd = (event: any) => {
77
        const offsetY = event.nativeEvent.contentOffset.y;
78
        const contentHeight = event.nativeEvent.contentSize.height;
79
        const screenHeight = event.nativeEvent.layoutMeasurement.height;
80

    
81
        if (offsetY + screenHeight >= contentHeight - 20) {
82
            // Load more notes when the user is near the end of the list
83
            const nextBatchSize = 5; // Number of notes to render in the next batch
84

    
85
            if ((renderedNotes + nextBatchSize) > notes.length && (renderedNotes + nextBatchSize) != notes.length) {
86
                toast.closeAll()
87
                toast.show({
88
                    render: ({
89
                        id
90
                    }) => {
91
                        return <InfoToast text={"All notes loaded..."} onClose={() => toast.close(id)} />;
92
                    },
93
                    duration: 3000
94
                });
95
                setRenderedNotes(notes.length);
96

    
97
            }
98
            else if ((renderedNotes + nextBatchSize) != notes.length) {
99
                toast.closeAll()
100
                toast.show({
101
                    render: ({
102
                        id
103
                    }) => {
104
                        return <InfoToast text={"Loading more notes..."} onClose={() => toast.close(id)} />;
105
                    },
106
                    duration: 3000
107
                });
108
                setRenderedNotes(renderedNotes + nextBatchSize);
109

    
110
            }
111
        }
112
    };
113

    
114
    return (
115
        <KeyboardAvoidingView
116
            h={ {lg: "auto"} }
117
            behavior={ "padding" }
118
            keyboardVerticalOffset={ 600 }
119
            mb={ 2 }
120
        >
121
        <VStack >
122
            <Box height={props.height - (replyingTo ? 40 : 0) ?? undefined} marginBottom={2}>
123
                {/* Notes */}
124
                <ScrollView contentContainerStyle={{ flexGrow: 1 }} onScrollEndDrag={handleScrollEnd} paddingLeft={2.5} paddingRight={2.5}>
125
                    {
126
                        notes && notes.length > 0 ? (
127
                            <VStack>
128
                                {
129
                                    notes.slice(0, renderedNotes).map((note, index) => {
130

    
131
                                        return <NoteView higlighted={replyingTo?.messageId == note.uuid} key={index} note={note}
132
                                            handleReply={
133
                                                handleReply
134
                                            }
135

    
136
                                            handleDelete={
137
                                                (note.username == userId || userRole == UserRole.Admin) ?
138
                                                    handleDelete
139
                                                    :
140
                                                    undefined
141
                                            }
142

    
143
                                            handleEdit={
144
                                                (note.username == userId || userRole == UserRole.Admin) ?
145
                                                    handleEdit
146
                                                    :
147
                                                    undefined
148
                                            } 
149
                                            setConfirmDialog={setConfirmDialog} navigation={props.navigation} />;
150

    
151
                                    })
152
                                }
153
                            </VStack>
154
                        ) : (
155
                            <Text>There are no notes.</Text>
156
                        )
157
                    }
158
                </ScrollView>
159
            </Box>
160

    
161
            {/* Create comment */}
162
            {
163
                props.handleCreateComment != null &&
164
                <VStack>
165

    
166
                    {/* Replying to */}
167
                    {replyingTo != null &&
168
                        <Flex direction="row" alignItems="center" justify="flex-end" height={10} backgroundColor={"secondary.50"}>
169
                            <Text>Replying to {replyingTo.userName}</Text>
170
                            <IconButton onPress={() => setReplyingTo(null)} size="sm" icon={<CloseIcon />} marginLeft={-1} marginRight={20} />
171
                        </Flex>
172
                    }
173

    
174

    
175
                    {/* Add comment */}
176
                    <HStack height={90} marginLeft={1.5} marginRight={1.5}>
177
                        <TextArea
178
                            flex={1}
179
                            placeholder={userRole == UserRole.Unauthorized || userRole == UserRole.User ? "Unathorized to add comment" : "Add comment"}
180
                            isDisabled={userRole == UserRole.Unauthorized || userRole == UserRole.User}
181
                            fontSize={14}
182
                            value={newComment}
183
                            onChangeText={setNewComment}
184
                            autoCompleteType={undefined}
185

    
186

    
187
                        />
188
                        <Flex marginLeft={2} marginBottom={8}>
189
                            <Button disabled={userRole == UserRole.Unauthorized || userRole == UserRole.User} onPress={() => props.handleCreateComment(newComment, replyingTo)} startIcon={<MessageIcon color="#FFF" />} />
190
                        </Flex>
191
                    </HStack>
192
                </VStack>
193
            }
194
            <ConfirmDialog {...confirmDialog} isShown={confirmDialog != null} cancelRef={cancelRef} />
195
        </VStack >
196
        </KeyboardAvoidingView>
197
    );
198
}
199

    
200
export default NotesListView
(2-2/2)