Projekt

Obecné

Profil

Stáhnout (6.16 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { HStack, Box, Text, Image, VStack, Flex, Pressable, DeleteIcon, Button, Center, Avatar, TextArea, AlertDialog } 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 { StyleProp, ViewStyle } from "react-native";
8

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

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

    
14
    const [text, setText] = useState(note.note);
15

    
16
    console.log("rerender");
17

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

    
24
    const getUsernameInitials = (username: string): string => {
25
        if (username) {
26
            const words = username.split(" ");
27
            const initials = words.map(word => word.charAt(0).toUpperCase());
28
            return initials.join("");
29
        }
30
        return "UK"; // Unknown
31
    }
32

    
33
    const toggleEdited = () => {
34
        console.log("Toggle edit");
35
        if (isEdited) {
36
            if (text == note.note) {
37
                setIsEdited(false);
38
            }
39
            else {
40
                // confirm dialog
41
                props.setConfirmDialog({
42
                    headerText: "Cancel Edit",
43
                    bodyText: "Are you sure you want to revert changes on this note?",
44
                    confirmText: "Revert Changes",
45
                    confirmColor: "danger",
46
                    onClose: () => { props.setConfirmDialog(null) },
47
                    onSubmit: () => { setText(note.note); setIsEdited(false); props.setConfirmDialog(null) }
48
                });
49
            }
50
        }
51
        else {
52
            setIsEdited(true);
53
        }
54
    }
55

    
56
    const deleteClicked = () => {
57
        props.setConfirmDialog({
58
            headerText: "Delete Note",
59
            bodyText: "Are you sure you want to delete selected note?",
60
            confirmText: "Delete",
61
            confirmColor: "danger",
62
            onClose: () => { props.setConfirmDialog(null) },
63
            onSubmit: () => { props.handleDelete(note.uuid); props.setConfirmDialog(null) }
64
        });
65
    }
66

    
67
    const saveEdit = () => {
68
        if (note.note == text) {
69
            setIsEdited(false);
70
        }
71
        else {
72
            props.handleEdit({ ...note, note: text });
73
            setIsEdited(false);
74
        }
75
    }
76

    
77
    return (
78
        <HStack marginTop={5}>
79
            <Box width="10" height="10" marginTop="2">
80
                <Avatar bg={note.noteColor} size="sm" source={{
81
                    uri: AVATAR_URL + "/" + note.avatarUrl
82
                        // TODO bude zde nebo v atributu ta připona?
83
                        + ".png"
84
                }}>
85
                    {getUsernameInitials(note.username)}
86
                </Avatar>
87
            </Box>
88
            <VStack flex={1} marginLeft="0.5">
89

    
90
                {/* Top section */}
91
                <HStack justifyContent={"space-between"}>
92
                    <Text bold>
93
                        {note.username}
94
                    </Text>
95
                    <Flex direction="row" alignItems="center" justify="flex-end" >
96
                        <Text italic color="light.500">
97
                            {getDateString(note.createdTime)}
98
                        </Text>
99
                    </Flex>
100
                </HStack>
101

    
102
                {/* Middle section */}
103
                <HStack justifyContent={"space-between"} marginTop={2}>
104
                    {isEdited ?
105
                        <TextArea flex={1} fontSize="14" autoCompleteType={undefined} value={text} onChangeText={setText} />
106
                        :
107
                        <Text flex={1} >
108
                            {text}
109
                        </Text>
110
                    }
111

    
112
                    {note.items && note.items.length > 0 &&
113
                        <Flex marginLeft={1} flex={0}>
114
                            <Button p={1} variant="outline" size="sm" onPress={() => props.navigation.navigate("Item", { itemId: note.items[0] })}>
115
                                {note.items[0]}
116
                            </Button>
117
                        </Flex>
118

    
119
                    }
120
                </HStack>
121

    
122
                {/* Bottom section */}
123
                <HStack justifyContent={"space-between"} marginTop={2}>
124
                    {/* () => props.handleEdit(note.uuid, text) */}
125
                    <Flex direction="row" alignItems="center">
126
                        {props.handleReply != null &&
127
                            <Pressable onPress={() => props.handleReply(note.uuid, note.username)}>
128
                                <Text color="light.500">Reply</Text>
129
                            </Pressable>}
130
                    </Flex>
131

    
132
                    <Flex direction="row" alignItems="center" justify="flex-end">
133
                        {props.handleEdit != null &&
134
                            isEdited ?
135
                            <>
136
                                <Pressable onPress={saveEdit} marginRight={5}>
137
                                    <Text color="light.500">Save</Text>
138
                                </Pressable>
139
                                <Pressable onPress={toggleEdited} marginRight={6}>
140
                                    <Text color="light.500">Cancel</Text>
141
                                </Pressable>
142
                            </>
143
                            :
144
                            <Pressable onPress={toggleEdited} marginRight={5}>
145
                                <EditIcon color="light.500" />
146
                            </Pressable>}
147
                        {props.handleDelete != null &&
148
                            <Pressable onPress={deleteClicked}>
149
                                <DeleteIcon />
150
                            </Pressable>}
151
                    </Flex>
152
                </HStack>
153
            </VStack>
154
        </HStack>
155
    );
156
})
157

    
158
export default NoteView
159

    
(1-1/2)