@feardread/feature-factory 1.1.4 → 1.1.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@feardread/feature-factory",
3
- "version": "1.1.4",
3
+ "version": "1.1.6",
4
4
  "main": "dist/index.js",
5
5
  "scripts": {
6
6
  "build": "rollup -c",
@@ -7,6 +7,7 @@ const StateFactory = (entity, data = null) => ({
7
7
  success: false,
8
8
  error: null
9
9
  });
10
+
10
11
  const FeatureFactory = (name, endpoint, options = {}) => {
11
12
  const { initialData, service } = options;
12
13
  const apiurl = `${name}/${endpoint}`;
@@ -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
- console.log('factory resp = ', response);
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) => {
@@ -64,23 +84,11 @@ const FeatureFactory = (name, endpoint, options = {}) => {
64
84
  state.loading = false;
65
85
  state.success = false;
66
86
  })
67
- .addCase(fetchOne.pending, (state) => {
68
- state.loading = true;
69
- })
70
- .addCase(fetchOne.fulfilled, (state, action) => {
71
- state.loading = false;
72
- state.success = true;
73
- state[name] = action.payload;
74
- })
75
- .addCase(fetchOne.rejected, (state, action) => {
76
- state.error = action.error;
77
- state.loading = false;
78
- state.success = false;
79
- })
80
87
  }
81
88
  })
82
89
 
83
90
  return {
91
+ API,
84
92
  reducer: slice.reducer,
85
93
  actions: slice.actions,
86
94
  asyncActions: {