Projekt

Obecné

Profil

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