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 |
3a226033
|
Fantič
|
const NoteView = (props: { note: Note, handleReply: any, handleDelete: any, handleEdit: any, setConfirmDialog: any, navigation: 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 |
bb690a9a
|
Fantič
|
const getDateString = (timestamp: Date): string => {
|
17 |
|
|
return new Date(timestamp).toLocaleString("en-US",
|
18 |
b225ffee
|
Fantič
|
{ year: "numeric", month: "short", day: "2-digit", hour: "2-digit", minute: "2-digit", hour12: false }
|
19 |
|
|
).replace(/,/g, "").replace(/ /g, "-").replace(/-(?!.*-)/, " ");
|
20 |
|
|
}
|
21 |
|
|
|
22 |
3a226033
|
Fantič
|
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 |
b225ffee
|
Fantič
|
}
|
30 |
|
|
|
31 |
|
|
const toggleEdited = () => {
|
32 |
|
|
console.log("Toggle edit");
|
33 |
|
|
if (isEdited) {
|
34 |
|
|
if (text == note.note) {
|
35 |
|
|
setIsEdited(false);
|
36 |
|
|
}
|
37 |
bb690a9a
|
Fantič
|
else {
|
38 |
b225ffee
|
Fantič
|
// 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 |
bb690a9a
|
Fantič
|
onSubmit: () => { props.handleDelete(note.uuid); props.setConfirmDialog(null) }
|
62 |
b225ffee
|
Fantič
|
});
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
const saveEdit = () => {
|
66 |
|
|
if (note.note == text) {
|
67 |
|
|
setIsEdited(false);
|
68 |
|
|
}
|
69 |
|
|
else {
|
70 |
3a226033
|
Fantič
|
props.handleEdit({...note, note: text});
|
71 |
b225ffee
|
Fantič
|
setIsEdited(false);
|
72 |
|
|
}
|
73 |
|
|
}
|
74 |
|
|
|
75 |
cb25df10
|
Fantič
|
return (
|
76 |
b225ffee
|
Fantič
|
<HStack marginTop="5%">
|
77 |
|
|
<Box width="10" height="10" marginTop="2.5%">
|
78 |
|
|
<Avatar bg={note.noteColor} size="sm" source={{
|
79 |
3a226033
|
Fantič
|
uri: AVATAR_URL + "/" + note.avatarUrl
|
80 |
|
|
// TODO bude zde nebo v atributu ta připona?
|
81 |
|
|
+ ".png"
|
82 |
|
|
}}>
|
83 |
b225ffee
|
Fantič
|
{getUsernameInitials(note.username)}
|
84 |
|
|
</Avatar>
|
85 |
cb25df10
|
Fantič
|
</Box>
|
86 |
b225ffee
|
Fantič
|
<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 |
3a226033
|
Fantič
|
<Button width={"16"} variant="outline" size="sm" onPress={() => props.navigation.navigate("Item", { itemId: note.items[0] })}>
|
109 |
b225ffee
|
Fantič
|
{note.items[0]}
|
110 |
|
|
</Button>
|
111 |
|
|
</Flex>
|
112 |
cb25df10
|
Fantič
|
|
113 |
b225ffee
|
Fantič
|
}
|
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 |
cb25df10
|
Fantič
|
</HStack>
|
144 |
|
|
);
|
145 |
|
|
}
|
146 |
|
|
|
147 |
|
|
export default NoteView
|