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