1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
import api from '../utils/Api'
const initialState = {
isLoading: false,
schematics: [],
gallery: []
}
// Api call for listing saved schematic to display on dashboard
export const fetchSchematics = createAsyncThunk(
'dashboard/fetchSchematics',
async (_, { getState, rejectWithValue }) => {
const token = getState().auth.token
if (!token) return rejectWithValue('No token found')
const config = {
headers: {
'Content-Type': 'application/json',
Authorization: `Token ${token}`
}
}
try {
const res = await api.get('save/list', config)
if (res.status === 200) {
return res.data
}
return rejectWithValue('No data found')
} catch (err) {
console.error(err)
return rejectWithValue('No data found')
}
})
export const fetchGallery = createAsyncThunk(
'dashboard/fetchGallery',
async (_, { rejectWithValue }) => {
try {
const res = await api.get('save/gallery')
if (res.status === 200) {
return res.data
}
return rejectWithValue('No data found')
} catch (err) {
console.error(err)
return rejectWithValue('No data found')
}
})
// Api call for deleting saved schematic
export const deleteSchematic = createAsyncThunk(
'dashboard/deleteSchematic',
async (saveId, { getState, rejectWithValue, dispatch }) => {
const token = getState().auth.token
if (!token) return rejectWithValue('No token found')
const config = {
headers: {
'Content-Type': 'application/json',
Authorization: `Token ${token}`
}
}
try {
const res = await api.delete(`save/diagram/${saveId}`, config)
if (res.status === 200) {
await dispatch(fetchSchematics())
return saveId
}
return rejectWithValue('No data found')
} catch (err) {
console.error(err)
return rejectWithValue('No data found')
}
})
const dashboardSlice = createSlice({
name: 'dashboard',
initialState,
extraReducers: (builder) => {
builder
.addCase(fetchSchematics.pending, (state) => {
state.isLoading = true
})
.addCase(fetchSchematics.fulfilled, (state, action) => {
state.isLoading = false
state.schematics = action.payload
})
.addCase(fetchSchematics.rejected, (state) => {
state.isLoading = false
state.schematics = []
})
.addCase(fetchGallery.pending, (state) => {
state.isLoading = true
})
.addCase(fetchGallery.fulfilled, (state, action) => {
state.isLoading = false
state.gallery = action.payload
})
.addCase(fetchGallery.rejected, (state) => {
state.isLoading = false
state.gallery = []
})
}
})
export default dashboardSlice.reducer
|