Create page speed function (#483)
* Added pagespeed monitor create function * Added pagespeed slice * Updated slice name * Displayed page speed monitors and updated directory name
This commit is contained in:
1 parent
bdf8d6aaea
commit
979c198702
6 files changed
+251
-73
No files matched your search
@@ -84,6 +84,17 @@ const Field = ({
|
|||||||
value={value}
|
value={value}
|
||||||
onChange={onChange}
|
onChange={onChange}
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
|
sx={
|
||||||
|
type === "url"
|
||||||
|
? {
|
||||||
|
"& .MuiInputBase-root": { padding: 0 },
|
||||||
|
"& .MuiStack-root": {
|
||||||
|
borderTopLeftRadius: `${theme.shape.borderRadius}px`,
|
||||||
|
borderBottomLeftRadius: `${theme.shape.borderRadius}px`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
: {}
|
||||||
|
}
|
||||||
InputProps={{
|
InputProps={{
|
||||||
startAdornment: type === "url" && (
|
startAdornment: type === "url" && (
|
||||||
<Stack
|
<Stack
|
||||||
@@ -92,6 +103,8 @@ const Field = ({
|
|||||||
height="100%"
|
height="100%"
|
||||||
sx={{
|
sx={{
|
||||||
borderRight: `solid 1px ${theme.palette.section.borderColor}`,
|
borderRight: `solid 1px ${theme.palette.section.borderColor}`,
|
||||||
|
backgroundColor: "#f9f9fa",
|
||||||
|
pl: theme.gap.medium,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Typography component="h5" sx={{ lineHeight: 1 }}>
|
<Typography component="h5" sx={{ lineHeight: 1 }}>
|
||||||
|
|||||||
@@ -0,0 +1,146 @@
|
|||||||
|
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
|
||||||
|
import { jwtDecode } from "jwt-decode";
|
||||||
|
import axiosInstance from "../../Utils/axiosConfig";
|
||||||
|
const initialState = {
|
||||||
|
isLoading: false,
|
||||||
|
monitors: [],
|
||||||
|
success: null,
|
||||||
|
msg: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
export const createPageSpeed = createAsyncThunk(
|
||||||
|
"pageSpeedMonitors/createPageSpeed",
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
return thunkApi.rejectWithValue(error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
export const getPageSpeedMonitors = createAsyncThunk(
|
||||||
|
"pageSpeedMonitors/getPageSpeedMonitors",
|
||||||
|
async (token, thunkApi) => {
|
||||||
|
try {
|
||||||
|
const res = await axiosInstance.get("/monitors");
|
||||||
|
return res.data;
|
||||||
|
} catch (error) {
|
||||||
|
if (error.response && error.response.data) {
|
||||||
|
return thunkApi.rejectWithValue(error.response.data);
|
||||||
|
}
|
||||||
|
return thunkApi.rejectWithValue(error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
export const getPageSpeedByUserId = createAsyncThunk(
|
||||||
|
"pageSpeedMonitors/getPageSpeedByUserId",
|
||||||
|
async (token, thunkApi) => {
|
||||||
|
const user = jwtDecode(token);
|
||||||
|
try {
|
||||||
|
const res = await axiosInstance.get(
|
||||||
|
`/monitors/user/${user._id}?limit=25&type=pagespeed&sortOrder=desc`,
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
return res.data;
|
||||||
|
} catch (error) {
|
||||||
|
if (error.response && error.response.data) {
|
||||||
|
return thunkApi.rejectWithValue(error.response.data);
|
||||||
|
}
|
||||||
|
return thunkApi.rejectWithValue(error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const pageSpeedMonitorSlice = createSlice({
|
||||||
|
name: "pageSpeedMonitor",
|
||||||
|
initialState,
|
||||||
|
reducers: {
|
||||||
|
clearMonitorState: (state) => {
|
||||||
|
state.isLoading = false;
|
||||||
|
state.monitors = [];
|
||||||
|
state.success = null;
|
||||||
|
state.msg = null;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
extraReducers: (builder) => {
|
||||||
|
builder
|
||||||
|
// *****************************************************
|
||||||
|
// All Monitors
|
||||||
|
// *****************************************************
|
||||||
|
.addCase(getPageSpeedMonitors.pending, (state) => {
|
||||||
|
state.isLoading = true;
|
||||||
|
})
|
||||||
|
.addCase(getPageSpeedMonitors.fulfilled, (state, action) => {
|
||||||
|
state.isLoading = false;
|
||||||
|
state.success = action.payload.success;
|
||||||
|
state.msg = action.payload.msg;
|
||||||
|
state.monitors = action.payload.data;
|
||||||
|
})
|
||||||
|
.addCase(getPageSpeedMonitors.rejected, (state, action) => {
|
||||||
|
state.isLoading = false;
|
||||||
|
state.success = false;
|
||||||
|
state.msg = action.payload
|
||||||
|
? action.payload.msg
|
||||||
|
: "Getting montiors failed";
|
||||||
|
})
|
||||||
|
// *****************************************************
|
||||||
|
// Monitors by userId
|
||||||
|
// *****************************************************
|
||||||
|
|
||||||
|
.addCase(getPageSpeedByUserId.pending, (state) => {
|
||||||
|
state.isLoading = true;
|
||||||
|
})
|
||||||
|
.addCase(getPageSpeedByUserId.fulfilled, (state, action) => {
|
||||||
|
state.isLoading = false;
|
||||||
|
state.success = action.payload.msg;
|
||||||
|
state.monitors = action.payload.data;
|
||||||
|
})
|
||||||
|
.addCase(getPageSpeedByUserId.rejected, (state, action) => {
|
||||||
|
state.isLoading = false;
|
||||||
|
state.success = false;
|
||||||
|
state.msg = action.payload
|
||||||
|
? action.payload.msg
|
||||||
|
: "Getting page speed monitors failed";
|
||||||
|
})
|
||||||
|
|
||||||
|
// *****************************************************
|
||||||
|
// Create Monitor
|
||||||
|
// *****************************************************
|
||||||
|
.addCase(createPageSpeed.pending, (state) => {
|
||||||
|
state.isLoading = true;
|
||||||
|
})
|
||||||
|
.addCase(createPageSpeed.fulfilled, (state, action) => {
|
||||||
|
state.isLoading = false;
|
||||||
|
state.success = action.payload.success;
|
||||||
|
state.msg = action.payload.msg;
|
||||||
|
})
|
||||||
|
.addCase(createPageSpeed.rejected, (state, action) => {
|
||||||
|
state.isLoading = false;
|
||||||
|
state.success = false;
|
||||||
|
state.msg = action.payload
|
||||||
|
? action.payload.msg
|
||||||
|
: "Failed to create page speed monitor";
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export const { setMonitors, clearMonitorState } = pageSpeedMonitorSlice.actions;
|
||||||
|
|
||||||
|
export default pageSpeedMonitorSlice.reducer;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
.create-page-speed > .MuiStack-root {
|
.create-page-speed form {
|
||||||
position: relative;
|
position: relative;
|
||||||
margin: auto;
|
margin: auto;
|
||||||
margin-top: var(--env-var-default-2);
|
margin-top: var(--env-var-default-2);
|
||||||
|
|||||||
@@ -1,18 +1,25 @@
|
|||||||
import { Box, IconButton, Stack, Typography } from "@mui/material";
|
import { Box, IconButton, Stack, Typography } from "@mui/material";
|
||||||
|
import { useState } from "react";
|
||||||
import { useTheme } from "@emotion/react";
|
import { useTheme } from "@emotion/react";
|
||||||
import CloseRoundedIcon from "@mui/icons-material/CloseRounded";
|
import { useDispatch, useSelector } from "react-redux";
|
||||||
import { useNavigate } from "react-router";
|
import { useNavigate } from "react-router";
|
||||||
|
import CloseRoundedIcon from "@mui/icons-material/CloseRounded";
|
||||||
import Field from "../../../Components/Inputs/Field";
|
import Field from "../../../Components/Inputs/Field";
|
||||||
import Select from "../../../Components/Inputs/Select";
|
import Select from "../../../Components/Inputs/Select";
|
||||||
import Button from "../../../Components/Button";
|
import Button from "../../../Components/Button";
|
||||||
import Checkbox from "../../../Components/Inputs/Checkbox";
|
import Checkbox from "../../../Components/Inputs/Checkbox";
|
||||||
import { useState } from "react";
|
|
||||||
import { monitorValidation } from "../../../Validation/validation";
|
import { monitorValidation } from "../../../Validation/validation";
|
||||||
|
import { createToast } from "../../../Utils/toastUtils";
|
||||||
|
import { createPageSpeed } from "../../../Features/PageSpeedMonitor/pageSpeedMonitorSlice";
|
||||||
import "./index.css";
|
import "./index.css";
|
||||||
|
|
||||||
const CreatePageSpeed = () => {
|
const CreatePageSpeed = () => {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
const dispatch = useDispatch();
|
||||||
|
|
||||||
|
const MS_PER_MINUTE = 60000;
|
||||||
|
const { user, authToken } = useSelector((state) => state.auth);
|
||||||
|
|
||||||
const frequencies = [
|
const frequencies = [
|
||||||
{ _id: 1, name: "1 minute" },
|
{ _id: 1, name: "1 minute" },
|
||||||
@@ -26,7 +33,6 @@ const CreatePageSpeed = () => {
|
|||||||
name: "",
|
name: "",
|
||||||
url: "",
|
url: "",
|
||||||
interval: 1,
|
interval: 1,
|
||||||
type: "pagespeed",
|
|
||||||
});
|
});
|
||||||
const [errors, setErrors] = useState({});
|
const [errors, setErrors] = useState({});
|
||||||
|
|
||||||
@@ -34,25 +40,68 @@ const CreatePageSpeed = () => {
|
|||||||
const { value } = event.target;
|
const { value } = event.target;
|
||||||
setForm((prev) => ({ ...prev, [id]: value }));
|
setForm((prev) => ({ ...prev, [id]: value }));
|
||||||
|
|
||||||
const validation = monitorValidation.validate(
|
const { error } = monitorValidation.validate(
|
||||||
{ [id]: value },
|
{ [id]: value },
|
||||||
{ abortEarly: false }
|
{ abortEarly: false }
|
||||||
);
|
);
|
||||||
|
|
||||||
setErrors((prev) => {
|
setErrors((prev) => {
|
||||||
const updatedErrors = { ...prev };
|
const newErrors = { ...prev };
|
||||||
if (validation.error) {
|
if (error) newErrors[id] = error.details[0].message;
|
||||||
updatedErrors[id] = validation.error.details[0].message;
|
else delete newErrors[id];
|
||||||
} else {
|
return newErrors;
|
||||||
delete updatedErrors[id];
|
|
||||||
}
|
|
||||||
return updatedErrors;
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleCreate = async (event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
|
let monitor = {
|
||||||
|
url: "http://" + form.url,
|
||||||
|
name: form.name === "" ? form.url : form.name,
|
||||||
|
};
|
||||||
|
|
||||||
|
const { error } = monitorValidation.validate(form, { abortEarly: false });
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
const newErrors = {};
|
||||||
|
error.details.forEach((err) => {
|
||||||
|
newErrors[err.path[0]] = err.message;
|
||||||
|
});
|
||||||
|
setErrors(newErrors);
|
||||||
|
createToast({ body: "Error validating data." });
|
||||||
|
} else {
|
||||||
|
monitor = {
|
||||||
|
...monitor,
|
||||||
|
description: monitor.name,
|
||||||
|
userId: user._id,
|
||||||
|
interval: form.interval * MS_PER_MINUTE,
|
||||||
|
type: "pagespeed",
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
const action = await dispatch(createPageSpeed({ authToken, monitor }));
|
||||||
|
if (action.meta.requestStatus === "fulfilled") {
|
||||||
|
navigate("/page-speed");
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
createToast({
|
||||||
|
body:
|
||||||
|
error.details && error.details.length > 0
|
||||||
|
? error.details[0].message
|
||||||
|
: "Unknown error.",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box className="create-page-speed">
|
<Box className="create-page-speed">
|
||||||
<Stack gap={theme.gap.xl}>
|
<form
|
||||||
|
noValidate
|
||||||
|
spellCheck="false"
|
||||||
|
onSubmit={handleCreate}
|
||||||
|
style={{ display: "flex", flexDirection: "column", gap: theme.gap.xl }}
|
||||||
|
>
|
||||||
<IconButton
|
<IconButton
|
||||||
aria-label="close modal"
|
aria-label="close modal"
|
||||||
onClick={() => navigate("/page-speed")}
|
onClick={() => navigate("/page-speed")}
|
||||||
@@ -111,12 +160,12 @@ const CreatePageSpeed = () => {
|
|||||||
<Checkbox
|
<Checkbox
|
||||||
id="notify-email"
|
id="notify-email"
|
||||||
label="Notify via email (to gorkem.cetin@bluewavelabs.ca)"
|
label="Notify via email (to gorkem.cetin@bluewavelabs.ca)"
|
||||||
isChecked={true}
|
isChecked={false}
|
||||||
/>
|
/>
|
||||||
<Checkbox
|
<Checkbox
|
||||||
id="notify-emails"
|
id="notify-emails"
|
||||||
label="Notify via email to following emails"
|
label="Notify via email to following emails"
|
||||||
isChecked={true}
|
isChecked={false}
|
||||||
/>
|
/>
|
||||||
<Box mx={`calc(${theme.gap.ml} * 2)`}>
|
<Box mx={`calc(${theme.gap.ml} * 2)`}>
|
||||||
<Field
|
<Field
|
||||||
@@ -133,10 +182,19 @@ const CreatePageSpeed = () => {
|
|||||||
</Box>
|
</Box>
|
||||||
</Stack>
|
</Stack>
|
||||||
<Stack direction="row" justifyContent="flex-end" gap={theme.gap.small}>
|
<Stack direction="row" justifyContent="flex-end" gap={theme.gap.small}>
|
||||||
<Button level="tertiary" label="Cancel" />
|
<Button
|
||||||
<Button level="primary" label="Create" />
|
level="tertiary"
|
||||||
|
label="Cancel"
|
||||||
|
onClick={() => navigate("/page-speed")}
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
type="submit"
|
||||||
|
level="primary"
|
||||||
|
label="Create"
|
||||||
|
onClick={handleCreate}
|
||||||
|
/>
|
||||||
</Stack>
|
</Stack>
|
||||||
</Stack>
|
</form>
|
||||||
</Box>
|
</Box>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
import { Box, Grid, Stack, Typography } from "@mui/material";
|
import { Box, Grid, Stack, Typography } from "@mui/material";
|
||||||
import Fallback from "../../Components/Fallback";
|
import { useEffect } from "react";
|
||||||
import { useTheme } from "@emotion/react";
|
import { useTheme } from "@emotion/react";
|
||||||
import PageSpeedIcon from "../../assets/icons/page-speed.svg?react";
|
|
||||||
|
|
||||||
import "./index.css";
|
|
||||||
import { formatDate, formatDurationRounded } from "../../Utils/timeUtils";
|
import { formatDate, formatDurationRounded } from "../../Utils/timeUtils";
|
||||||
import { StatusLabel } from "../../Components/Label";
|
import { StatusLabel } from "../../Components/Label";
|
||||||
|
import { useDispatch, useSelector } from "react-redux";
|
||||||
|
import { getPageSpeedByUserId } from "../../Features/PageSpeedMonitor/pageSpeedMonitorSlice";
|
||||||
|
import PageSpeedIcon from "../../assets/icons/page-speed.svg?react";
|
||||||
|
import Fallback from "../../Components/Fallback";
|
||||||
|
import "./index.css";
|
||||||
|
|
||||||
const Card = ({ data }) => {
|
const Card = ({ data }) => {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
@@ -61,56 +63,13 @@ const Card = ({ data }) => {
|
|||||||
|
|
||||||
const PageSpeed = () => {
|
const PageSpeed = () => {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
|
const dispatch = useDispatch();
|
||||||
|
|
||||||
// sample data, remove later
|
const { authToken } = useSelector((state) => state.auth);
|
||||||
let monitors = [
|
const { monitors } = useSelector((state) => state.pageSpeedMonitors);
|
||||||
{
|
useEffect(() => {
|
||||||
success: true,
|
dispatch(getPageSpeedByUserId(authToken));
|
||||||
msg: 'Got monitor for 66a3d58ecd42ab3ed1171cf1 successfully"',
|
}, []);
|
||||||
data: [
|
|
||||||
{
|
|
||||||
_id: "66a3ef558943628c59aabf00",
|
|
||||||
userId: "66a3d58ecd42ab3ed1171cf1",
|
|
||||||
name: "Google",
|
|
||||||
description: "Google",
|
|
||||||
status: true,
|
|
||||||
type: "pagespeed",
|
|
||||||
url: "https://www.google.com",
|
|
||||||
isActive: true,
|
|
||||||
interval: 10000,
|
|
||||||
createdAt: "2024-07-26T18:47:49.212Z",
|
|
||||||
updatedAt: "2024-07-26T18:47:49.212Z",
|
|
||||||
__v: 0,
|
|
||||||
checks: [
|
|
||||||
{
|
|
||||||
_id: "66a3f2266a073a2ff8dd0f7f",
|
|
||||||
monitorId: "66a3ef558943628c59aabf00",
|
|
||||||
status: true,
|
|
||||||
accessibility: 90,
|
|
||||||
bestPractices: 93,
|
|
||||||
seo: 92,
|
|
||||||
performance: 93,
|
|
||||||
createdAt: "2024-07-26T18:59:50.103Z",
|
|
||||||
updatedAt: "2024-07-26T18:59:50.103Z",
|
|
||||||
__v: 0,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
_id: "66a3f2226a073a2ff8dd0f7d",
|
|
||||||
monitorId: "66a3ef558943628c59aabf00",
|
|
||||||
status: true,
|
|
||||||
accessibility: 90,
|
|
||||||
bestPractices: 93,
|
|
||||||
seo: 92,
|
|
||||||
performance: 87,
|
|
||||||
createdAt: "2024-07-26T18:59:46.280Z",
|
|
||||||
updatedAt: "2024-07-26T18:59:46.280Z",
|
|
||||||
__v: 0,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box className="page-speed">
|
<Box className="page-speed">
|
||||||
@@ -121,7 +80,7 @@ const PageSpeed = () => {
|
|||||||
Click on one of the monitors to get more site speed information.
|
Click on one of the monitors to get more site speed information.
|
||||||
</Typography>
|
</Typography>
|
||||||
<Grid container spacing={theme.gap.large}>
|
<Grid container spacing={theme.gap.large}>
|
||||||
{monitors[0].data?.map((monitor) => (
|
{monitors?.map((monitor) => (
|
||||||
<Card data={monitor} key={`monitor-${monitor._id}`} />
|
<Card data={monitor} key={`monitor-${monitor._id}`} />
|
||||||
))}
|
))}
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|||||||
+3
-1
@@ -1,4 +1,5 @@
|
|||||||
import { configureStore, combineReducers } from "@reduxjs/toolkit";
|
import { configureStore, combineReducers } from "@reduxjs/toolkit";
|
||||||
|
import pageSpeedMonitorReducer from "./Features/PageSpeedMonitor/pageSpeedMonitorSlice";
|
||||||
import monitorsReducer from "./Features/Monitors/monitorsSlice";
|
import monitorsReducer from "./Features/Monitors/monitorsSlice";
|
||||||
import authReducer from "./Features/Auth/authSlice";
|
import authReducer from "./Features/Auth/authSlice";
|
||||||
import storage from "redux-persist/lib/storage";
|
import storage from "redux-persist/lib/storage";
|
||||||
@@ -18,13 +19,14 @@ const authTransform = createTransform(
|
|||||||
const persistConfig = {
|
const persistConfig = {
|
||||||
key: "root",
|
key: "root",
|
||||||
storage,
|
storage,
|
||||||
whitielist: ["auth", "monitors"],
|
whitielist: ["auth", "monitors", "pageSpeed"],
|
||||||
transforms: [authTransform],
|
transforms: [authTransform],
|
||||||
};
|
};
|
||||||
|
|
||||||
const rootReducer = combineReducers({
|
const rootReducer = combineReducers({
|
||||||
monitors: monitorsReducer,
|
monitors: monitorsReducer,
|
||||||
auth: authReducer,
|
auth: authReducer,
|
||||||
|
pageSpeedMonitors: pageSpeedMonitorReducer,
|
||||||
});
|
});
|
||||||
|
|
||||||
const persistedReducer = persistReducer(persistConfig, rootReducer);
|
const persistedReducer = persistReducer(persistConfig, rootReducer);
|
||||||
|
|||||||
Reference in new issue
Block a user