Revize b225ffee
Přidáno uživatelem Fantič před téměř 2 roky(ů)
src/components/notes/NoteView.tsx | ||
---|---|---|
1 |
import { HStack, Box, Text } from "native-base"; |
|
1 |
import { HStack, Box, Text, Image, VStack, Flex, Pressable, DeleteIcon, Button, Center, Avatar, TextArea, AlertDialog } from "native-base";
|
|
2 | 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"; |
|
3 | 8 |
|
4 |
const NoteView = (props: { note: Note }) => { |
|
9 |
const NoteView = (props: { note: Note, handleReply: any, handleDelete: any, handleEdit: any, setConfirmDialog: any }) => {
|
|
5 | 10 |
const note = props.note; |
11 |
|
|
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 |
|
|
6 | 73 |
return ( |
7 |
<HStack> |
|
8 |
<Box width="5%" /> |
|
9 |
<Box width="30%"> |
|
10 |
<Text bold>{note.username}</Text> |
|
74 |
<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> |
|
11 | 81 |
</Box> |
82 |
<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> |
|
12 | 108 |
|
13 |
<Text>{note.note}</Text> |
|
109 |
} |
|
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> |
|
14 | 139 |
</HStack> |
15 | 140 |
); |
16 | 141 |
} |
Také k dispozici: Unified diff
re #10569: NotesView: interface implementation