1
|
import React, { useCallback, useEffect, useState } from "react"
|
2
|
import { log } from "../logging/logger"
|
3
|
import { DrawerScreenProps } from "@react-navigation/drawer"
|
4
|
import { Box, Button, ChevronDownIcon, ChevronUpIcon, Flex, HStack, MinusIcon, Popover, Switch, Text, VStack, useToast } from "native-base"
|
5
|
import { SortOptions } from "../types/general"
|
6
|
import { Note } from "../types/note"
|
7
|
import NotesListView from "../components/notes/NotesListView"
|
8
|
import { useDispatch, useSelector } from "react-redux"
|
9
|
import { AppDispatch, RootState } from "../stores/store"
|
10
|
import LoadingBox from "../components/loading/LoadingBox"
|
11
|
import { createNote, getAllNotes } from "../stores/actions/notesThunks"
|
12
|
import { RootDrawerParamList } from "./Navigation"
|
13
|
import { ErrorToast } from "../components/toast/ErrorToast"
|
14
|
import { InfoToast } from "../components/toast/InfoToast"
|
15
|
|
16
|
import { Dimensions } from "react-native";
|
17
|
|
18
|
const NotesViewPage = ({ navigation }: DrawerScreenProps<RootDrawerParamList, 'Notes'>) => {
|
19
|
|
20
|
const { notes, notesLoading, requestPending, triggerRefresh } = useSelector((state: RootState) => state.noteViewState);
|
21
|
|
22
|
const dispatch = useDispatch<AppDispatch>();
|
23
|
|
24
|
const [isSortOpen, setIsSortOpen] = useState(false);
|
25
|
|
26
|
const [myCommentsCheck, setMyCommentsCheck] = useState(false);
|
27
|
const [generalCommentsCheck, setGeneralCommentsCheck] = useState(true);
|
28
|
|
29
|
const lastError = useSelector((state: RootState) => state.noteViewState.lastError)
|
30
|
const toast = useToast();
|
31
|
|
32
|
useEffect(() => {
|
33
|
if (lastError) {
|
34
|
toast.closeAll()
|
35
|
toast.show({
|
36
|
render: ({
|
37
|
id
|
38
|
}) => {
|
39
|
return <ErrorToast headerText={"Error"} text={lastError} onClose={() => toast.close(id)} />;
|
40
|
},
|
41
|
duration: 3000
|
42
|
});
|
43
|
}
|
44
|
}, [lastError])
|
45
|
|
46
|
const [sortSettings, setSortSettings] = useState({
|
47
|
items: SortOptions.None,
|
48
|
date: SortOptions.None
|
49
|
});
|
50
|
|
51
|
const handleSortChanged = useCallback((key: "items" | "date", sortSettings: any) => {
|
52
|
let currentState = sortSettings[key];
|
53
|
if (currentState == SortOptions.None) {
|
54
|
setSortSettings({
|
55
|
...sortSettings,
|
56
|
[key]: SortOptions.Asc
|
57
|
})
|
58
|
}
|
59
|
else if (currentState == SortOptions.Asc) {
|
60
|
setSortSettings({
|
61
|
...sortSettings,
|
62
|
[key]: SortOptions.Desc
|
63
|
})
|
64
|
}
|
65
|
else {
|
66
|
setSortSettings({
|
67
|
...sortSettings,
|
68
|
[key]: SortOptions.None
|
69
|
})
|
70
|
}
|
71
|
}, [setSortSettings]);
|
72
|
|
73
|
const getSortIcon = (sortOption: SortOptions, size: string) => {
|
74
|
switch (sortOption) {
|
75
|
case SortOptions.Asc:
|
76
|
return <ChevronUpIcon size={size} />;
|
77
|
case SortOptions.Desc:
|
78
|
return <ChevronDownIcon size={size} />;
|
79
|
default:
|
80
|
return <MinusIcon size={size} />;
|
81
|
}
|
82
|
};
|
83
|
|
84
|
// trigger refresh on triggerRefresh increment
|
85
|
useEffect(() => {
|
86
|
log.debug("NotesViewPage", "useEffect", "getNotes");
|
87
|
getNotes();
|
88
|
}, [triggerRefresh])
|
89
|
|
90
|
// general comments check changed
|
91
|
useEffect(() => {
|
92
|
if (!notesLoading) {
|
93
|
log.debug("NotesViewPage", "useEffect", "getNotes");
|
94
|
getNotes();
|
95
|
}
|
96
|
}, [generalCommentsCheck, myCommentsCheck])
|
97
|
|
98
|
|
99
|
const getNotes = useCallback(() => {
|
100
|
dispatch(getAllNotes({
|
101
|
myComments: myCommentsCheck,
|
102
|
generalComments: generalCommentsCheck,
|
103
|
sortOptions: sortSettings
|
104
|
}));
|
105
|
}, [myCommentsCheck, generalCommentsCheck, sortSettings]);
|
106
|
|
107
|
|
108
|
const handleCreateComment = useCallback((newComment: string, replyingTo: { messageId: string; userName: string } | null) => {
|
109
|
console.log(newComment, replyingTo)
|
110
|
|
111
|
if (!requestPending) {
|
112
|
// creating new comment
|
113
|
toast.show({
|
114
|
render: ({
|
115
|
id
|
116
|
}) => {
|
117
|
return <InfoToast text={"Adding note"} onClose={() => toast.close(id)} />;
|
118
|
},
|
119
|
duration: 2000
|
120
|
});
|
121
|
if (replyingTo != null) {
|
122
|
dispatch(createNote({
|
123
|
newNote: { note: newComment, reply_to: replyingTo.messageId } as Note
|
124
|
}))
|
125
|
}
|
126
|
else {
|
127
|
dispatch(createNote({
|
128
|
newNote: { note: newComment } as Note
|
129
|
}))
|
130
|
}
|
131
|
}
|
132
|
}, [dispatch, createNote]);
|
133
|
|
134
|
|
135
|
|
136
|
return (
|
137
|
<Box marginLeft={2.5} marginRight={2.5} marginTop={2.5}>
|
138
|
<VStack>
|
139
|
<HStack height={75} justifyContent="space-between">
|
140
|
<Box justifyContent={"center"}>
|
141
|
<Popover // @ts-ignore
|
142
|
trigger={triggerProps => {
|
143
|
return <Button colorScheme="primary" {...triggerProps} onPress={() => setIsSortOpen(true)}>
|
144
|
Sort Options
|
145
|
</Button>;
|
146
|
}} isOpen={isSortOpen} onClose={() => setIsSortOpen(!isSortOpen)}>
|
147
|
<Popover.Content w="48">
|
148
|
<Popover.Arrow />
|
149
|
<Popover.CloseButton onPress={() => setIsSortOpen(false)} />
|
150
|
<Popover.Header>Sort Options</Popover.Header>
|
151
|
<Popover.Body>
|
152
|
<VStack>
|
153
|
<Button size="md" variant="outline" onPress={() => handleSortChanged("items", sortSettings)}
|
154
|
rightIcon={getSortIcon(sortSettings.items, "sm")}
|
155
|
>
|
156
|
Items</Button>
|
157
|
<Button size="md" variant="outline" marginTop={"5%"} onPress={() => handleSortChanged("date", sortSettings)}
|
158
|
rightIcon={getSortIcon(sortSettings.date, "sm")}>
|
159
|
Date</Button>
|
160
|
</VStack>
|
161
|
</Popover.Body>
|
162
|
<Popover.Footer justifyContent="flex-end">
|
163
|
<Button.Group space={2}>
|
164
|
<Button colorScheme="coolGray" variant="ghost" onPress={() => setIsSortOpen(false)}>
|
165
|
Cancel
|
166
|
</Button>
|
167
|
<Button colorScheme="success" onPress={() => { setIsSortOpen(false); getNotes() }}>
|
168
|
Save
|
169
|
</Button>
|
170
|
</Button.Group>
|
171
|
</Popover.Footer>
|
172
|
</Popover.Content>
|
173
|
</Popover>
|
174
|
</Box>
|
175
|
|
176
|
<VStack space={0.5}>
|
177
|
<Flex direction="row" alignItems="center" justify="flex-end" height={35}>
|
178
|
<Text>My comments</Text>
|
179
|
<Switch isChecked={myCommentsCheck} onValueChange={() => { if (!notesLoading) setMyCommentsCheck(!myCommentsCheck) }} size="md" />
|
180
|
</Flex>
|
181
|
<Flex direction="row" alignItems="center" justify="flex-end" height={35}>
|
182
|
<Text>General comments</Text>
|
183
|
<Switch isChecked={generalCommentsCheck} onValueChange={() => { if (!notesLoading) setGeneralCommentsCheck(!generalCommentsCheck) }} size="md" />
|
184
|
</Flex>
|
185
|
</VStack>
|
186
|
</HStack>
|
187
|
{
|
188
|
notesLoading ? (
|
189
|
<LoadingBox text="Loading notes" />
|
190
|
)
|
191
|
:
|
192
|
(<NotesListView height={Dimensions.get('window').height - 70 - 160} notes={notes} navigation={navigation} handleCreateComment={handleCreateComment} requestPending={requestPending} />)
|
193
|
}
|
194
|
|
195
|
|
196
|
</VStack>
|
197
|
</Box>
|
198
|
);
|
199
|
}
|
200
|
|
201
|
export default NotesViewPage;
|