Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 56626a63

Přidáno uživatelem Fantič před více než 1 rok

#re 10797 NotesListView set replying optimize

Zobrazit rozdíly:

src/components/item/ItemView.tsx
1 1
import { Center, Box, VStack, Button, HStack, Text, Image, ScrollView, View, Spacer, Card, Heading, Switch, Flex } from "native-base"
2
import React, { useEffect, useState } from "react"
2
import React, { useCallback, useEffect, useState } from "react"
3 3
import { Item } from "../../types/item"
4 4
import { Note } from "../../types/note"
5 5
import LoadingBox from "../loading/LoadingBox"
......
145 145
        }
146 146
    }
147 147

  
148
    const handleCreateItemComment = (newComment: string, replyingTo: { messageId: string; userName: string } | null) => {
149
        if (!requestPending) {
150
            // creating new comment
151
            if (replyingTo != null) {
152
                dispatch(createNote({
153
                    newNote: { note: newComment, reply_to: replyingTo.messageId } as Note
154
                }))
148
    const handleCreateItemComment = useCallback(
149
        (newComment: string, replyingTo: { messageId: string; userName: string } | null) => {
150
            if (!requestPending) {
151
                // creating new comment
152
                if (replyingTo != null) {
153
                    dispatch(createNote({ newNote: { note: newComment, reply_to: replyingTo.messageId } as Note }));
154
                } else {
155
                    dispatch(createNote({ newNote: { note: newComment, items: props.item.id } as any }));
156
                }
155 157
            }
156
            else {
157
                dispatch(createNote({
158
                    newNote: { note: newComment, items: props.item.id } as any
159
                }))
160
            }
161
        }
162
    }
158
        },
159
        [props.item.id]
160
    );
163 161

  
164 162
    return (
165 163

  
src/components/notes/NoteView.tsx
4 4
import { AVATAR_URL } from "../../api/constants";
5 5
import { useState } from "react";
6 6
import React from "react";
7
import { ConfirmDialog } from "../general/Dialogs";
8 7

  
9
const NoteView = (props: { note: Note, handleReply: any, handleDelete: any, handleEdit: any, setConfirmDialog: any, navigation: any }) => {
8
const NoteView =  React.memo((props: { note: Note, handleReply: any, handleDelete: any, handleEdit: any, setConfirmDialog: any, navigation: any }) => {
10 9
    const note = props.note;
11 10

  
12 11
    const [isEdited, setIsEdited] = useState(false);
13 12

  
14 13
    const [text, setText] = useState(note.note);
15 14

  
15
    console.log("rerender");
16

  
16 17
    const getDateString = (timestamp: Date): string => {
17 18
        return new Date(timestamp).toLocaleString("en-US",
18 19
            { year: "numeric", month: "short", day: "2-digit", hour: "2-digit", minute: "2-digit", hour12: false }
......
142 143
            </VStack>
143 144
        </HStack>
144 145
    );
145
}
146
})
146 147

  
147 148
export default NoteView
148 149

  
src/components/notes/NotesListView.tsx
2 2
import { Note } from "../../types/note";
3 3
import NoteView from "./NoteView";
4 4
import { MessageIcon } from "../general/Icons";
5
import React, { useState } from "react";
5
import React, { useCallback, useState } from "react";
6 6
import { updateNote, deleteNote } from "../../stores/actions/notesThunks";
7 7
import { ConfirmDialog } from "../general/Dialogs";
8 8
import { AppDispatch } from "../../stores/store";
......
28 28
        onSubmit?: () => void;
29 29
    } | null>(null);
30 30

  
31
    const handleReply = (messageId: string, userName: string) => {
32
        setReplyingTo({ messageId, userName })
33
    }
34

  
35
    const handleEdit = (note: Note) => {
36
        if (!props.requestPending)
31
    const handleReply = useCallback((messageId: string, userName: string) => {
32
        setReplyingTo({ messageId, userName });
33
    }, [setReplyingTo]);
34
    
35
    const handleEdit = useCallback((note: Note) => {
36
        if (!props.requestPending) {
37 37
            dispatch(updateNote(note));
38
    }
39

  
40
    const handleDelete = (messageId: string) => {
41
        if (!props.requestPending)
42
            dispatch(deleteNote({ noteId: messageId }))
43
    }
38
        }
39
    }, [dispatch, props.requestPending]);
40
    
41
    const handleDelete = useCallback((messageId: string) => {
42
        if (!props.requestPending) {
43
            dispatch(deleteNote({ noteId: messageId }));
44
        }
45
    }, [dispatch, props.requestPending]);
44 46

  
45 47
    return (
46 48
        <VStack>
src/stores/actions/itemThunks.ts
123 123
                            note: (note as any).note,
124 124
                            avatarUrl: (note as any).avatar,
125 125
                            items: (note as any).items,
126
                            replies: (note as any).replies ?? undefined,
126 127
                            createdTime: (note as any).created, 
127 128
                            updatedTime: (note as any).updated,
128 129
                            noteColor: (note as any).note_color,
src/stores/actions/notesThunks.ts
93 93
                            note: (note as any).note,
94 94
                            avatarUrl: (note as any).avatar,
95 95
                            items: (note as any).items,
96
                            replies: (note as any).replies ?? undefined,
96 97
                            createdTime: (note as any).created,
97 98
                            updatedTime: (note as any).updated,
98 99
                            noteColor: (note as any).note_color,
src/types/note.ts
5 5
    note: string // text
6 6
    avatarUrl: string
7 7
    items: string[]
8
    replies?: Note[]
8 9
    createdTime: Date 
9 10
    updatedTime: Date
10 11
    noteColor: string
11
    reply_to: string // parent id
12
    reply_to?: string // parent id
12 13
}
13 14

  
14 15

  

Také k dispozici: Unified diff