Projekt

Obecné

Profil

Stáhnout (5.89 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 { ConfirmDialog } from "../general/Dialogs";
8

    
9
const NoteView = (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
    const getDateString = (timestamp: Date): string => {
17
        return new Date(timestamp).toLocaleString("en-US",
18
            { year: "numeric", month: "short", day: "2-digit", hour: "2-digit", minute: "2-digit", hour12: false }
19
        ).replace(/,/g, "").replace(/ /g, "-").replace(/-(?!.*-)/, " ");
20
    }
21

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

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

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

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

    
75
    return (
76
        <HStack marginTop="5%">
77
            <Box width="10" height="10" marginTop="2.5%">
78
                <Avatar bg={note.noteColor} size="sm" source={{
79
                    uri: AVATAR_URL + "/" + note.avatarUrl
80
                        // TODO bude zde nebo v atributu ta připona?
81
                        + ".png"
82
                }}>
83
                    {getUsernameInitials(note.username)}
84
                </Avatar>
85
            </Box>
86
            <VStack width="97.5%">
87
                <HStack>
88
                    <Text bold width="60%">
89
                        {note.username}
90
                    </Text>
91
                    <Flex direction="row" alignItems="center" justify="flex-end" >
92
                        <Text italic color="light.500">
93
                            {getDateString(note.createdTime)}
94
                        </Text>
95
                    </Flex>
96
                </HStack>
97
                <HStack>
98
                    {isEdited ?
99
                        <TextArea fontSize="14" width="72.5%" autoCompleteType={undefined} value={text} onChangeText={setText} />
100
                        :
101
                        <Text width="72.5%">
102
                            {text}
103
                        </Text>
104
                    }
105

    
106
                    {note.items && note.items.length > 0 &&
107
                        <Flex marginLeft="1.5%" direction="column" alignItems="flex-start" justify="flex-start" >
108
                            <Button width={"16"} variant="outline" size="sm" onPress={() => props.navigation.navigate("Item", { itemId: note.items[0] })}>
109
                                {note.items[0]}
110
                            </Button>
111
                        </Flex>
112

    
113
                    }
114
                </HStack>
115
                <HStack>
116
                    {/* () => props.handleEdit(note.uuid, text) */}
117
                    {props.handleReply != null &&
118
                        <Pressable onPress={() => props.handleReply(note.uuid, note.username)}>
119
                            <Text color="light.500">Reply</Text>
120
                        </Pressable>}
121
                    <Flex width="82.5%" direction="row" alignItems="center" justify="flex-end" marginTop="0.5%">
122
                        {props.handleEdit != null &&
123
                            isEdited ?
124
                            <>
125
                                <Pressable onPress={saveEdit} marginRight="5%">
126
                                    <Text color="light.500">Save</Text>
127
                                </Pressable>
128
                                <Pressable onPress={toggleEdited} marginRight="18.5%">
129
                                    <Text color="light.500">Cancel</Text>
130
                                </Pressable>
131
                            </>
132
                            :
133
                            <Pressable onPress={toggleEdited} marginRight={"5%"}>
134
                                <EditIcon color="light.500" />
135
                            </Pressable>}
136
                        {props.handleDelete != null &&
137
                            <Pressable onPress={deleteClicked}>
138
                                <DeleteIcon />
139
                            </Pressable>}
140
                    </Flex>
141
                </HStack>
142
            </VStack>
143
        </HStack>
144
    );
145
}
146

    
147
export default NoteView
148

    
(1-1/2)