Skip to content

fix: update ErrorDialog logic and tests #10111

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

Merged
merged 7 commits into from
Oct 6, 2023
Merged
Changes from 1 commit
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
Next Next commit
fix: make error text less naggy
  • Loading branch information
Parkreiner committed Oct 6, 2023
commit 261a5ef50546730d2a25e9a75008b0ae6181e530
122 changes: 65 additions & 57 deletions site/src/components/Dialogs/DeleteDialog/DeleteDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import makeStyles from "@mui/styles/makeStyles";
import {
type FC,
type FormEvent,
type PropsWithChildren,
useId,
useState,
} from "react";

import { useTheme } from "@emotion/react";
import TextField from "@mui/material/TextField";
import { ChangeEvent, useState, PropsWithChildren, FC } from "react";
import { ConfirmDialog } from "../ConfirmDialog/ConfirmDialog";

export interface DeleteDialogProps {
Expand All @@ -22,51 +29,23 @@ export const DeleteDialog: FC<PropsWithChildren<DeleteDialogProps>> = ({
name,
confirmLoading,
}) => {
const styles = useStyles();
const [nameValue, setNameValue] = useState("");
const confirmed = name === nameValue;
const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
setNameValue(event.target.value);
};
const hasError = nameValue.length > 0 && !confirmed;
const hookId = useId();
const theme = useTheme();

const content = (
<>
<p>Deleting this {entity} is irreversible!</p>
{Boolean(info) && <p className={styles.warning}>{info}</p>}
<p>Are you sure you want to proceed?</p>
<p>
Type &ldquo;<strong>{name}</strong>&rdquo; below to confirm.
</p>
const [confirmationText, setConfirmationText] = useState("");
const [isFocused, setIsFocused] = useState(false);

<form
onSubmit={(e) => {
e.preventDefault();
if (confirmed) {
onConfirm();
}
}}
>
<TextField
fullWidth
autoFocus
className={styles.textField}
name="confirmation"
autoComplete="off"
id="confirmation"
placeholder={name}
value={nameValue}
onChange={handleChange}
label={`Name of the ${entity} to delete`}
error={hasError}
helperText={
hasError && `${nameValue} does not match the name of this ${entity}`
}
inputProps={{ ["data-testid"]: "delete-dialog-name-confirmation" }}
/>
</form>
</>
);
const onSubmit = (e: FormEvent) => {
e.preventDefault();
if (deletionConfirmed) {
onConfirm();
}
};

const confirmationId = `${hookId}-${DeleteDialog.name}-confirm`;
const deletionConfirmed = name === confirmationText;
const hasError =
!isFocused && !deletionConfirmed && confirmationText.length > 0;

return (
<ConfirmDialog
Expand All @@ -76,19 +55,48 @@ export const DeleteDialog: FC<PropsWithChildren<DeleteDialogProps>> = ({
title={`Delete ${entity}`}
onConfirm={onConfirm}
onClose={onCancel}
description={content}
confirmLoading={confirmLoading}
disabled={!confirmed}
disabled={!deletionConfirmed}
description={
<>
<p>Deleting this {entity} is irreversible!</p>

{Boolean(info) && (
<p css={{ color: theme.palette.warning.light }}>{info}</p>
)}

<p>Are you sure you want to proceed?</p>

<p>
Type &ldquo;<strong>{name}</strong>&rdquo; below to confirm.
</p>

<form onSubmit={onSubmit}>
<TextField
fullWidth
autoFocus
sx={{ marginTop: theme.spacing(3) }}
name="confirmation"
autoComplete="off"
id={confirmationId}
placeholder={name}
value={confirmationText}
onChange={(event) => setConfirmationText(event.target.value)}
onFocus={() => setIsFocused(true)}
onBlur={() => setIsFocused(false)}
label={`Name of the ${entity} to delete`}
error={hasError}
helperText={
hasError &&
`${confirmationText} does not match the name of this ${entity}`
}
inputProps={{
["data-testid"]: "delete-dialog-name-confirmation",
}}
/>
</form>
</>
}
/>
);
};

const useStyles = makeStyles((theme) => ({
warning: {
color: theme.palette.warning.light,
},

textField: {
marginTop: theme.spacing(3),
},
}));