Projekt

Obecné

Profil

Stáhnout (3.97 KB) Statistiky
| Větev: | Tag: | Revize:
1 bb690a9a Fantič
import { createAsyncThunk } from "@reduxjs/toolkit"
2 3a226033 Fantič
import { createNoteRequest, deleteNoteRequest, getAllNotesRequest, updateNoteRequest } from "../../api/notesservice"
3 bb690a9a Fantič
import { SortOptions } from "../../types/general";
4 3a226033 Fantič
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
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 bb690a9a Fantič
72
73
export const getAllNotes = createAsyncThunk(
74
    "notes/getAllNotes",
75 3a226033 Fantič
    async ({ myComments, generalComments, sortOptions }: { myComments: boolean, generalComments: boolean, sortOptions: { items: SortOptions, date: SortOptions } }) => {
76 bb690a9a Fantič
        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 3a226033 Fantič
                if (response.data.length > 0) {
85 bb690a9a Fantič
86
                    let notes = [];
87 3a226033 Fantič
                    for (let i = 0; i < response.data.length; i++) {
88 bb690a9a Fantič
                        let note = response.data[i];
89
                        notes.push({
90
                            uuid: (note as any).uuid,
91
                            username: (note as any).created_by,
92
                            userId: (note as any).created_by_id,
93
                            note: (note as any).note,
94
                            avatarUrl: (note as any).avatar,
95
                            items: (note as any).items,
96 3a226033 Fantič
                            createdTime: (note as any).created,
97 bb690a9a Fantič
                            updatedTime: (note as any).updated,
98
                            noteColor: (note as any).note_color,
99 3a226033 Fantič
                            reply_to: (note as any).reply_to
100 bb690a9a Fantič
                        })
101
                    }
102
103
                    return {
104
                        notes,
105
                    }
106
                }
107 3a226033 Fantič
                else {
108 bb690a9a Fantič
                    // no notes
109
                    return {
110
                        notes: [],
111
                    }
112
                }
113
            }
114
            else {
115
                console.log("Error");
116
                return Promise.reject(response.data ? response.data : "Error")
117
            }
118
        } catch (err: any) {
119
            console.log(err);
120
            return Promise.reject(err.response.data)
121
        }
122
    }
123
)