Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 2135f53d

Přidáno uživatelem Fantič před téměř 2 roky(ů)

re #10678: ItemNotes refactor NoteListView

Zobrazit rozdíly:

src/pages/NotesViewPage.tsx
1 1
import React, { useEffect, useState } from "react"
2 2
import { log } from "../logging/logger"
3 3
import { DrawerScreenProps } from "@react-navigation/drawer"
4
import { Box, Button, ChevronDownIcon, ChevronUpIcon, CloseIcon, Flex, HStack, Icon, IconButton, MinusIcon, Popover, Pressable, ScrollView, Switch, Text, TextArea, VStack } from "native-base"
4
import { Box, Button, ChevronDownIcon, ChevronUpIcon, Flex, HStack, MinusIcon, Popover, Switch, Text, VStack } from "native-base"
5 5
import { SortOptions } from "../types/general"
6 6
import { Note } from "../types/note"
7 7
import NotesListView from "../components/notes/NotesListView"
8
import { G, Path } from "react-native-svg"
9
import { MessageIcon } from "../components/general/Icons"
10
import { ConfirmDialog } from "../components/general/Dialogs"
11 8
import { useDispatch, useSelector } from "react-redux"
12 9
import { AppDispatch, RootState } from "../stores/store"
13 10
import LoadingBox from "../components/loading/LoadingBox"
14
import { createNote, deleteNote, getAllNotes, updateNote } from "../stores/actions/notesThunks"
15
import { login } from "../stores/actions/userThunks"
11
import { createNote, getAllNotes } from "../stores/actions/notesThunks"
16 12
import { RootDrawerParamList } from "./Navigation"
17
const NotesViewPage = ({ route, navigation }: DrawerScreenProps<RootDrawerParamList, 'Notes'>) => {
13
const NotesViewPage = ({ navigation }: DrawerScreenProps<RootDrawerParamList, 'Notes'>) => {
18 14

  
19 15
  const { notes, notesLoading, requestPending, triggerRefresh } = useSelector((state: RootState) => state.noteViewState)
20
  
16

  
21 17
  const dispatch = useDispatch<AppDispatch>();
22 18

  
23 19
  const [isSortOpen, setIsSortOpen] = useState(false);
24
  
25
  const [newComment, setNewComment] = useState<string>("");
26 20

  
27 21
  const [myCommentsCheck, setMyCommentsCheck] = useState(false);
28 22
  const [generalCommentsCheck, setGeneralCommentsCheck] = useState(true);
29 23

  
30
  const [replyingTo, setReplyingTo] = useState<{ messageId: string; userName: string } | null>(null);
31

  
32
  const cancelRef = React.useRef(null);
33

  
34
  const [confirmDialog, setConfirmDialog] = React.useState<{
35
    headerText?: string;
36
    bodyText?: string;
37
    confirmText?: string;
38
    confirmColor?: string;
39
    cancelRef?: any;
40
    onClose?: () => void;
41
    onSubmit?: () => void;
42
  } | null>(null);
43

  
44 24
  const [sortSettings, setSortSettings] = useState({
45 25
    items: SortOptions.None,
46 26
    date: SortOptions.None
......
83 63
  useEffect(() => {
84 64
    log.debug("NotesViewPage", "useEffect", "getNotes");
85 65
    getNotes();
86
    
87
    setNewComment("");
88
    setReplyingTo(null);
89 66
  }, [triggerRefresh])
90 67

  
91 68
  // general comments check changed
92 69
  useEffect(() => {
93
    if(!notesLoading){
70
    if (!notesLoading) {
94 71
      log.debug("NotesViewPage", "useEffect", "getNotes");
95 72
      getNotes();
96 73
    }
......
105 82
    }));
106 83
  }
107 84

  
108
  const handleReply = (messageId: string, userName: string) => {
109
    setReplyingTo({ messageId, userName })
110
  }
111

  
112
  const handleEdit = (note : Note) => {
113
    if(!requestPending)
114
      dispatch(updateNote(note));
115
  }
85
  const handleCreateComment = (newComment : string, replyingTo : { messageId: string; userName: string } | null) => {
116 86

  
117
  const handleDelete = (messageId: string) => {
118
    if(!requestPending)
119
      dispatch(deleteNote({noteId: messageId}))
120
  }
87
    console.log(newComment, replyingTo)
121 88

  
122
  const handleCreateComment = () => {
123
    if(!requestPending){
89
    if (!requestPending) {
124 90
      // creating new comment
125
      if(replyingTo != null){
126
        dispatch(createNote( {
127
          newNote: {note: newComment, reply_to: replyingTo.messageId} as Note
91
      if (replyingTo != null) {
92
        dispatch(createNote({
93
          newNote: { note: newComment, reply_to: replyingTo.messageId } as Note
128 94
        }))
129 95
      }
130
      else{
96
      else {
131 97
        dispatch(createNote({
132
          newNote: {note: newComment} as Note
98
          newNote: { note: newComment } as Note
133 99
        }))
134 100
      }
135
    } 
101
    }
136 102
  }
137 103

  
104

  
138 105
  return (
139 106
    <Box width="92%" marginLeft="5%">
140 107
      <VStack>
......
177 144
          <VStack width="70%" space="0">
178 145
            <Flex direction="row" alignItems="center" justify="flex-end">
179 146
              <Text>My comments</Text>
180
              <Switch isChecked={myCommentsCheck} onValueChange={() => {if(!notesLoading) setMyCommentsCheck(!myCommentsCheck)}} size="md" />
147
              <Switch isChecked={myCommentsCheck} onValueChange={() => { if (!notesLoading) setMyCommentsCheck(!myCommentsCheck) }} size="md" />
181 148
            </Flex>
182 149
            <Flex direction="row" alignItems="center" justify="flex-end" marginTop="-7.5%">
183 150
              <Text>General comments</Text>
184
              <Switch isChecked={generalCommentsCheck} onValueChange={() => {if(!notesLoading) setGeneralCommentsCheck(!generalCommentsCheck)}} size="md" />
151
              <Switch isChecked={generalCommentsCheck} onValueChange={() => { if (!notesLoading) setGeneralCommentsCheck(!generalCommentsCheck) }} size="md" />
185 152
            </Flex>
186 153
          </VStack>
187 154
        </HStack>
188
        <Box h={replyingTo != null ? "62.5%" : "67.5%"}>
189
          {
190
            notesLoading ? (<LoadingBox text="Loading notes"/>)
191
              :
192
              (<NotesListView notes={notes} handleReply={handleReply} handleDelete={handleDelete} handleEdit={handleEdit} setConfirmDialog={setConfirmDialog} navigation={navigation} />)
193
          }
194
        </Box>
195
        {replyingTo != null &&
196
          <Flex direction="row" alignItems="center" justify="flex-end" h="5%" marginRight="10%">
197
            <Text>Replying to {replyingTo.userName}</Text>
198
            <IconButton onPress={() => setReplyingTo(null)} size="sm" icon={<CloseIcon />} marginLeft="-1%">
199
            </IconButton>
200
          </Flex>
155
        {
156
          notesLoading ? (<LoadingBox text="Loading notes" />)
157
            :
158
            (<NotesListView notes={notes} navigation={navigation} handleCreateComment={handleCreateComment} requestPending={requestPending} />)
201 159
        }
202
        <HStack h={replyingTo != null ? "20%" : "15%"} marginTop={replyingTo != null ? "0%" : "5%"}>
203
          <TextArea placeholder="Add comment" width="90%" fontSize={14} value={newComment} onChangeText={setNewComment} autoCompleteType={undefined} ></TextArea>
204
          <VStack>
205
            <Box h={replyingTo != null ? "30%" : "45%"}></Box>
206
            <Box marginLeft="2">
207
              <Button onPress={handleCreateComment} startIcon={<MessageIcon color="#FFF" /> }></Button>
208
            </Box>
209
          </VStack>
210
        </HStack>
160

  
211 161
      </VStack>
212
      <ConfirmDialog {...confirmDialog} isShown={confirmDialog != null} cancelRef={cancelRef} />
213 162
    </Box>
214 163
  );
215 164
}

Také k dispozici: Unified diff