Projekt

Obecné

Profil

Stáhnout (5.17 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { createAsyncThunk } from "@reduxjs/toolkit"
2
import { createNoteRequest, deleteNoteRequest, getAllNotesRequest, updateNoteRequest } from "../../api/notesservice"
3
import { SortOptions } from "../../types/general";
4
import { Note } from "../../types/note";
5
import { useDispatch } from "react-redux";
6
import { AppDispatch } from "../store";
7

    
8
export const deleteNote = createAsyncThunk(
9
    "notes/deleteNote",
10
    async ({ noteId }: { noteId: string }) => {
11
        try {
12

    
13
            const response = await deleteNoteRequest(noteId);
14

    
15
            console.log(response);
16

    
17
            if (response.status === 200) {
18
                return { deletedNoteId: noteId };
19
            }
20
            else {
21
                console.log("Error");
22
                return Promise.reject(response.data ? response.data : "Error")
23
            }
24
        } catch (err: any) {
25
            console.log(err);
26
            return Promise.reject(err.response.data)
27
        }
28
    }
29
)
30

    
31
export const createNote = createAsyncThunk(
32
    "notes/createNote",
33
    async ({ newNote }: { newNote: Note}) => {
34
        try {
35
            console.log(newNote.note);
36
            const response = await createNoteRequest(newNote);
37
            console.log(response);
38
            if (response.status === 201) {
39
                return { createdNote: newNote };
40
            }
41
            else {
42
                console.log("Error");
43
                return Promise.reject(response.data ? response.data : "Error")
44
            }
45
        } catch (err: any) {
46
            console.log(err);
47
            return Promise.reject(err.response.data)
48
        }
49
    }
50
)
51

    
52
export const updateNote = createAsyncThunk(
53
    "notes/updateNote",
54
    async (note: Note) => {
55
        try {
56
            const response = await updateNoteRequest(note);
57
            if (response.status === 201) {
58
                return { updateNote: note };
59
            }
60
            else {
61
                console.log("Error");
62
                return Promise.reject(response.data ? response.data : "Error")
63
            }
64
        } catch (err: any) {
65
            console.log(err);
66
            return Promise.reject(err.response.data)
67
        }
68
    }
69
)
70

    
71

    
72

    
73
export const getAllNotes = createAsyncThunk(
74
    "notes/getAllNotes",
75
    async ({ myComments, generalComments, sortOptions }: { myComments: boolean, generalComments: boolean, sortOptions: { items: SortOptions, date: SortOptions } }) => {
76
        try {
77
            console.log("GET notes/getNotes/");
78

    
79
            const response = await getAllNotesRequest(myComments, generalComments, sortOptions);
80

    
81
            console.log(response);
82

    
83
            if (response.status === 200) {
84
                if (response.data.length > 0) {
85

    
86
                    let notes = [];
87
                    for (let i = 0; i < response.data.length; i++) {
88
                        let note = response.data[i];
89
                        let replies : any = undefined
90

    
91
                        // convert replies
92
                        if(note.replies){
93
                            replies = [];
94
                            for(let i = 0; i < note.replies.length; i++){
95
                                const reply = note.replies[i];
96
                                replies.push({
97
                                    uuid: (reply as any).uuid,
98
                                    username: (reply as any).created_by,
99
                                    userId: (reply as any).created_by_id,
100
                                    note: (reply as any).note,
101
                                    avatarUrl: (reply as any).avatar,
102
                                    items: (reply as any).items,
103
                                    createdTime: (reply as any).created,
104
                                    updatedTime: (reply as any).updated,
105
                                    noteColor: (reply as any).note_color,
106
                                    reply_to: note.uuid
107
                                })
108
                            }
109
                        }
110

    
111
                        notes.push({
112
                            uuid: (note as any).uuid,
113
                            username: (note as any).created_by,
114
                            userId: (note as any).created_by_id,
115
                            note: (note as any).note,
116
                            avatarUrl: (note as any).avatar,
117
                            items: (note as any).items,
118
                            replies: replies,
119
                            createdTime: (note as any).created,
120
                            updatedTime: (note as any).updated,
121
                            noteColor: (note as any).note_color,
122
                            reply_to: (note as any).reply_to
123
                        })
124
                    }
125

    
126
                    return {
127
                        notes,
128
                    }
129
                }
130
                else {
131
                    // no notes
132
                    return {
133
                        notes: [],
134
                    }
135
                }
136
            }
137
            else {
138
                console.log("Error");
139
                return Promise.reject(response.data ? response.data : "Error")
140
            }
141
        } catch (err: any) {
142
            console.log(err);
143
            return Promise.reject(err.response.data)
144
        }
145
    }
146
)
(4-4/6)