Projekt

Obecné

Profil

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