Projekt

Obecné

Profil

Stáhnout (7.73 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="solid" backgroundColor="#F4DFAB" borderRadius={8} size="sm" onPress={() => props.navigation.navigate("Item", { itemId: note.items[0] })}>
123
                                <Text color="#654B07" fontSize={12}>
124
                                    {note.items[0]}
125
                                </Text>
126
                            </Button>
127
                        </Flex>
128

    
129
                    }
130
                </HStack>
131

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

    
148
                            </Pressable>}
149
                    </Flex>
150

    
151
                    <Flex direction="row" alignItems="center" justify="flex-end">
152
                        {props.handleEdit != null &&
153
                            isEdited ?
154
                            <>
155
                                <Pressable onPress={saveEdit} marginRight={5}>
156
                                    <Text color="light.500">Save</Text>
157
                                </Pressable>
158
                                <Pressable onPress={toggleEdited} marginRight={6}>
159
                                    <Text color="light.500">Cancel</Text>
160
                                </Pressable>
161
                            </>
162
                            :
163
                            <Pressable onPress={toggleEdited} marginRight={5}>
164
                                <EditIcon color="light.500" />
165
                            </Pressable>}
166
                        {props.handleDelete != null &&
167
                            <Pressable onPress={deleteClicked}>
168
                                <DeleteIcon />
169
                            </Pressable>}
170
                    </Flex>
171
                </HStack>
172
                {showReplies &&
173
                    <VStack style={{ borderLeftColor: '#FFC107', borderLeftWidth: 1, paddingLeft: 5, marginTop: 5 }}>
174
                        {note.replies?.map((note, index) => {
175
                            return (
176
                                <NoteView key={index} note={note} handleReply={null} handleDelete={props.handleDelete} handleEdit={props.handleEdit} setConfirmDialog={props.setConfirmDialog} navigation={props.navigation} />
177
                            )
178
                        })}
179
                    </VStack>
180
                }
181
            </VStack>
182
        </HStack>
183
    );
184
})
185

    
186
export default NoteView
187

    
(1-1/2)