Projekt

Obecné

Profil

Stáhnout (7.69 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 760dd761 Fantič
  const handleSortChanged = useCallback((key: "items" | "date", sortSettings: any) => {
52 917be067 Fantič
    let currentState = sortSettings[key];
53 9814562f Fantič
    let otherKey = "items"
54
55
    if (key == "items") {
56
      otherKey = "date"
57
    }
58
59 917be067 Fantič
    if (currentState == SortOptions.None) {
60
      setSortSettings({
61
        ...sortSettings,
62 9814562f Fantič
        [key]: SortOptions.Asc,
63
        [otherKey]: SortOptions.None
64 917be067 Fantič
      })
65
    }
66
    else if (currentState == SortOptions.Asc) {
67
      setSortSettings({
68
        ...sortSettings,
69 9814562f Fantič
        [key]: SortOptions.Desc,
70
        [otherKey]: SortOptions.None
71 917be067 Fantič
      })
72
    }
73
    else {
74
      setSortSettings({
75
        ...sortSettings,
76 9814562f Fantič
        [key]: SortOptions.None,
77
        [otherKey]: SortOptions.None
78 917be067 Fantič
      })
79
    }
80 b5ac7218 Fantič
  }, [setSortSettings]);
81 917be067 Fantič
82 9814562f Fantič
  const getSortIcon = (sortOption: SortOptions, size: string, color?: string) => {
83 917be067 Fantič
    switch (sortOption) {
84
      case SortOptions.Asc:
85 9814562f Fantič
        return <ChevronUpIcon size={size} color={color} />;
86 917be067 Fantič
      case SortOptions.Desc:
87 9814562f Fantič
        return <ChevronDownIcon size={size} color={color} />;
88 917be067 Fantič
      default:
89 9814562f Fantič
        return <MinusIcon size={size} color={color} />;
90 917be067 Fantič
    }
91
  };
92
93 3a226033 Fantič
  // trigger refresh on triggerRefresh increment
94 bb690a9a Fantič
  useEffect(() => {
95
    log.debug("NotesViewPage", "useEffect", "getNotes");
96
    getNotes();
97 3a226033 Fantič
  }, [triggerRefresh])
98 bb690a9a Fantič
99
  // general comments check changed
100
  useEffect(() => {
101 2135f53d Fantič
    if (!notesLoading) {
102 bb690a9a Fantič
      log.debug("NotesViewPage", "useEffect", "getNotes");
103
      getNotes();
104
    }
105
  }, [generalCommentsCheck, myCommentsCheck])
106
107
108 bc25708c Fantič
  const getNotes = useCallback(() => {
109 bb690a9a Fantič
    dispatch(getAllNotes({
110
      myComments: myCommentsCheck,
111
      generalComments: generalCommentsCheck,
112
      sortOptions: sortSettings
113
    }));
114 bc25708c Fantič
  }, [myCommentsCheck, generalCommentsCheck, sortSettings]);
115 917be067 Fantič
116
117 bc25708c Fantič
  const handleCreateComment = useCallback((newComment: string, replyingTo: { messageId: string; userName: string } | null) => {
118 2135f53d Fantič
    console.log(newComment, replyingTo)
119 917be067 Fantič
120 2135f53d Fantič
    if (!requestPending) {
121 3a226033 Fantič
      // creating new comment
122 293f99f8 Fantič
      toast.show({
123
        render: ({
124 9814562f Fantič
          id
125 293f99f8 Fantič
        }) => {
126 9814562f Fantič
          return <InfoToast text={"Adding note"} onClose={() => toast.close(id)} />;
127 293f99f8 Fantič
        },
128
        duration: 2000
129 9814562f Fantič
      });
130 2135f53d Fantič
      if (replyingTo != null) {
131
        dispatch(createNote({
132
          newNote: { note: newComment, reply_to: replyingTo.messageId } as Note
133 3a226033 Fantič
        }))
134
      }
135 2135f53d Fantič
      else {
136 3a226033 Fantič
        dispatch(createNote({
137 2135f53d Fantič
          newNote: { note: newComment } as Note
138 3a226033 Fantič
        }))
139
      }
140 2135f53d Fantič
    }
141 bc25708c Fantič
  }, [dispatch, createNote]);
142
143 bb690a9a Fantič
144 2135f53d Fantič
145 b225ffee Fantič
  return (
146 9814562f Fantič
    <Box>
147 b225ffee Fantič
      <VStack>
148 b56c1e7d Fantič
        <Flex direction="row" align="flex-start" justifyContent="space-between" marginTop={1} marginLeft={2.5} marginRight={2.5}>
149
          <Text fontWeight="semibold" mt={3} ml={2} fontSize="md" color="#4D4D4D">General comments</Text>
150
          <Switch isChecked={generalCommentsCheck} onValueChange={() => { if (!notesLoading) setGeneralCommentsCheck(!generalCommentsCheck) }} size="lg" marginRight={2} />
151
        </Flex>
152
        <Flex direction="row" align="flex-start" justifyContent="space-between" marginTop={-1} marginLeft={2.5} marginRight={2.5}>
153
          <Text fontWeight="semibold" mt={3} ml={2} fontSize="md" color="#4D4D4D">My comments</Text>
154
          <Switch padding={0} isChecked={myCommentsCheck} onValueChange={() => { if (!notesLoading) setMyCommentsCheck(!myCommentsCheck) }} size="lg" marginRight={2} />
155
        </Flex>
156
        <Popover // @ts-ignore
157
          trigger={triggerProps => {
158
            return <Button marginLeft={4} marginBottom={2} width={150} backgroundColor="#F4DFAB" {...triggerProps} onPress={() => setIsSortOpen(true)}
159
              borderRadius={7}
160
              rightIcon={(sortSettings.date != SortOptions.None || sortSettings.items != SortOptions.None) ? (getSortIcon(sortSettings.date != SortOptions.None ? sortSettings.date : sortSettings.items, "xs", "#654B07")) : undefined}
161
            >
162
              <Text fontSize={14} color="#654B07">
163
                Sort {(sortSettings.date != SortOptions.None || sortSettings.items != SortOptions.None) ? (sortSettings.date != SortOptions.None ? "by date" : "by items") : ""}
164
              </Text>
165
            </Button>;
166
          }} isOpen={isSortOpen} onClose={() => setIsSortOpen(!isSortOpen)}>
167
          <Popover.Content w="48">
168
            <Popover.Arrow />
169
            <Popover.CloseButton onPress={() => setIsSortOpen(false)} />
170
            <Popover.Header><Text fontSize={"md"} color="#654B07">Sort Options</Text></Popover.Header>
171
            <Popover.Body>
172
              <VStack>
173
                <Button size="md" variant="outline" onPress={() => handleSortChanged("items", sortSettings)}
174
                  rightIcon={getSortIcon(sortSettings.items, "sm", "#654B07")}
175 9814562f Fantič
                >
176 b56c1e7d Fantič
                  Items</Button>
177
                <Button size="md" variant="outline" marginTop={"5%"} onPress={() => handleSortChanged("date", sortSettings)}
178
                  rightIcon={getSortIcon(sortSettings.date, "sm", "#654B07")}>
179
                  Date</Button>
180
              </VStack>
181
            </Popover.Body>
182
            <Popover.Footer justifyContent="flex-end">
183
              <Button.Group space={2}>
184
                <Button colorScheme="coolGray" variant="ghost" onPress={() => setIsSortOpen(false)}>
185
                  Cancel
186
                </Button>
187
                <Button backgroundColor={"#F4DFAB"} onPress={() => { setIsSortOpen(false); getNotes() }}>
188
                  <Text color="#654B07">
189
                    Save
190 9814562f Fantič
                  </Text>
191 b56c1e7d Fantič
                </Button>
192
              </Button.Group>
193
            </Popover.Footer>
194
          </Popover.Content>
195
        </Popover>
196 b5ac7218 Fantič
        {
197
          notesLoading ? (
198
            <LoadingBox text="Loading notes" />
199
          )
200
            :
201 b56c1e7d Fantič
            (<NotesListView height={Dimensions.get('window').height - 307} notes={notes} navigation={navigation} handleCreateComment={handleCreateComment} requestPending={requestPending} />)
202 b5ac7218 Fantič
        }
203 67b916af Fantič
204 2135f53d Fantič
205 b225ffee Fantič
      </VStack>
206
    </Box>
207 917be067 Fantič
  );
208
}
209
210
export default NotesViewPage;