Projekt

Obecné

Profil

Stáhnout (7.57 KB) Statistiky
| Větev: | Tag: | Revize:
1 a27b3427 Fantič
import { HStack, Box, Text, Image, VStack, Flex, Pressable, DeleteIcon, Button, Center, Avatar, TextArea, AlertDialog, ScrollView } from "native-base";
2 cb25df10 Fantič
import { Note } from "../../types/note";
3 b225ffee Fantič
import { EditIcon } from "../general/Icons";
4
import { AVATAR_URL } from "../../api/constants";
5
import { useState } from "react";
6
import React from "react";
7 a27b3427 Fantič
import NotesListView from "./NotesListView";
8 cb25df10 Fantič
9 8b1106f4 Fantič
const NoteView = React.memo((props: { note: Note, handleReply: any, handleDelete: any, handleEdit: any, setConfirmDialog: any, navigation: any, parentId?: string, higlighted?: boolean }) => {
10 cb25df10 Fantič
    const note = props.note;
11 b225ffee Fantič
12
    const [isEdited, setIsEdited] = useState(false);
13
14 a27b3427 Fantič
    const [showReplies, setShowReplies] = useState(false);
15
16 b225ffee Fantič
    const [text, setText] = useState(note.note);
17
18 8b1106f4 Fantič
    console.log("rerender");
19 56626a63 Fantič
20 bb690a9a Fantič
    const getDateString = (timestamp: Date): string => {
21
        return new Date(timestamp).toLocaleString("en-US",
22 b225ffee Fantič
            { year: "numeric", month: "short", day: "2-digit", hour: "2-digit", minute: "2-digit", hour12: false }
23
        ).replace(/,/g, "").replace(/ /g, "-").replace(/-(?!.*-)/, " ");
24
    }
25
26 51e53a71 Fantič
    const getUsernameInitials = (username: string): string => {
27 3a226033 Fantič
        if (username) {
28
            const words = username.split(" ");
29
            const initials = words.map(word => word.charAt(0).toUpperCase());
30
            return initials.join("");
31
        }
32
        return "UK"; // Unknown
33 b225ffee Fantič
    }
34
35 a27b3427 Fantič
36
    const toggleShowReplies = () => {
37
        console.log("Toggle showReplies");
38
        setShowReplies(!showReplies)
39
    }
40
41 b225ffee Fantič
    const toggleEdited = () => {
42
        console.log("Toggle edit");
43
        if (isEdited) {
44
            if (text == note.note) {
45
                setIsEdited(false);
46
            }
47 bb690a9a Fantič
            else {
48 b225ffee Fantič
                // confirm dialog
49
                props.setConfirmDialog({
50
                    headerText: "Cancel Edit",
51
                    bodyText: "Are you sure you want to revert changes on this note?",
52
                    confirmText: "Revert Changes",
53
                    confirmColor: "danger",
54
                    onClose: () => { props.setConfirmDialog(null) },
55
                    onSubmit: () => { setText(note.note); setIsEdited(false); props.setConfirmDialog(null) }
56
                });
57
            }
58
        }
59
        else {
60
            setIsEdited(true);
61
        }
62
    }
63
64
    const deleteClicked = () => {
65
        props.setConfirmDialog({
66
            headerText: "Delete Note",
67
            bodyText: "Are you sure you want to delete selected note?",
68
            confirmText: "Delete",
69
            confirmColor: "danger",
70
            onClose: () => { props.setConfirmDialog(null) },
71 bb690a9a Fantič
            onSubmit: () => { props.handleDelete(note.uuid); props.setConfirmDialog(null) }
72 b225ffee Fantič
        });
73
    }
74
75
    const saveEdit = () => {
76
        if (note.note == text) {
77
            setIsEdited(false);
78
        }
79
        else {
80 51e53a71 Fantič
            props.handleEdit({ ...note, note: text });
81 b225ffee Fantič
            setIsEdited(false);
82
        }
83
    }
84
85 cb25df10 Fantič
    return (
86 8b1106f4 Fantič
        <HStack style={props.higlighted && {backgroundColor: "#FFF8E1",borderBottomColor: '#FFD54F', borderBottomWidth: 1.5, paddingBottom: 10}} marginTop={5}>
87 51e53a71 Fantič
            <Box width="10" height="10" marginTop="2">
88 b225ffee Fantič
                <Avatar bg={note.noteColor} size="sm" source={{
89 3a226033 Fantič
                    uri: AVATAR_URL + "/" + note.avatarUrl
90
                        // TODO bude zde nebo v atributu ta připona?
91
                        + ".png"
92
                }}>
93 b225ffee Fantič
                    {getUsernameInitials(note.username)}
94
                </Avatar>
95 cb25df10 Fantič
            </Box>
96 51e53a71 Fantič
            <VStack flex={1} marginLeft="0.5">
97
98
                {/* Top section */}
99
                <HStack justifyContent={"space-between"}>
100
                    <Text bold>
101 b225ffee Fantič
                        {note.username}
102
                    </Text>
103
                    <Flex direction="row" alignItems="center" justify="flex-end" >
104
                        <Text italic color="light.500">
105
                            {getDateString(note.createdTime)}
106
                        </Text>
107
                    </Flex>
108
                </HStack>
109 51e53a71 Fantič
110
                {/* Middle section */}
111
                <HStack justifyContent={"space-between"} marginTop={2}>
112 b225ffee Fantič
                    {isEdited ?
113 51e53a71 Fantič
                        <TextArea flex={1} fontSize="14" autoCompleteType={undefined} value={text} onChangeText={setText} />
114 b225ffee Fantič
                        :
115 51e53a71 Fantič
                        <Text flex={1} >
116 b225ffee Fantič
                            {text}
117
                        </Text>
118
                    }
119
120
                    {note.items && note.items.length > 0 &&
121 51e53a71 Fantič
                        <Flex marginLeft={1} flex={0}>
122
                            <Button p={1} variant="outline" size="sm" onPress={() => props.navigation.navigate("Item", { itemId: note.items[0] })}>
123 b225ffee Fantič
                                {note.items[0]}
124
                            </Button>
125
                        </Flex>
126 cb25df10 Fantič
127 b225ffee Fantič
                    }
128
                </HStack>
129 51e53a71 Fantič
130
                {/* Bottom section */}
131
                <HStack justifyContent={"space-between"} marginTop={2}>
132 b225ffee Fantič
                    {/* () => props.handleEdit(note.uuid, text) */}
133 51e53a71 Fantič
                    <Flex direction="row" alignItems="center">
134
                        {props.handleReply != null &&
135
                            <Pressable onPress={() => props.handleReply(note.uuid, note.username)}>
136
                                <Text color="light.500">Reply</Text>
137
                            </Pressable>}
138 a27b3427 Fantič
                        {!props.parentId && note.replies && note.replies.length > 0 &&
139
                            <Pressable marginLeft={3} onPress={() => toggleShowReplies()}>
140
                                {!showReplies ?
141
                                    <Text color="light.500">{note.replies.length} replies</Text>
142
                                    :
143
                                    <Text color="light.500">Hide</Text>
144
                                }
145
146
                            </Pressable>}
147 51e53a71 Fantič
                    </Flex>
148
149
                    <Flex direction="row" alignItems="center" justify="flex-end">
150 b225ffee Fantič
                        {props.handleEdit != null &&
151
                            isEdited ?
152
                            <>
153 51e53a71 Fantič
                                <Pressable onPress={saveEdit} marginRight={5}>
154 b225ffee Fantič
                                    <Text color="light.500">Save</Text>
155
                                </Pressable>
156 51e53a71 Fantič
                                <Pressable onPress={toggleEdited} marginRight={6}>
157 b225ffee Fantič
                                    <Text color="light.500">Cancel</Text>
158
                                </Pressable>
159
                            </>
160
                            :
161 51e53a71 Fantič
                            <Pressable onPress={toggleEdited} marginRight={5}>
162 b225ffee Fantič
                                <EditIcon color="light.500" />
163
                            </Pressable>}
164
                        {props.handleDelete != null &&
165
                            <Pressable onPress={deleteClicked}>
166
                                <DeleteIcon />
167
                            </Pressable>}
168
                    </Flex>
169
                </HStack>
170 a27b3427 Fantič
                {showReplies &&
171
                    <VStack style={{borderLeftColor: '#FFC107', borderLeftWidth: 1, paddingLeft: 5, marginTop: 5}}>
172
                        {note.replies?.map((note, index) => {
173
                            return (
174
                                <NoteView key={index} note={note} handleReply={null} handleDelete={props.handleDelete} handleEdit={props.handleEdit} setConfirmDialog={props.setConfirmDialog} navigation={props.navigation} />
175
                            )
176
                        })}
177
                    </VStack>
178
                }
179 b225ffee Fantič
            </VStack>
180 cb25df10 Fantič
        </HStack>
181
    );
182 56626a63 Fantič
})
183 cb25df10 Fantič
184
export default NoteView