Projekt

Obecné

Profil

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