Projekt

Obecné

Profil

Stáhnout (7.6 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { HStack, Box, Text, Image, VStack, Flex, Pressable, DeleteIcon, Button, Center, Avatar, TextArea, AlertDialog, ScrollView } from "native-base";
2
import { Note } from "../../types/note";
3
import { EditIcon } from "../general/Icons";
4
import { AVATAR_URL } from "../../api/constants";
5
import { useState } from "react";
6
import React from "react";
7
import NotesListView from "./NotesListView";
8

    
9
const NoteView = React.memo((props: { note: Note, handleReply: any, handleDelete: any, handleEdit: any, setConfirmDialog: any, navigation: any, parentId?: string, higlighted?: boolean }) => {
10
    const note = props.note;
11

    
12
    const [isEdited, setIsEdited] = useState(false);
13

    
14
    const [showReplies, setShowReplies] = useState(false);
15

    
16
    const [text, setText] = useState(note.note);
17

    
18
    console.log("rerender");
19

    
20
    const getDateString = (timestamp: Date): string => {
21
        return new Date(timestamp).toLocaleString("en-US",
22
            { year: "numeric", month: "short", day: "2-digit", hour: "2-digit", minute: "2-digit", hour12: false }
23
        ).replace(/,/g, "").replace(/ /g, "-").replace(/-(?!.*-)/, " ");
24
    }
25

    
26
    const getUsernameInitials = (username: string): string => {
27
        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
    }
34

    
35

    
36
    const toggleShowReplies = () => {
37
        console.log("Toggle showReplies");
38
        setShowReplies(!showReplies)
39
    }
40

    
41
    const toggleEdited = () => {
42
        console.log("Toggle edit");
43
        if (isEdited) {
44
            if (text == note.note) {
45
                setIsEdited(false);
46
            }
47
            else {
48
                // 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
            onSubmit: () => { props.handleDelete(note.uuid); props.setConfirmDialog(null) }
72
        });
73
    }
74

    
75
    const saveEdit = () => {
76
        if (note.note == text) {
77
            setIsEdited(false);
78
        }
79
        else {
80
            props.handleEdit({ ...note, note: text });
81
            setIsEdited(false);
82
        }
83
    }
84

    
85
    return (
86
        <HStack style={props.higlighted && { backgroundColor: "#FFF8E1", borderBottomColor: '#FFD54F', borderBottomWidth: 1.5, paddingBottom: 10 }} marginTop={5}>
87
            <Box width="12" height="12" marginTop="2">
88
                <Avatar bg={note.noteColor} width={10} height={10} source={{
89
                    uri: AVATAR_URL + "/" + note.avatarUrl
90
                        // TODO bude zde nebo v atributu ta připona?
91
                        + ".png"
92
                }}>
93
                    {getUsernameInitials(note.username)}
94
                </Avatar>
95
            </Box>
96
            <VStack flex={1} marginLeft="0.5">
97

    
98
                {/* Top section */}
99
                <Text bold>
100
                    {note.username}
101
                </Text>
102

    
103
                <Text italic color="#8F8F8F" fontSize="xs">
104
                    {getDateString(note.createdTime)}
105
                </Text>
106

    
107
                {/* Middle section */}
108
                <HStack justifyContent={"space-between"} marginTop={2}>
109
                    {isEdited ?
110
                        <TextArea flex={1} fontSize="14" autoCompleteType={undefined} value={text} onChangeText={setText} />
111
                        :
112
                        <Text flex={1} >
113
                            {text}
114
                        </Text>
115
                    }
116

    
117
                    {note.items && note.items.length > 0 &&
118
                        <Flex marginLeft={1} flex={0}>
119
                            <Button p={1} variant="solid" backgroundColor="#F4DFAB" borderRadius={8} size="sm" onPress={() => props.navigation.navigate("Item", { itemId: note.items[0] })}>
120
                                <Text color="#654B07" fontSize={12}>
121
                                    {note.items[0]}
122
                                </Text>
123
                            </Button>
124
                        </Flex>
125

    
126
                    }
127
                </HStack>
128

    
129
                {/* Bottom section */}
130
                <HStack justifyContent={"space-between"} marginTop={2}>
131
                    {/* () => props.handleEdit(note.uuid, text) */}
132
                    <Flex direction="row" alignItems="center">
133
                        {props.handleReply != null &&
134
                            <Pressable onPress={() => props.handleReply(note.uuid, note.username)}>
135
                                <Text color="#8F8F8F">Reply</Text>
136
                            </Pressable>}
137
                        {!props.parentId && note.replies && note.replies.length > 0 &&
138
                            <Pressable marginLeft={3} onPress={() => toggleShowReplies()}>
139
                                {!showReplies ?
140
                                    <Text color="#8F8F8F">{note.replies.length} replies</Text>
141
                                    :
142
                                    <Text color="#8F8F8F">Hide</Text>
143
                                }
144

    
145
                            </Pressable>}
146
                    </Flex>
147

    
148
                    <Flex direction="row" alignItems="center" justify="flex-end">
149
                        {props.handleEdit &&
150
                            isEdited &&
151
                            <>
152
                                < Pressable onPress={saveEdit} marginRight={5}>
153
                                    <Text color="#8F8F8F">Save</Text>
154
                                </Pressable>
155
                                <Pressable onPress={toggleEdited} marginRight={6}>
156
                                    <Text color="#8F8F8F">Cancel</Text>
157
                                </Pressable>
158
                            </>}
159
                        {props.handleEdit &&
160
                            !isEdited &&
161
                            <Pressable onPress={toggleEdited} marginRight={5}>
162
                                <EditIcon color="#8F8F8F" />
163
                            </Pressable>}
164
                        {props.handleDelete &&
165
                            <Pressable onPress={deleteClicked}>
166
                                <DeleteIcon />
167
                            </Pressable>
168
                        }
169

    
170
                    </Flex>
171
                </HStack>
172
                {
173
                    showReplies &&
174
                    <VStack style={{ borderLeftColor: '#FFC107', borderLeftWidth: 1, paddingLeft: 5, marginTop: 5 }}>
175
                        {note.replies?.map((note, index) => {
176
                            return (
177
                                <NoteView key={index} note={note} handleReply={null} handleDelete={props.handleDelete} handleEdit={props.handleEdit} setConfirmDialog={props.setConfirmDialog} navigation={props.navigation} />
178
                            )
179
                        })}
180
                    </VStack>
181
                }
182
            </VStack >
183
        </HStack >
184
    );
185
})
186

    
187
export default NoteView
188

    
(1-1/2)