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
|
note.items = ["helo"]
|
78
|
|
79
|
return (
|
80
|
<HStack marginTop={5}>
|
81
|
<Box width="10" height="10" marginTop="2">
|
82
|
<Avatar bg={note.noteColor} size="sm" source={{
|
83
|
uri: AVATAR_URL + "/" + note.avatarUrl
|
84
|
// TODO bude zde nebo v atributu ta připona?
|
85
|
+ ".png"
|
86
|
}}>
|
87
|
{getUsernameInitials(note.username)}
|
88
|
</Avatar>
|
89
|
</Box>
|
90
|
<VStack flex={1} marginLeft="0.5">
|
91
|
|
92
|
{/* Top section */}
|
93
|
<HStack justifyContent={"space-between"}>
|
94
|
<Text bold>
|
95
|
{note.username}
|
96
|
</Text>
|
97
|
<Flex direction="row" alignItems="center" justify="flex-end" >
|
98
|
<Text italic color="light.500">
|
99
|
{getDateString(note.createdTime)}
|
100
|
</Text>
|
101
|
</Flex>
|
102
|
</HStack>
|
103
|
|
104
|
{/* Middle section */}
|
105
|
<HStack justifyContent={"space-between"} marginTop={2}>
|
106
|
{isEdited ?
|
107
|
<TextArea flex={1} fontSize="14" autoCompleteType={undefined} value={text} onChangeText={setText} />
|
108
|
:
|
109
|
<Text flex={1} >
|
110
|
{text}
|
111
|
</Text>
|
112
|
}
|
113
|
|
114
|
{note.items && note.items.length > 0 &&
|
115
|
<Flex marginLeft={1} flex={0}>
|
116
|
<Button p={1} variant="outline" size="sm" onPress={() => props.navigation.navigate("Item", { itemId: note.items[0] })}>
|
117
|
{note.items[0]}
|
118
|
</Button>
|
119
|
</Flex>
|
120
|
|
121
|
}
|
122
|
</HStack>
|
123
|
|
124
|
{/* Bottom section */}
|
125
|
<HStack justifyContent={"space-between"} marginTop={2}>
|
126
|
{/* () => props.handleEdit(note.uuid, text) */}
|
127
|
<Flex direction="row" alignItems="center">
|
128
|
{props.handleReply != null &&
|
129
|
<Pressable onPress={() => props.handleReply(note.uuid, note.username)}>
|
130
|
<Text color="light.500">Reply</Text>
|
131
|
</Pressable>}
|
132
|
</Flex>
|
133
|
|
134
|
<Flex direction="row" alignItems="center" justify="flex-end">
|
135
|
{props.handleEdit != null &&
|
136
|
isEdited ?
|
137
|
<>
|
138
|
<Pressable onPress={saveEdit} marginRight={5}>
|
139
|
<Text color="light.500">Save</Text>
|
140
|
</Pressable>
|
141
|
<Pressable onPress={toggleEdited} marginRight={6}>
|
142
|
<Text color="light.500">Cancel</Text>
|
143
|
</Pressable>
|
144
|
</>
|
145
|
:
|
146
|
<Pressable onPress={toggleEdited} marginRight={5}>
|
147
|
<EditIcon color="light.500" />
|
148
|
</Pressable>}
|
149
|
{props.handleDelete != null &&
|
150
|
<Pressable onPress={deleteClicked}>
|
151
|
<DeleteIcon />
|
152
|
</Pressable>}
|
153
|
</Flex>
|
154
|
</HStack>
|
155
|
</VStack>
|
156
|
</HStack>
|
157
|
);
|
158
|
})
|
159
|
|
160
|
export default NoteView
|
161
|
|