Projekt

Obecné

Profil

Stáhnout (5.97 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 2135f53d Fantič
import { Box, Button, ChevronDownIcon, ChevronUpIcon, Flex, HStack, MinusIcon, Popover, Switch, Text, VStack } 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 d3c12593 Fantič
import { RootDrawerParamList } from "./Navigation"
13 b5ac7218 Fantič
import { Dimensions } from "react-native"
14 2135f53d Fantič
const NotesViewPage = ({ navigation }: DrawerScreenProps<RootDrawerParamList, 'Notes'>) => {
15 917be067 Fantič
16 3a226033 Fantič
  const { notes, notesLoading, requestPending, triggerRefresh } = useSelector((state: RootState) => state.noteViewState)
17 2135f53d Fantič
18 bb690a9a Fantič
  const dispatch = useDispatch<AppDispatch>();
19
20 917be067 Fantič
  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 b5ac7218 Fantič
  const handleSortChanged = useCallback((key: "items" | "date") => {
31 917be067 Fantič
    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 b5ac7218 Fantič
  }, [setSortSettings]);
51 917be067 Fantič
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 3a226033 Fantič
  // trigger refresh on triggerRefresh increment
64 bb690a9a Fantič
  useEffect(() => {
65
    log.debug("NotesViewPage", "useEffect", "getNotes");
66
    getNotes();
67 3a226033 Fantič
  }, [triggerRefresh])
68 bb690a9a Fantič
69
  // general comments check changed
70
  useEffect(() => {
71 2135f53d Fantič
    if (!notesLoading) {
72 bb690a9a Fantič
      log.debug("NotesViewPage", "useEffect", "getNotes");
73
      getNotes();
74
    }
75
  }, [generalCommentsCheck, myCommentsCheck])
76
77
78 917be067 Fantič
  const getNotes = () => {
79 bb690a9a Fantič
    dispatch(getAllNotes({
80
      myComments: myCommentsCheck,
81
      generalComments: generalCommentsCheck,
82
      sortOptions: sortSettings
83
    }));
84 917be067 Fantič
  }
85
86 67b916af Fantič
  const handleCreateComment = (newComment: string, replyingTo: { messageId: string; userName: string } | null) => {
87 917be067 Fantič
88 2135f53d Fantič
    console.log(newComment, replyingTo)
89 917be067 Fantič
90 2135f53d Fantič
    if (!requestPending) {
91 3a226033 Fantič
      // creating new comment
92 2135f53d Fantič
      if (replyingTo != null) {
93
        dispatch(createNote({
94
          newNote: { note: newComment, reply_to: replyingTo.messageId } as Note
95 3a226033 Fantič
        }))
96
      }
97 2135f53d Fantič
      else {
98 3a226033 Fantič
        dispatch(createNote({
99 2135f53d Fantič
          newNote: { note: newComment } as Note
100 3a226033 Fantič
        }))
101
      }
102 2135f53d Fantič
    }
103 3a226033 Fantič
  }
104 bb690a9a Fantič
105 2135f53d Fantič
106 b225ffee Fantič
  return (
107 b5ac7218 Fantič
    <Box marginLeft={2.5} marginRight={2.5} marginTop={2.5}>
108 b225ffee Fantič
      <VStack>
109 b5ac7218 Fantič
        <HStack height={75} justifyContent="space-between">
110
          <Box justifyContent={"center"}>
111 b225ffee Fantič
            <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 b5ac7218 Fantič
146
          <VStack space={0.5}>
147
            <Flex direction="row" alignItems="center" justify="flex-end" height={35}>
148 b225ffee Fantič
              <Text>My comments</Text>
149 2135f53d Fantič
              <Switch isChecked={myCommentsCheck} onValueChange={() => { if (!notesLoading) setMyCommentsCheck(!myCommentsCheck) }} size="md" />
150 b225ffee Fantič
            </Flex>
151 b5ac7218 Fantič
            <Flex direction="row" alignItems="center" justify="flex-end" height={35}>
152 b225ffee Fantič
              <Text>General comments</Text>
153 2135f53d Fantič
              <Switch isChecked={generalCommentsCheck} onValueChange={() => { if (!notesLoading) setGeneralCommentsCheck(!generalCommentsCheck) }} size="md" />
154 b225ffee Fantič
            </Flex>
155
          </VStack>
156
        </HStack>
157 b5ac7218 Fantič
        {
158
          notesLoading ? (
159
            <LoadingBox text="Loading notes" />
160
          )
161
            :
162
            (<NotesListView notes={notes} navigation={navigation} handleCreateComment={handleCreateComment} requestPending={requestPending} />)
163
        }
164 67b916af Fantič
165 2135f53d Fantič
166 b225ffee Fantič
      </VStack>
167
    </Box>
168 917be067 Fantič
  );
169
}
170
171
export default NotesViewPage;