2024-06-04 13:32:17 -07:00
|
|
|
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
|
2024-06-21 11:08:31 -07:00
|
|
|
import { jwtDecode } from "jwt-decode";
|
2024-08-18 12:11:24 -07:00
|
|
|
import axiosInstance from "../../Utils/NetworkService";
|
2024-06-04 12:40:05 -07:00
|
|
|
const initialState = {
|
|
|
|
|
isLoading: false,
|
|
|
|
|
monitors: [],
|
|
|
|
|
success: null,
|
|
|
|
|
msg: null,
|
|
|
|
|
};
|
2024-06-04 12:15:35 -07:00
|
|
|
|
2024-07-30 11:25:14 -07:00
|
|
|
export const createUptimeMonitor = createAsyncThunk(
|
2024-06-27 15:27:32 -07:00
|
|
|
"monitors/createMonitor",
|
|
|
|
|
async (data, thunkApi) => {
|
|
|
|
|
try {
|
|
|
|
|
const { authToken, monitor } = data;
|
2024-08-17 09:35:50 -07:00
|
|
|
const res = await axiosInstance.createMonitor(authToken, monitor);
|
2024-06-04 13:32:17 -07:00
|
|
|
return res.data;
|
|
|
|
|
} catch (error) {
|
2024-06-25 11:14:16 -07:00
|
|
|
if (error.response && error.response.data) {
|
|
|
|
|
return thunkApi.rejectWithValue(error.response.data);
|
|
|
|
|
}
|
2024-08-13 13:24:35 -04:00
|
|
|
const payload = {
|
|
|
|
|
status: false,
|
|
|
|
|
msg: error.message ? error.message : "Unknown error",
|
|
|
|
|
};
|
|
|
|
|
return thunkApi.rejectWithValue(payload);
|
2024-06-04 13:32:17 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2024-07-30 11:25:14 -07:00
|
|
|
export const getUptimeMonitorsByUserId = createAsyncThunk(
|
2024-06-20 13:50:31 -07:00
|
|
|
"montiors/getMonitorsByUserId",
|
|
|
|
|
async (token, thunkApi) => {
|
2024-06-21 11:08:31 -07:00
|
|
|
const user = jwtDecode(token);
|
2024-06-20 13:50:31 -07:00
|
|
|
try {
|
2024-08-17 09:35:50 -07:00
|
|
|
const res = await axiosInstance.getMonitorsByUserId(
|
|
|
|
|
token,
|
|
|
|
|
user._id,
|
|
|
|
|
25,
|
|
|
|
|
["http", "ping"],
|
|
|
|
|
"desc",
|
|
|
|
|
true
|
2024-07-12 10:24:46 -07:00
|
|
|
);
|
2024-06-21 11:08:31 -07:00
|
|
|
return res.data;
|
2024-06-20 13:50:31 -07:00
|
|
|
} catch (error) {
|
2024-06-25 11:14:16 -07:00
|
|
|
if (error.response && error.response.data) {
|
|
|
|
|
return thunkApi.rejectWithValue(error.response.data);
|
|
|
|
|
}
|
2024-08-13 13:24:35 -04:00
|
|
|
const payload = {
|
|
|
|
|
status: false,
|
|
|
|
|
msg: error.message ? error.message : "Unknown error",
|
|
|
|
|
};
|
|
|
|
|
return thunkApi.rejectWithValue(payload);
|
2024-06-20 13:50:31 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2024-07-31 11:00:45 -07:00
|
|
|
export const updateUptimeMonitor = createAsyncThunk(
|
|
|
|
|
"monitors/updateMonitor",
|
|
|
|
|
async (data, thunkApi) => {
|
|
|
|
|
try {
|
|
|
|
|
const { authToken, monitor } = data;
|
2024-07-31 11:36:51 -07:00
|
|
|
const updatedFields = {
|
|
|
|
|
name: monitor.name,
|
|
|
|
|
description: monitor.description,
|
|
|
|
|
interval: monitor.interval,
|
2024-08-02 11:54:46 -07:00
|
|
|
notifications: monitor.notifications,
|
2024-07-31 11:36:51 -07:00
|
|
|
};
|
2024-08-17 09:35:50 -07:00
|
|
|
const res = await axiosInstance.updateMonitor(
|
|
|
|
|
authToken,
|
|
|
|
|
monitor._id,
|
|
|
|
|
updatedFields
|
2024-07-31 11:36:51 -07:00
|
|
|
);
|
|
|
|
|
return res.data;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (error.response && error.response.data) {
|
|
|
|
|
return thunkApi.rejectWithValue(error.response.data);
|
|
|
|
|
}
|
2024-08-13 13:24:35 -04:00
|
|
|
const payload = {
|
|
|
|
|
status: false,
|
|
|
|
|
msg: error.message ? error.message : "Unknown error",
|
|
|
|
|
};
|
|
|
|
|
return thunkApi.rejectWithValue(payload);
|
2024-07-31 11:36:51 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export const deleteUptimeMonitor = createAsyncThunk(
|
|
|
|
|
"monitors/deleteMonitor",
|
|
|
|
|
async (data, thunkApi) => {
|
|
|
|
|
try {
|
|
|
|
|
const { authToken, monitor } = data;
|
2024-08-17 09:35:50 -07:00
|
|
|
const res = await axiosInstance.deleteMonitorById(authToken, monitor._id);
|
2024-07-31 11:00:45 -07:00
|
|
|
return res.data;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (error.response && error.response.data) {
|
|
|
|
|
return thunkApi.rejectWithValue(error.response.data);
|
|
|
|
|
}
|
2024-08-13 13:24:35 -04:00
|
|
|
const payload = {
|
|
|
|
|
status: false,
|
|
|
|
|
msg: error.message ? error.message : "Unknown error",
|
|
|
|
|
};
|
|
|
|
|
return thunkApi.rejectWithValue(payload);
|
2024-07-31 11:00:45 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2024-07-30 11:25:14 -07:00
|
|
|
const uptimeMonitorsSlice = createSlice({
|
|
|
|
|
name: "uptimeMonitors",
|
2024-06-04 12:15:35 -07:00
|
|
|
initialState,
|
2024-06-24 11:56:25 -07:00
|
|
|
reducers: {
|
2024-07-30 11:25:14 -07:00
|
|
|
clearUptimeMonitorState: (state) => {
|
2024-06-24 11:56:25 -07:00
|
|
|
state.isLoading = false;
|
|
|
|
|
state.monitors = [];
|
|
|
|
|
state.success = null;
|
|
|
|
|
state.msg = null;
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-06-04 13:32:17 -07:00
|
|
|
extraReducers: (builder) => {
|
|
|
|
|
builder
|
2024-06-20 13:50:31 -07:00
|
|
|
// *****************************************************
|
|
|
|
|
// Monitors by userId
|
|
|
|
|
// *****************************************************
|
|
|
|
|
|
2024-07-30 11:25:14 -07:00
|
|
|
.addCase(getUptimeMonitorsByUserId.pending, (state) => {
|
2024-06-20 13:50:31 -07:00
|
|
|
state.isLoading = true;
|
|
|
|
|
})
|
2024-07-30 11:25:14 -07:00
|
|
|
.addCase(getUptimeMonitorsByUserId.fulfilled, (state, action) => {
|
2024-06-20 13:50:31 -07:00
|
|
|
state.isLoading = false;
|
|
|
|
|
state.success = action.payload.msg;
|
|
|
|
|
state.monitors = action.payload.data;
|
|
|
|
|
})
|
2024-07-30 11:25:14 -07:00
|
|
|
.addCase(getUptimeMonitorsByUserId.rejected, (state, action) => {
|
2024-06-20 13:50:31 -07:00
|
|
|
state.isLoading = false;
|
2024-06-25 11:14:16 -07:00
|
|
|
state.success = false;
|
|
|
|
|
state.msg = action.payload
|
|
|
|
|
? action.payload.msg
|
2024-07-30 11:25:14 -07:00
|
|
|
: "Getting uptime monitors failed";
|
2024-06-27 15:27:32 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// *****************************************************
|
|
|
|
|
// Create Monitor
|
|
|
|
|
// *****************************************************
|
2024-07-30 11:25:14 -07:00
|
|
|
.addCase(createUptimeMonitor.pending, (state) => {
|
2024-06-27 15:27:32 -07:00
|
|
|
state.isLoading = true;
|
|
|
|
|
})
|
2024-07-30 11:25:14 -07:00
|
|
|
.addCase(createUptimeMonitor.fulfilled, (state, action) => {
|
2024-06-27 15:27:32 -07:00
|
|
|
state.isLoading = false;
|
|
|
|
|
state.success = action.payload.success;
|
|
|
|
|
state.msg = action.payload.msg;
|
|
|
|
|
})
|
2024-07-30 11:25:14 -07:00
|
|
|
.addCase(createUptimeMonitor.rejected, (state, action) => {
|
2024-06-27 15:27:32 -07:00
|
|
|
state.isLoading = false;
|
|
|
|
|
state.success = false;
|
|
|
|
|
state.msg = action.payload
|
|
|
|
|
? action.payload.msg
|
2024-07-30 11:25:14 -07:00
|
|
|
: "Failed to create uptime monitor";
|
2024-07-31 11:00:45 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// *****************************************************
|
|
|
|
|
// update Monitor
|
|
|
|
|
// *****************************************************
|
|
|
|
|
.addCase(updateUptimeMonitor.pending, (state) => {
|
|
|
|
|
state.isLoading = true;
|
|
|
|
|
})
|
|
|
|
|
.addCase(updateUptimeMonitor.fulfilled, (state, action) => {
|
|
|
|
|
state.isLoading = false;
|
|
|
|
|
state.success = action.payload.success;
|
|
|
|
|
state.msg = action.payload.msg;
|
|
|
|
|
})
|
|
|
|
|
.addCase(updateUptimeMonitor.rejected, (state, action) => {
|
|
|
|
|
state.isLoading = false;
|
|
|
|
|
state.success = false;
|
|
|
|
|
state.msg = action.payload
|
|
|
|
|
? action.payload.msg
|
|
|
|
|
: "Failed to update uptime monitor";
|
2024-07-31 11:36:51 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// *****************************************************
|
|
|
|
|
// Delete Monitor
|
|
|
|
|
// *****************************************************
|
|
|
|
|
.addCase(deleteUptimeMonitor.pending, (state) => {
|
|
|
|
|
state.isLoading = true;
|
|
|
|
|
})
|
|
|
|
|
.addCase(deleteUptimeMonitor.fulfilled, (state, action) => {
|
|
|
|
|
state.isLoading = false;
|
|
|
|
|
state.success = action.payload.success;
|
|
|
|
|
state.msg = action.payload.msg;
|
|
|
|
|
})
|
|
|
|
|
.addCase(deleteUptimeMonitor.rejected, (state, action) => {
|
|
|
|
|
state.isLoading = false;
|
|
|
|
|
state.success = false;
|
|
|
|
|
state.msg = action.payload
|
|
|
|
|
? action.payload.msg
|
|
|
|
|
: "Failed to delete uptime monitor";
|
2024-06-04 13:32:17 -07:00
|
|
|
});
|
2024-06-04 12:40:05 -07:00
|
|
|
},
|
2024-06-04 12:15:35 -07:00
|
|
|
});
|
|
|
|
|
|
2024-07-30 11:25:14 -07:00
|
|
|
export const { setUptimeMonitors, clearUptimeMonitorState } =
|
|
|
|
|
uptimeMonitorsSlice.actions;
|
2024-06-04 12:15:35 -07:00
|
|
|
|
2024-07-30 11:25:14 -07:00
|
|
|
export default uptimeMonitorsSlice.reducer;
|