Projekt

Obecné

Profil

Stáhnout (2.93 KB) Statistiky
| Větev: | Tag: | Revize:
1 3a226033 Fantič
import { AsyncThunkAction, PayloadAction, createSlice } from "@reduxjs/toolkit"
2
import { deleteNote, getAllNotes, createNote, updateNote } from "../actions/notesThunks"
3 bb690a9a Fantič
import { Certainty, ItemViewState, Item } from "../../types/item";
4
import { Note, NotesViewState } from "../../types/note";
5
6
7
8
const initialState: NotesViewState = {
9
    notes: [],
10
    notesLoading: true, // for reading notes
11
    requestPending: false, // for updating or deleting note
12 3a226033 Fantič
    triggerRefresh: 0,
13 bb690a9a Fantič
    lastError: ""
14
}
15
16
export const noteSlice = createSlice({
17
    name: "note",
18
    initialState: initialState,
19
    reducers: {
20
    },
21
    extraReducers: (builder) => {
22 3a226033 Fantič
        // getAllNotes
23
        builder.addCase(getAllNotes.fulfilled, (state, action) => {
24
            state.notesLoading = false;
25
            state.lastError = "";
26
            state.notes = action.payload.notes;
27
        })
28
        builder.addCase(getAllNotes.pending, (state, action) => {
29
            state.notesLoading = true;
30
        })
31
        builder.addCase(getAllNotes.rejected, (state, action) => {
32
            state.notesLoading = false;
33
            state.lastError = action.error.message
34
        })
35
36
        // deleteNote
37
        builder.addCase(deleteNote.pending, (state, action) => {
38
            state.requestPending = true;
39
        })
40
        builder.addCase(deleteNote.fulfilled, (state, action) => {
41
            state.lastError = "";
42
            state.triggerRefresh++;
43
            state.requestPending = false;
44
        })
45
        builder.addCase(deleteNote.rejected, (state, action) => {
46
            state.requestPending = false;
47
            state.lastError = action.error.message
48
        })
49
50
        // createNote
51
        builder.addCase(createNote.pending, (state, action) => {
52
            state.requestPending = true;
53
        })
54
        builder.addCase(createNote.fulfilled, (state, action) => {
55
            state.notes = [action.payload.createdNote ,...state.notes]
56
            state.lastError = "";
57
            state.triggerRefresh++;
58
            state.requestPending = false;
59
        })
60
        builder.addCase(createNote.rejected, (state, action) => {
61
            state.requestPending = false;
62
            state.lastError = action.error.message
63
        })
64
65
        // createNote
66
        builder.addCase(updateNote.pending, (state, action) => {
67
            state.requestPending = true;
68
        })
69
        builder.addCase(updateNote.fulfilled, (state, action) => {
70
            state.requestPending = false;
71
            state.notes = state.notes.map((note) => {
72
                if (note.uuid === action.payload.updateNote.uuid) {
73
                    return action.payload.updateNote;
74
                } else {
75
                    return note;
76
                }
77
            });
78
            state.lastError = "";
79
        });
80
        builder.addCase(updateNote.rejected, (state, action) => {
81
            state.requestPending = false;
82
            state.lastError = action.error.message
83
        })
84 bb690a9a Fantič
    }
85
})
86
87 3a226033 Fantič
export default noteSlice.reducer