@feardread/feature-factory 1.1.3 → 1.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bundle.min.js +1 -1
- package/dist/index.esm.js +1 -1
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/factory/factory.js +42 -7
package/package.json
CHANGED
package/src/factory/factory.js
CHANGED
|
@@ -7,8 +7,9 @@ const StateFactory = (entity, data = null) => ({
|
|
|
7
7
|
success: false,
|
|
8
8
|
error: null
|
|
9
9
|
});
|
|
10
|
+
|
|
10
11
|
const FeatureFactory = (name, endpoint, options = {}) => {
|
|
11
|
-
const { initialData,
|
|
12
|
+
const { initialData, service } = options;
|
|
12
13
|
const apiurl = `${name}/${endpoint}`;
|
|
13
14
|
|
|
14
15
|
const fetch = createAsyncThunk(
|
|
@@ -16,7 +17,6 @@ const FeatureFactory = (name, endpoint, options = {}) => {
|
|
|
16
17
|
async (_, { rejectWithValue }) => {
|
|
17
18
|
return await API.get(apiurl)
|
|
18
19
|
.then((response) => {
|
|
19
|
-
console.log('factory resp = ', response);
|
|
20
20
|
if (response.data && response.data.success) {
|
|
21
21
|
return response.data.result;
|
|
22
22
|
}
|
|
@@ -27,13 +27,29 @@ const FeatureFactory = (name, endpoint, options = {}) => {
|
|
|
27
27
|
}
|
|
28
28
|
)
|
|
29
29
|
|
|
30
|
-
const fetchOne = (id) => {
|
|
30
|
+
const fetchOne = async (id) => {
|
|
31
31
|
return createAsyncThunk(
|
|
32
32
|
`${name}/${id}`,
|
|
33
33
|
async (_, { rejectWithValue }) => {
|
|
34
34
|
return await API.get(`${name}/${id}`)
|
|
35
35
|
.then((response) => {
|
|
36
|
-
|
|
36
|
+
if (response.data && response.data.success) {
|
|
37
|
+
return response.data.result;
|
|
38
|
+
}
|
|
39
|
+
})
|
|
40
|
+
.catch((error) => {
|
|
41
|
+
return rejectWithValue('Error :: ', error);
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const search = async (query) => {
|
|
48
|
+
return createAsyncThunk(
|
|
49
|
+
`${name}/search?${query}`,
|
|
50
|
+
async (_, { rejectWithValue }) => {
|
|
51
|
+
return await API.get(`${name}/search?${query}`)
|
|
52
|
+
.then((response) => {
|
|
37
53
|
if (response.data && response.data.success) {
|
|
38
54
|
return response.data.result;
|
|
39
55
|
}
|
|
@@ -48,7 +64,11 @@ const FeatureFactory = (name, endpoint, options = {}) => {
|
|
|
48
64
|
const slice = createSlice({
|
|
49
65
|
name,
|
|
50
66
|
initialState: StateFactory(name, initialData),
|
|
51
|
-
reducers: {
|
|
67
|
+
reducers: {
|
|
68
|
+
clearData: (state) => {
|
|
69
|
+
state[name] = null;
|
|
70
|
+
}
|
|
71
|
+
},
|
|
52
72
|
extraReducers: (builder) => {
|
|
53
73
|
builder
|
|
54
74
|
.addCase(fetch.pending, (state) => {
|
|
@@ -77,18 +97,33 @@ const FeatureFactory = (name, endpoint, options = {}) => {
|
|
|
77
97
|
state.loading = false;
|
|
78
98
|
state.success = false;
|
|
79
99
|
})
|
|
100
|
+
.addCase(search.pending, (state) => {
|
|
101
|
+
state.loading = true;
|
|
102
|
+
})
|
|
103
|
+
.addCase(search.fulfilled, (state, action) => {
|
|
104
|
+
state.loading = false;
|
|
105
|
+
state.success = true;
|
|
106
|
+
state[name] = action.payload;
|
|
107
|
+
})
|
|
108
|
+
.addCase(search.rejected, (state, action) => {
|
|
109
|
+
state.error = action.error;
|
|
110
|
+
state.loading = false;
|
|
111
|
+
state.success = false;
|
|
112
|
+
})
|
|
80
113
|
}
|
|
81
114
|
})
|
|
82
115
|
|
|
83
116
|
return {
|
|
117
|
+
API,
|
|
84
118
|
reducer: slice.reducer,
|
|
85
119
|
actions: slice.actions,
|
|
86
120
|
asyncActions: {
|
|
87
121
|
fetch,
|
|
88
122
|
fetchOne,
|
|
89
|
-
search
|
|
123
|
+
search,
|
|
124
|
+
services: (service) ? service : {}
|
|
90
125
|
},
|
|
91
|
-
|
|
126
|
+
|
|
92
127
|
}
|
|
93
128
|
|
|
94
129
|
}
|