-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseLoader.js
156 lines (142 loc) · 3.73 KB
/
useLoader.js
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
import axios from "axios"
import { useEffect, useState } from "react"
import { applyMiddleware, createStore } from "redux"
import logger from "redux-logger"
import thunk from "redux-thunk"
// Action types ---------------------------------------------------------------
const FETCH_DATA_REQUEST = "FETCH_DATA_REQUEST"
const FETCH_DATA_SUCCESS = "FETCH_DATA_SUCCESS"
const FETCH_DATA_FAILURE = "FETCH_DATA_FAILURE"
// ----------------------------------------------------------------------------
// Action Creators ------------------------------------------------------------
const fetchDataRequest = () => {
return {
type: FETCH_DATA_REQUEST
}
}
const fetchDataSuccess = (response) => {
return {
type: FETCH_DATA_SUCCESS,
payload: response
}
}
const fetchDataFailure = (error) => {
return {
type: FETCH_DATA_FAILURE,
payload: error
}
}
const fetchData = (
requestURL = "",
requestMethod = "GET",
requestPayload = null,
onSuccess,
onFailure
) => (dispatch) => {
dispatch(fetchDataRequest())
switch (requestMethod) {
case "GET":
axios
.get(requestURL)
.then((response) => {
let fetchSuccessAction = fetchDataSuccess(response)
onSuccess && onSuccess(response)
dispatch(fetchSuccessAction)
})
.catch((error) => {
let fetchFailureAction = fetchDataFailure(error)
onFailure && onFailure(error)
dispatch(fetchFailureAction)
})
break
case "POST":
axios
.post(requestURL, requestPayload)
.then((response) => {
let fetchSuccessAction = fetchDataSuccess(response)
onSuccess && onSuccess(response)
dispatch(fetchSuccessAction)
})
.catch((error) => {
let fetchFailureAction = fetchDataFailure(error)
onFailure && onFailure(error)
dispatch(fetchFailureAction)
})
break
default:
throw new Error("Invalid Request Method!!")
}
}
// ----------------------------------------------------------------------------
// Intial State ---------------------------------------------------------------
const intialState = {
loading: false,
response: null,
error: null
}
// ----------------------------------------------------------------------------
// Reducer --------------------------------------------------------------------
const LoaderReducer = (state = intialState, action) => {
switch (action.type) {
case FETCH_DATA_REQUEST:
return {
...state,
loading: true,
response: null,
error: null
}
case FETCH_DATA_SUCCESS:
return {
...state,
loading: false,
response: action.payload,
error: null
}
case FETCH_DATA_FAILURE:
return {
...state,
loading: false,
response: null,
error: action.payload
}
default:
return state
}
}
// ----------------------------------------------------------------------------
// Store ----------------------------------------------------------------------
const localStore = createStore(LoaderReducer, applyMiddleware(logger, thunk))
// ----------------------------------------------------------------------------
// useLoader Hook------------------------------------------------------
const useLoader = (args) => {
const dispatch = localStore.dispatch
const [loader, setLoader] = useState(localStore.getState())
const unsubscribe = localStore.subscribe(() =>
setLoader(localStore.getState())
)
useEffect(() => {
return () => {
unsubscribe()
}
}, [])
const {
requestURL,
requestMethod,
requestPayload,
onSuccess,
onFailure
} = args
const getData = () =>
dispatch(
fetchData(
requestURL,
requestMethod,
requestPayload,
onSuccess,
onFailure
)
)
return [loader.loading, loader.response, loader.error, getData]
}
// ----------------------------------------------------------------------------
export default useLoader