Projekt

Obecné

Profil

Stáhnout (7.57 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="10" height="10" marginTop="2">
88
                <Avatar bg={note.noteColor} size="sm" 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
                <HStack justifyContent={"space-between"}>
100
                    <Text bold>
101
                        {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

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

    
120
                    {note.items && note.items.length > 0 &&
121
                        <Flex marginLeft={1} flex={0}>
122
                            <Button p={1} variant="outline" size="sm" onPress={() => props.navigation.navigate("Item", { itemId: note.items[0] })}>
123
                                {note.items[0]}
124
                            </Button>
125
                        </Flex>
126

    
127
                    }
128
                </HStack>
129

    
130
                {/* Bottom section */}
131
                <HStack justifyContent={"space-between"} marginTop={2}>
132
                    {/* () => props.handleEdit(note.uuid, text) */}
133
                    <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
                        {!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
                    </Flex>
148

    
149
                    <Flex direction="row" alignItems="center" justify="flex-end">
150
                        {props.handleEdit != null &&
151
                            isEdited ?
152
                            <>
153
                                <Pressable onPress={saveEdit} marginRight={5}>
154
                                    <Text color="light.500">Save</Text>
155
                                </Pressable>
156
                                <Pressable onPress={toggleEdited} marginRight={6}>
157
                                    <Text color="light.500">Cancel</Text>
158
                                </Pressable>
159
                            </>
160
                            :
161
                            <Pressable onPress={toggleEdited} marginRight={5}>
162
                                <EditIcon color="light.500" />
163
                            </Pressable>}
164
                        {props.handleDelete != null &&
165
                            <Pressable onPress={deleteClicked}>
166
                                <DeleteIcon />
167
                            </Pressable>}
168
                    </Flex>
169
                </HStack>
170
                {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
            </VStack>
180
        </HStack>
181
    );
182
})
183

    
184
export default NoteView
185

    
(1-1/2)