Projekt

Obecné

Profil

Stáhnout (5.61 KB) Statistiky
| Větev: | Tag: | Revize:
1 b225ffee Fantič
import { HStack, Box, Text, Image, VStack, Flex, Pressable, DeleteIcon, Button, Center, Avatar, TextArea, AlertDialog } from "native-base";
2 cb25df10 Fantič
import { Note } from "../../types/note";
3 b225ffee Fantič
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 cb25df10 Fantič
9 b225ffee Fantič
const NoteView = (props: { note: Note, handleReply: any, handleDelete: any, handleEdit: any, setConfirmDialog: any }) => {
10 cb25df10 Fantič
    const note = props.note;
11 b225ffee Fantič
12
    const [isEdited, setIsEdited] = useState(false);
13
14
    const [text, setText] = useState(note.note);
15
16
    const getDateString = (date: Date): string => {
17
        return date.toLocaleDateString("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
        const words = username.split(" ");
24
        const initials = words.map(word => word.charAt(0).toUpperCase());
25
        return initials.join("");
26
    }
27
28
    const toggleEdited = () => {
29
        console.log("Toggle edit");
30
        if (isEdited) {
31
            if (text == note.note) {
32
                setIsEdited(false);
33
            }
34
            else{
35
                // confirm dialog
36
                props.setConfirmDialog({
37
                    headerText: "Cancel Edit",
38
                    bodyText: "Are you sure you want to revert changes on this note?",
39
                    confirmText: "Revert Changes",
40
                    confirmColor: "danger",
41
                    onClose: () => { props.setConfirmDialog(null) },
42
                    onSubmit: () => { setText(note.note); setIsEdited(false); props.setConfirmDialog(null) }
43
                });
44
            }
45
        }
46
        else {
47
            setIsEdited(true);
48
        }
49
    }
50
51
    const deleteClicked = () => {
52
        props.setConfirmDialog({
53
            headerText: "Delete Note",
54
            bodyText: "Are you sure you want to delete selected note?",
55
            confirmText: "Delete",
56
            confirmColor: "danger",
57
            onClose: () => { props.setConfirmDialog(null) },
58
            onSubmit: () => { props.handleDelete(note.uuid);props.setConfirmDialog(null) }
59
        });
60
    }
61
62
    const saveEdit = () => {
63
        if (note.note == text) {
64
            setIsEdited(false);
65
        }
66
        else {
67
            props.handleEdit(note.uuid, text);
68
            // todo
69
            setIsEdited(false);
70
        }
71
    }
72
73 cb25df10 Fantič
    return (
74 b225ffee Fantič
        <HStack marginTop="5%">
75
            <Box width="10" height="10" marginTop="2.5%">
76
                <Avatar bg={note.noteColor} size="sm" source={{
77
                    uri: AVATAR_URL + "/" + note.avatarUrl
78
                }}>
79
                    {getUsernameInitials(note.username)}
80
                </Avatar>
81 cb25df10 Fantič
            </Box>
82 b225ffee Fantič
            <VStack width="97.5%">
83
                <HStack>
84
                    <Text bold width="60%">
85
                        {note.username}
86
                    </Text>
87
                    <Flex direction="row" alignItems="center" justify="flex-end" >
88
                        <Text italic color="light.500">
89
                            {getDateString(note.createdTime)}
90
                        </Text>
91
                    </Flex>
92
                </HStack>
93
                <HStack>
94
                    {isEdited ?
95
                        <TextArea fontSize="14" width="72.5%" autoCompleteType={undefined} value={text} onChangeText={setText} />
96
                        :
97
                        <Text width="72.5%">
98
                            {text}
99
                        </Text>
100
                    }
101
102
                    {note.items && note.items.length > 0 &&
103
                        <Flex marginLeft="1.5%" direction="column" alignItems="flex-start" justify="flex-start" >
104
                            <Button width={"16"} variant="outline" size="sm">
105
                                {note.items[0]}
106
                            </Button>
107
                        </Flex>
108 cb25df10 Fantič
109 b225ffee Fantič
                    }
110
                </HStack>
111
                <HStack>
112
                    {/* () => props.handleEdit(note.uuid, text) */}
113
                    {props.handleReply != null &&
114
                        <Pressable onPress={() => props.handleReply(note.uuid, note.username)}>
115
                            <Text color="light.500">Reply</Text>
116
                        </Pressable>}
117
                    <Flex width="82.5%" direction="row" alignItems="center" justify="flex-end" marginTop="0.5%">
118
                        {props.handleEdit != null &&
119
                            isEdited ?
120
                            <>
121
                                <Pressable onPress={saveEdit} marginRight="5%">
122
                                    <Text color="light.500">Save</Text>
123
                                </Pressable>
124
                                <Pressable onPress={toggleEdited} marginRight="18.5%">
125
                                    <Text color="light.500">Cancel</Text>
126
                                </Pressable>
127
                            </>
128
                            :
129
                            <Pressable onPress={toggleEdited} marginRight={"5%"}>
130
                                <EditIcon color="light.500" />
131
                            </Pressable>}
132
                        {props.handleDelete != null &&
133
                            <Pressable onPress={deleteClicked}>
134
                                <DeleteIcon />
135
                            </Pressable>}
136
                    </Flex>
137
                </HStack>
138
            </VStack>
139 cb25df10 Fantič
        </HStack>
140
    );
141
}
142
143
export default NoteView