Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor network service to use config objects for clarity #858

Merged
merged 3 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions Client/src/Components/TabPanels/Account/TeamPanel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ const TeamPanel = () => {
useEffect(() => {
const fetchTeam = async () => {
try {
const response = await networkService.getAllUsers(authToken);
const response = await networkService.getAllUsers({
authToken: authToken,
});
setMembers(response.data.data);
} catch (error) {
createToast({
Expand Down Expand Up @@ -187,11 +189,11 @@ const TeamPanel = () => {
}

try {
await networkService.requestInvitationToken(
authToken,
toInvite.email,
toInvite.role
);
await networkService.requestInvitationToken({
authToken: authToken,
email: toInvite.email,
role: toInvite.role,
});
closeInviteModal();
createToast({
body: "Member invited. They will receive an email with details on how to create their account.",
Expand Down
19 changes: 14 additions & 5 deletions Client/src/Features/Auth/authSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ export const update = createAsyncThunk(
form.deleteProfileImage &&
fd.append("deleteProfileImage", form.deleteProfileImage);

const res = await networkService.updateUser(token, user._id, fd);
console.log(res);
const res = await networkService.updateUser({
authToken: token,
userId: user._id,
form: fd,
});
ajhollid marked this conversation as resolved.
Show resolved Hide resolved
return res.data;
} catch (error) {
if (error.response && error.response.data) {
Expand All @@ -88,7 +91,10 @@ export const deleteUser = createAsyncThunk(
const user = jwtDecode(data);

try {
const res = await networkService.deleteUser(data, user._id);
const res = await networkService.deleteUser({
authToken: data,
userId: user._id,
});
return res.data;
} catch (error) {
if (error.response && error.response.data) {
Expand Down Expand Up @@ -127,8 +133,11 @@ export const setNewPassword = createAsyncThunk(
async (data, thunkApi) => {
const { token, form } = data;
try {
await networkService.validateRecoveryToken(token);
const res = await networkService.setNewPassword(token, form);
await networkService.validateRecoveryToken({ recoveryToken: token });
const res = await networkService.setNewPassword({
recoveryToken: token,
form: form,
});
return res.data;
} catch (error) {
if (error.response.data) {
Expand Down
43 changes: 26 additions & 17 deletions Client/src/Features/PageSpeedMonitor/pageSpeedMonitorSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ export const createPageSpeed = createAsyncThunk(
async (data, thunkApi) => {
try {
const { authToken, monitor } = data;
const res = await networkService.createMonitor(authToken, monitor);
const res = await networkService.createMonitor({
authToken: authToken,
monitor: monitor,
});
return res.data;
} catch (error) {
if (error.response && error.response.data) {
Expand All @@ -33,7 +36,10 @@ export const getPagespeedMonitorById = createAsyncThunk(
async (data, thunkApi) => {
try {
const { authToken, monitorId } = data;
const res = await networkService.getMonitorByid(authToken, monitorId);
const res = await networkService.getMonitorById({
authToken: authToken,
monitorId: monitorId,
});
return res.data;
} catch (error) {
if (error.response && error.response.data) {
Expand All @@ -53,11 +59,11 @@ export const getPageSpeedByTeamId = createAsyncThunk(
async (token, thunkApi) => {
const user = jwtDecode(token);
try {
const res = await networkService.getMonitorsAndSummaryByTeamId(
token,
user.teamId,
["pagespeed"]
);
const res = await networkService.getMonitorsAndSummaryByTeamId({
authToken: token,
teamId: user.teamId,
types: ["pagespeed"],
});

return res.data;
} catch (error) {
Expand All @@ -84,11 +90,11 @@ export const updatePageSpeed = createAsyncThunk(
interval: monitor.interval,
// notifications: monitor.notifications,
};
const res = await networkService.updateMonitor(
authToken,
monitor._id,
updatedFields
);
const res = await networkService.updateMonitor({
authToken: authToken,
monitorId: monitor._id,
updatedFields: updatedFields,
});
return res.data;
} catch (error) {
if (error.response && error.response.data) {
Expand All @@ -108,10 +114,10 @@ export const deletePageSpeed = createAsyncThunk(
async (data, thunkApi) => {
try {
const { authToken, monitor } = data;
const res = await networkService.deleteMonitorById(
authToken,
monitor._id
);
const res = await networkService.deleteMonitorById({
authToken: authToken,
monitorId: monitor._id,
});
return res.data;
} catch (error) {
if (error.response && error.response.data) {
Expand All @@ -130,7 +136,10 @@ export const pausePageSpeed = createAsyncThunk(
async (data, thunkApi) => {
try {
const { authToken, monitorId } = data;
const res = await networkService.pauseMonitorById(authToken, monitorId);
const res = await networkService.pauseMonitorById({
authToken: authToken,
monitorId: monitorId,
});
return res.data;
} catch (error) {
if (error.response && error.response.data) {
Expand Down
56 changes: 36 additions & 20 deletions Client/src/Features/UptimeMonitors/uptimeMonitorsSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ export const createUptimeMonitor = createAsyncThunk(
async (data, thunkApi) => {
try {
const { authToken, monitor } = data;
const res = await networkService.createMonitor(authToken, monitor);
const res = await networkService.createMonitor({
authToken: authToken,
monitor: monitor,
});
ajhollid marked this conversation as resolved.
Show resolved Hide resolved
return res.data;
} catch (error) {
if (error.response && error.response.data) {
Expand All @@ -33,7 +36,10 @@ export const getUptimeMonitorById = createAsyncThunk(
async (data, thunkApi) => {
try {
const { authToken, monitorId } = data;
const res = await networkService.getMonitorByid(authToken, monitorId);
const res = await networkService.getMonitorById({
authToken: authToken,
monitorId: monitorId,
});
return res.data;
} catch (error) {
if (error.response && error.response.data) {
Expand All @@ -53,11 +59,11 @@ export const getUptimeMonitorsByTeamId = createAsyncThunk(
async (token, thunkApi) => {
const user = jwtDecode(token);
try {
const res = await networkService.getMonitorsAndSummaryByTeamId(
token,
user.teamId,
["http", "ping"]
);
const res = await networkService.getMonitorsAndSummaryByTeamId({
authToken: token,
teamId: user.teamId,
types: ["http", "ping"],
});
return res.data;
} catch (error) {
if (error.response && error.response.data) {
Expand All @@ -83,11 +89,11 @@ export const updateUptimeMonitor = createAsyncThunk(
interval: monitor.interval,
notifications: monitor.notifications,
};
const res = await networkService.updateMonitor(
authToken,
monitor._id,
updatedFields
);
const res = await networkService.updateMonitor({
authToken: authToken,
monitorId: monitor._id,
updatedFields: updatedFields,
});
return res.data;
} catch (error) {
if (error.response && error.response.data) {
Expand All @@ -107,10 +113,10 @@ export const deleteUptimeMonitor = createAsyncThunk(
async (data, thunkApi) => {
try {
const { authToken, monitor } = data;
const res = await networkService.deleteMonitorById(
authToken,
monitor._id
);
const res = await networkService.deleteMonitorById({
authToken: authToken,
monitorId: monitor._id,
});
return res.data;
} catch (error) {
if (error.response && error.response.data) {
Expand All @@ -130,7 +136,10 @@ export const pauseUptimeMonitor = createAsyncThunk(
async (data, thunkApi) => {
try {
const { authToken, monitorId } = data;
const res = await networkService.pauseMonitorById(authToken, monitorId);
const res = await networkService.pauseMonitorById({
authToken: authToken,
monitorId: monitorId,
});
return res.data;
} catch (error) {
if (error.response && error.response.data) {
Expand All @@ -150,7 +159,10 @@ export const deleteMonitorChecksByTeamId = createAsyncThunk(
async (data, thunkApi) => {
try {
const { authToken, teamId } = data;
const res = await networkService.deleteChecksByTeamId(authToken, teamId);
const res = await networkService.deleteChecksByTeamId({
authToken: authToken,
teamId: teamId,
});
return res.data;
} catch (error) {
if (error.response && error.response.data) {
Expand All @@ -169,7 +181,9 @@ export const addDemoMonitors = createAsyncThunk(
async (data, thunkApi) => {
try {
const { authToken } = data;
const res = await networkService.addDemoMonitors(authToken);
const res = await networkService.addDemoMonitors({
authToken: authToken,
});
return res.data;
} catch (error) {
if (error.response && error.response.data) {
Expand All @@ -188,7 +202,9 @@ export const deleteAllMonitors = createAsyncThunk(
async (data, thunkApi) => {
try {
const { authToken } = data;
const res = await networkService.deleteAllMonitors(authToken);
const res = await networkService.deleteAllMonitors({
authToken: authToken,
});
return res.data;
} catch (error) {
if (error.response && error.response.data) {
Expand Down
40 changes: 20 additions & 20 deletions Client/src/Pages/Incidents/IncidentTable/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,27 @@ const IncidentTable = ({ monitors, selectedMonitor, filter }) => {
try {
let res;
if (selectedMonitor === "0") {
res = await networkService.getChecksByTeam(
authToken,
user.teamId,
"desc",
null,
null,
filter,
paginationController.page,
paginationController.rowsPerPage
);
res = await networkService.getChecksByTeam({
authToken: authToken,
userId: user.teamId,
sortOrder: "desc",
limit: null,
dateRange: null,
filter: filter,
page: paginationController.page,
rowsPerPage: paginationController.rowsPerPage,
});
} else {
res = await networkService.getChecksByMonitor(
authToken,
selectedMonitor,
"desc",
null,
null,
filter,
paginationController.page,
paginationController.rowsPerPage
);
res = await networkService.getChecksByMonitor({
authToken: authToken,
monitorId: selectedMonitor,
sortOrder: "desc",
limit: null,
dateRange: null,
sitler: filter,
page: paginationController.page,
rowsPerPage: paginationController.rowsPerPage,
});
}
setChecks(res.data.data.checks);
setChecksCount(res.data.data.checksCount);
Expand Down
20 changes: 10 additions & 10 deletions Client/src/Pages/Monitors/Details/PaginationTable/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ const PaginationTable = ({ monitorId, dateRange }) => {
useEffect(() => {
const fetchPage = async () => {
try {
const res = await networkService.getChecksByMonitor(
authToken,
monitorId,
"desc",
null,
dateRange,
null,
paginationController.page,
paginationController.rowsPerPage
);
const res = await networkService.getChecksByMonitor({
authToken: authToken,
monitoirId: monitorId,
sortOrder: "desc",
limit: null,
dateRange: dateRange,
filter: null,
page: paginationController.page,
rowsPerPage: paginationController.rowsPerPage,
});
setChecks(res.data.data.checks);
setChecksCount(res.data.data.checksCount);
} catch (error) {
Expand Down
26 changes: 13 additions & 13 deletions Client/src/Pages/Monitors/Details/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ const DetailsPage = ({ isAdmin }) => {

const fetchMonitor = useCallback(async () => {
try {
const res = await networkService.getStatsByMonitorId(
authToken,
monitorId,
null,
null,
dateRange,
50,
true
);
const res = await networkService.getStatsByMonitorId({
authToken: authToken,
monitorId: monitorId,
sortOrder: null,
limit: null,
dateRange: dateRange,
numToDisplay: 50,
normalize: true,
});
setMonitor(res?.data?.data ?? {});
} catch (error) {
logger.error(error);
Expand All @@ -89,10 +89,10 @@ const DetailsPage = ({ isAdmin }) => {
return;
}
try {
const res = await networkService.getCertificateExpiry(
authToken,
monitorId
);
const res = await networkService.getCertificateExpiry({
authToken: authToken,
monitorId: monitorId,
});

if (res?.data?.data?.certificateDate) {
let [month, day, year] = res.data.data.certificateDate.split("/");
Expand Down
Loading