Refactored uptimeMonitorsSlice axios requests
This commit is contained in:
1 parent
da50630672
commit
ceb8985afb
2 files changed
+64
-67
No files matched your search
@@ -13,32 +13,7 @@ export const createUptimeMonitor = createAsyncThunk(
|
||||
async (data, thunkApi) => {
|
||||
try {
|
||||
const { authToken, monitor } = data;
|
||||
|
||||
const res = await axiosInstance.post(`/monitors`, monitor, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${authToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
return res.data;
|
||||
} catch (error) {
|
||||
if (error.response && error.response.data) {
|
||||
return thunkApi.rejectWithValue(error.response.data);
|
||||
}
|
||||
const payload = {
|
||||
status: false,
|
||||
msg: error.message ? error.message : "Unknown error",
|
||||
};
|
||||
return thunkApi.rejectWithValue(payload);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
export const getUptimeMonitors = createAsyncThunk(
|
||||
"monitors/getMonitors",
|
||||
async (token, thunkApi) => {
|
||||
try {
|
||||
const res = await axiosInstance.get("/monitors");
|
||||
const res = await axiosInstance.createMonitor(authToken, monitor);
|
||||
return res.data;
|
||||
} catch (error) {
|
||||
if (error.response && error.response.data) {
|
||||
@@ -58,13 +33,13 @@ export const getUptimeMonitorsByUserId = createAsyncThunk(
|
||||
async (token, thunkApi) => {
|
||||
const user = jwtDecode(token);
|
||||
try {
|
||||
const res = await axiosInstance.get(
|
||||
`/monitors/user/${user._id}?limit=25&type=http&type=ping&sortOrder=desc&normalize=true`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
}
|
||||
const res = await axiosInstance.getMonitorsByUserId(
|
||||
token,
|
||||
user._id,
|
||||
25,
|
||||
["http", "ping"],
|
||||
"desc",
|
||||
true
|
||||
);
|
||||
return res.data;
|
||||
} catch (error) {
|
||||
@@ -91,15 +66,10 @@ export const updateUptimeMonitor = createAsyncThunk(
|
||||
interval: monitor.interval,
|
||||
notifications: monitor.notifications,
|
||||
};
|
||||
const res = await axiosInstance.put(
|
||||
`/monitors/${monitor._id}`,
|
||||
updatedFields,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${authToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
}
|
||||
const res = await axiosInstance.updateMonitor(
|
||||
authToken,
|
||||
monitor._id,
|
||||
updatedFields
|
||||
);
|
||||
return res.data;
|
||||
} catch (error) {
|
||||
@@ -120,12 +90,7 @@ export const deleteUptimeMonitor = createAsyncThunk(
|
||||
async (data, thunkApi) => {
|
||||
try {
|
||||
const { authToken, monitor } = data;
|
||||
const res = await axiosInstance.delete(`/monitors/${monitor._id}`, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${authToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
const res = await axiosInstance.deleteMonitorById(authToken, monitor._id);
|
||||
return res.data;
|
||||
} catch (error) {
|
||||
if (error.response && error.response.data) {
|
||||
@@ -153,25 +118,6 @@ const uptimeMonitorsSlice = createSlice({
|
||||
},
|
||||
extraReducers: (builder) => {
|
||||
builder
|
||||
// *****************************************************
|
||||
// All Monitors
|
||||
// *****************************************************
|
||||
.addCase(getUptimeMonitors.pending, (state) => {
|
||||
state.isLoading = true;
|
||||
})
|
||||
.addCase(getUptimeMonitors.fulfilled, (state, action) => {
|
||||
state.isLoading = false;
|
||||
state.success = action.payload.success;
|
||||
state.msg = action.payload.msg;
|
||||
state.monitors = action.payload.data;
|
||||
})
|
||||
.addCase(getUptimeMonitors.rejected, (state, action) => {
|
||||
state.isLoading = false;
|
||||
state.success = false;
|
||||
state.msg = action.payload
|
||||
? action.payload.msg
|
||||
: "Getting uptime monitors failed";
|
||||
})
|
||||
// *****************************************************
|
||||
// Monitors by userId
|
||||
// *****************************************************
|
||||
|
||||
@@ -24,4 +24,55 @@ axiosInstance.interceptors.response.use(
|
||||
}
|
||||
);
|
||||
|
||||
// Create a new monitor
|
||||
axiosInstance.createMonitor = async (authToken, monitor) => {
|
||||
return axiosInstance.post(`/monitors`, monitor, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${authToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
// Get all uptime monitors for a user
|
||||
axiosInstance.getMonitorsByUserId = async (
|
||||
authToken,
|
||||
userId,
|
||||
limit,
|
||||
types,
|
||||
sortOrder,
|
||||
normalize
|
||||
) => {
|
||||
const typeQuery = types.reduce((acc, type) => acc + `&type=${type}`, "");
|
||||
return axiosInstance.get(
|
||||
`/monitors/user/${userId}?limit=${limit}${typeQuery}&sortOrder=${sortOrder}&normalize=${normalize}`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${authToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
// Updates a single monitor
|
||||
axiosInstance.updateMonitor = async (authToken, monitorId, updatedFields) => {
|
||||
return axiosInstance.put(`/monitors/${monitorId}`, updatedFields, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${authToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
// Deletes a single monitor
|
||||
axiosInstance.deleteMonitorById = async (authToken, monitorId) => {
|
||||
return axiosInstance.delete(`/monitors/${monitorId}`, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${authToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export default axiosInstance;
|
||||
Reference in new issue
Block a user