Projekt

Obecné

Profil

« Předchozí | Další » 

Revize bc25708c

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

#re 10797 NoteListView lazy loading -> scroll load more notes

Zobrazit rozdíly:

src/components/notes/NotesListView.tsx
15 15
    const [newComment, setNewComment] = useState<string>("");
16 16
    const [replyingTo, setReplyingTo] = useState<{ messageId: string; userName: string } | null>(null);
17 17

  
18
    const [renderedNotes, setRenderedNotes] = useState(6);
19

  
18 20
    const cancelRef = React.useRef(null);
19 21

  
20 22
    const dispatch = useDispatch<AppDispatch>();
......
47 49
        }
48 50
    }, [dispatch, props.requestPending]);
49 51

  
52
    const handleScrollEnd = (event : any) => {
53
        const offsetY = event.nativeEvent.contentOffset.y;
54
        const contentHeight = event.nativeEvent.contentSize.height;
55
        const screenHeight = event.nativeEvent.layoutMeasurement.height;
56

  
57
        if (offsetY + screenHeight >= contentHeight - 20) {
58
            // Load more notes when the user is near the end of the list
59
            const nextBatchSize = 5; // Number of notes to render in the next batch
60

  
61
            if((renderedNotes + nextBatchSize) > notes.length && (renderedNotes + nextBatchSize) != notes.length){
62
                setRenderedNotes(notes.length);
63
                // TODO toast display info all rendered
64
            }
65
            else{
66
                setRenderedNotes(renderedNotes + nextBatchSize);
67
                // TODO toast display loading more notes
68
            }
69
        }
70
    };
71

  
72
    console.log(renderedNotes)
73
    console.log(notes.length)
74

  
50 75
    return (
51 76
        <VStack>
52 77
            <Box height={windowHeight - 70 - 160 - (replyingTo ? 40 : 0)} marginBottom={2}>
53 78
                {/* Notes */}
54
                <ScrollView contentContainerStyle={{ flexGrow: 1 }}>
79
                <ScrollView contentContainerStyle={{ flexGrow: 1 }} onScrollEndDrag={handleScrollEnd}>
55 80
                    {
56 81
                        notes && notes.length > 0 ? (
57 82
                            <VStack>
58
                                {notes.map((note, index) => {
59
                                    // if(index >= 5){
60
                                    //     return null;
61
                                    // }
62
                                    return (
83
                                {
84
                                    notes.slice(0, renderedNotes).map((note, index) => (
63 85
                                        <NoteView key={index} note={note} handleReply={handleReply} handleDelete={handleDelete} handleEdit={handleEdit} setConfirmDialog={setConfirmDialog} navigation={props.navigation} />
64
                                    )
65
                                })}
86
                                    ))
87
                                }
66 88
                            </VStack>
67 89
                        ) : (
68 90
                            <Text>There are no notes.</Text>
......
84 106
                            </IconButton>
85 107
                        </Flex>
86 108
                    }
87
                    
109

  
88 110

  
89 111
                    {/* Add comment */}
90 112
                    <HStack height={90} >
src/pages/NotesViewPage.tsx
75 75
  }, [generalCommentsCheck, myCommentsCheck])
76 76

  
77 77

  
78
  const getNotes = () => {
78
  const getNotes = useCallback(() => {
79 79
    dispatch(getAllNotes({
80 80
      myComments: myCommentsCheck,
81 81
      generalComments: generalCommentsCheck,
82 82
      sortOptions: sortSettings
83 83
    }));
84
  }
84
  }, [myCommentsCheck, generalCommentsCheck, sortSettings]);
85 85

  
86
  const handleCreateComment = (newComment: string, replyingTo: { messageId: string; userName: string } | null) => {
87 86

  
87
  const handleCreateComment = useCallback((newComment: string, replyingTo: { messageId: string; userName: string } | null) => {
88 88
    console.log(newComment, replyingTo)
89 89

  
90 90
    if (!requestPending) {
......
100 100
        }))
101 101
      }
102 102
    }
103
  }
103
  }, [dispatch, createNote]);
104

  
104 105

  
105 106

  
106 107
  return (
src/stores/actions/notesThunks.ts
2 2
import { createNoteRequest, deleteNoteRequest, getAllNotesRequest, updateNoteRequest } from "../../api/notesservice"
3 3
import { SortOptions } from "../../types/general";
4 4
import { Note } from "../../types/note";
5
import { useDispatch } from "react-redux";
6
import { AppDispatch } from "../store";
7 5

  
8 6
export const deleteNote = createAsyncThunk(
9 7
    "notes/deleteNote",

Také k dispozici: Unified diff