Projekt

Obecné

Profil

Stáhnout (5.72 KB) Statistiky
| Větev: | Tag: | Revize:
1 f22a2126 Michal Schwob
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 cb25df10 Fantič
import { Note } from "../../types/note";
16
import NoteView from "./NoteView";
17 2135f53d Fantič
import { MessageIcon } from "../general/Icons";
18 56626a63 Fantič
import React, { useCallback, useState } from "react";
19 2135f53d Fantič
import { updateNote, deleteNote } from "../../stores/actions/notesThunks";
20
import { ConfirmDialog } from "../general/Dialogs";
21 b56c1e7d Fantič
import { AppDispatch, RootState } from "../../stores/store";
22
import { useDispatch, useSelector } from "react-redux";
23 f386a4fe Fantič
import { InfoToast } from "../toast/InfoToast";
24 b56c1e7d Fantič
import { UserRole } from "../../stores/reducers/userSlice";
25 9abf01c3 Michal Schwob
import {FlatList, Platform} from "react-native"
26 cb25df10 Fantič
27 8b1106f4 Fantič
28
29 9abf01c3 Michal Schwob
const NotesListView = (props: { notes: Note[], navigation: any, handleCreateComment: any, requestPending: boolean }) => {
30 cb25df10 Fantič
    const notes = props.notes;
31 2135f53d Fantič
32 b56c1e7d Fantič
    // 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 2135f53d Fantič
    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 56626a63 Fantič
    const handleReply = useCallback((messageId: string, userName: string) => {
56
        setReplyingTo({ messageId, userName });
57
    }, [setReplyingTo]);
58 b5ac7218 Fantič
59 56626a63 Fantič
    const handleEdit = useCallback((note: Note) => {
60
        if (!props.requestPending) {
61 2135f53d Fantič
            dispatch(updateNote(note));
62 56626a63 Fantič
        }
63
    }, [dispatch, props.requestPending]);
64 b5ac7218 Fantič
65 56626a63 Fantič
    const handleDelete = useCallback((messageId: string) => {
66
        if (!props.requestPending) {
67
            dispatch(deleteNote({ noteId: messageId }));
68
        }
69
    }, [dispatch, props.requestPending]);
70 2135f53d Fantič
71 cb25df10 Fantič
    return (
72 9abf01c3 Michal Schwob
73
        <VStack flex={1} mx={3} >
74 b5ac7218 Fantič
                {/* Notes */}
75 9abf01c3 Michal Schwob
                <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 2135f53d Fantič
98 b5ac7218 Fantič
            {/* Create comment */}
99 cb25df10 Fantič
            {
100 2135f53d Fantič
                props.handleCreateComment != null &&
101 9abf01c3 Michal Schwob
102
                <VStack  >
103 b5ac7218 Fantič
104
                    {/* Replying to */}
105
                    {replyingTo != null &&
106 f22a2126 Michal Schwob
                        <Flex direction="row" alignItems="center" justify="flex-end" height={10} backgroundColor={"secondary.50"}>
107 b5ac7218 Fantič
                            <Text>Replying to {replyingTo.userName}</Text>
108 f22a2126 Michal Schwob
                            <IconButton onPress={() => setReplyingTo(null)} size="sm" icon={<CloseIcon />} marginLeft={-1} marginRight={20} />
109 b5ac7218 Fantič
                        </Flex>
110
                    }
111 bc25708c Fantič
112 b5ac7218 Fantič
113
                    {/* Add comment */}
114 9abf01c3 Michal Schwob
115
                    <HStack  marginLeft={1.5} marginRight={1.5}>
116 b5ac7218 Fantič
                        <TextArea
117
                            flex={1}
118 b56c1e7d Fantič
                            placeholder={userRole == UserRole.Unauthorized || userRole == UserRole.User ? "Unathorized to add comment" : "Add comment"}
119
                            isDisabled={userRole == UserRole.Unauthorized || userRole == UserRole.User}
120 b5ac7218 Fantič
                            fontSize={14}
121
                            value={newComment}
122
                            onChangeText={setNewComment}
123
                            autoCompleteType={undefined}
124 9abf01c3 Michal Schwob
                        />
125 b56c1e7d Fantič
126 9abf01c3 Michal Schwob
                        <Button size={"md"} disabled={userRole == UserRole.Unauthorized || userRole == UserRole.User} onPress={() => props.handleCreateComment(newComment, replyingTo)} startIcon={<MessageIcon color="#FFF" />} />
127 b56c1e7d Fantič
128 b5ac7218 Fantič
                    </HStack>
129
                </VStack>
130 9abf01c3 Michal Schwob
131 cb25df10 Fantič
            }
132 2135f53d Fantič
            <ConfirmDialog {...confirmDialog} isShown={confirmDialog != null} cancelRef={cancelRef} />
133 b5ac7218 Fantič
        </VStack >
134 cb25df10 Fantič
    );
135
}
136
137
export default NotesListView