Skip to content
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
24 changes: 16 additions & 8 deletions app/components/modal/monitor/configure.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ const MonitorConfigureModal = ({
isEdit = false,
}: ModalProps) => {
const [pageId, setPageId] = useState('basic');
const { errors, inputs, handleActionButtons, handleInput } = useMonitorForm(
const { errors, inputs, handleActionButtons, handleInput, errorPages, setErrorPages } = useMonitorForm(
monitor,
isEdit,
closeModal,
handleMonitorSubmit
handleMonitorSubmit,
setPageId
);

return (
Expand All @@ -65,12 +66,19 @@ const MonitorConfigureModal = ({
<div className="monitor-configure-left-pages">
{pages.map((page) => (
<div
className={
page.id === pageId
? 'monitor-configure-page-button active'
: 'monitor-configure-page-button'
}
onClick={() => setPageId(page.id)}
className={`monitor-configure-page-button
${page.id === pageId ? 'active' : ''}
${errorPages.has(page.id) ? 'error-circle' : ''}`}

Comment on lines +69 to +72
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this to classnames to make it a bit easier to read

onClick={() => {
setErrorPages(oldSet => {
const trimmedErrorPages = new Set([...oldSet])
trimmedErrorPages.delete(pageId)
return trimmedErrorPages
})
setPageId(page.id)

}}
key={page.id}
>
{page.icon}
Expand Down
12 changes: 12 additions & 0 deletions app/components/modal/monitor/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
}

.monitor-configure-page-button {
position: relative;
padding: 12px;
background-color: var(--accent-800);
border-radius: 8px;
Expand All @@ -31,6 +32,17 @@
&:hover {
background-color: var(--accent-700);
}

&.error-circle::after {
content: "";
position: absolute;
top: 40%;
right: 10px;
width: 10px;
height: 10px;
background-color: red;
border-radius: 50%;
}
Comment on lines +36 to +45
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not now but moving this to an explanation mark or some sort of icon in the future

}

.monitor-configure-left-container {
Expand Down
49 changes: 43 additions & 6 deletions app/hooks/useMonitorForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,71 @@ const useMonitorForm = (
values: Partial<MonitorProps> = defaultInputs,
isEdit: boolean = false,
closeModal: () => void,
setMonitor: (monitor: Partial<MonitorProps>) => void
setMonitor: (monitor: Partial<MonitorProps>) => void,
setPageId: (id: string) => void,
) => {
const [inputs, setInput] = useState<Partial<MonitorProps>>({
...defaultInputs,
...values,
});
const [errors, setErrors] = useState({});
const [errorPages, setErrorPages] = useState<Set<string>>(new Set())

const handleInput = (name: string, value: any) => {
setInput((prev) => ({ ...prev, [name]: value }));
};

const getPagesWithErrors = (errorsObj: Record<string,string>) => {

const associatedPage: Record<string, string> = {
'name': "basic",
'type': "basic",
'url': "basic",
'port': "basic",
'icon': "basic",
'method': "basic",
'json_query': "basic",
'interval': "interval",
'retry': "interval",
'retryInterval': "interval",
'requestTimeout': "interval",
'notificationType': "notification",
'headers': "advanced",
'body': "advanced",
'valid_status_codes': "advanced",
}
Comment on lines +43 to +59
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Figuring out a way to create a consistent list between all input types so this doesn't need to be updated every time a new monitor type is created.

const pagesWithError = new Set<string>();

Object.keys(errorsObj).forEach(error => {
const page = associatedPage[error]
if(error && page) pagesWithError.add(page)
})

return pagesWithError
}


const handleActionButtons = (action: string) => () => {
switch (action) {
case 'Create': {
const type = inputs.type ?? defaultInputs.type;
const validator = monitorValidators[type];
if (!validator) return console.log("Validator doesn't exist");

const errorsObj = validator(inputs);
const errorsObj = validator(inputs) as Record<string, string> | false;

if (errorsObj !== false) {
const pagesWithErrors = getPagesWithErrors(errorsObj)
setErrorPages(pagesWithErrors);
setPageId(Array.from(pagesWithErrors)[0])

if (errorsObj) {
setErrors(errorsObj);
setErrors(errorsObj);
break;
}

setErrorPages(new Set<string>())
setErrors({});

handleMonitor(inputs, isEdit, closeModal, setMonitor);
break;
}
Expand All @@ -66,7 +103,7 @@ const useMonitorForm = (
}
};

return { inputs, errors, handleActionButtons, handleInput };
return { inputs, errors, handleActionButtons, handleInput, errorPages, setErrorPages };
};

export default useMonitorForm;