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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
import api from '../utils/Api'
const initialState = {
isLoading: false,
isSimulate: false,
libraries: [],
collapse: {},
components: {},
component_images: []
}
// Api call for fetching component library list
export const fetchLibraries = createAsyncThunk(
'schematicEditor/fetchLibraries',
async (_, { rejectWithValue }) => {
// SAMPLE Response from API
// [
// {
// "id": 1
// "name": "Commonly Used Blocks",
// "sort_order": 1
// },
// ] -- Multiple dicts in array
try {
const res = await api.get('categories/')
if (res.status === 200) {
return res.data
}
return rejectWithValue(res.data || 'Failed to load libraries')
} catch (err) {
console.error(err)
const res = err.response
return rejectWithValue(res.data || 'Failed to load libraries')
}
})
// Api call for fetching components under specified library id
export const fetchComponents = createAsyncThunk(
'schematicEditor/fetchComponents',
async (libraryId, { rejectWithValue }) => {
// SAMPLE Response from API
// [
// {
// "id": 1,
// "blocktype": 1,
// "name": "LOGICAL_OP",
// "categories": [ 1 ],
// "initial_explicit_input_ports": 2,
// "initial_implicit_input_ports": 0,
// "initial_explicit_output_ports": 1,
// "initial_implicit_output_ports": 0,
// "initial_display_parameter": "AND",
// "p000_value_initial": "AND",
// "p001_value_initial": "AND",
// "p002_value_initial": "AND",
// "p003_value_initial": "AND",
// "p004_value_initial": null,
// ...
// "p039_value_initial": null,
// },
// ] -- Multiple dicts in array
const url = 'newblocks/?categories=' + parseInt(libraryId)
try {
const res = await api.get(url)
if (res.status === 200) {
return { components: res.data, id: libraryId }
}
return rejectWithValue(res.data || 'Failed to load blocks')
} catch (err) {
console.error(err)
const res = err.response
return rejectWithValue(res.data || 'Failed to load blocks')
}
})
// Api call for fetching component image names
export const fetchComponentImages = createAsyncThunk(
'schematicEditor/fetchComponentImages',
async (_, { rejectWithValue }) => {
// SAMPLE Response from API
// [
// "palettes/LOGICAL_OP.png",
// ...
// "palettes/SUPER_f.png"
// ] -- Multiple strings in array
const url = 'block_images'
try {
const res = await api.get(url)
if (res.status === 200) {
return res.data
}
return rejectWithValue(res.data || 'Failed to load block images')
} catch (err) {
console.error(err)
const res = err.response
return rejectWithValue(res.data || 'Failed to load block images')
}
})
const schematicEditorSlice = createSlice({
name: 'schematicEditor',
initialState,
reducers: {
toggleCollapse: (state, action) => {
const existingState = state.collapse[action.payload]
Object.keys(state.collapse).forEach((key) => {
state.collapse[key] = false
})
state.collapse[action.payload] = !existingState
},
toggleSimulate: (state) => {
state.isSimulate = !state.isSimulate
}
},
extraReducers: (builder) => {
builder
.addCase(fetchLibraries.pending, (state) => {
state.isLoading = true
})
.addCase(fetchLibraries.fulfilled, (state, action) => {
// Add 'open' parameter to track open/close state of collapse
state.isLoading = false
const collapse = {}
const components = {}
action.payload.forEach(element => {
collapse[element.id] = false
components[element.id] = []
})
state.libraries = action.payload
state.collapse = collapse
state.components = components
})
.addCase(fetchLibraries.rejected, (state) => {
state.isLoading = false
state.libraries = {}
state.collapse = {}
state.components = {}
})
.addCase(fetchComponents.pending, (state) => {
state.isLoading = true
})
.addCase(fetchComponents.fulfilled, (state, action) => {
state.isLoading = false
state.components[action.payload.id] = action.payload.components
})
.addCase(fetchComponents.rejected, (state) => {
state.isLoading = false
})
.addCase(fetchComponentImages.pending, (state) => {
state.isLoading = true
})
.addCase(fetchComponentImages.fulfilled, (state, action) => {
state.isLoading = false
state.component_images = action.payload
})
.addCase(fetchComponentImages.rejected, (state) => {
state.isLoading = false
})
}
})
export const {
toggleCollapse,
toggleSimulate
} = schematicEditorSlice.actions
export default schematicEditorSlice.reducer
|