Skip to content

Commit

Permalink
Add conflict checking to CreateModal
Browse files Browse the repository at this point in the history
  • Loading branch information
devleejb committed Jan 24, 2024
1 parent 0eabbb5 commit d208591
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
38 changes: 36 additions & 2 deletions frontend/src/components/modals/CreateModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {
} from "@mui/material";
import { FormContainer, TextFieldElement } from "react-hook-form-mui";
import CloseIcon from "@mui/icons-material/Close";
import { useMemo, useState } from "react";
import { useCheckNameConflictQuery } from "../../hooks/api/check";
import { useDebounce } from "react-use";

interface CreateRequest {
title: string;
Expand All @@ -18,10 +21,28 @@ interface CreateRequest {
interface CreateModalProps extends Omit<ModalProps, "children"> {
title: string;
onSuccess: (data: CreateRequest) => Promise<void>;
enableConflictCheck?: boolean;
}

function CreateModal(props: CreateModalProps) {
const { title, onSuccess, ...modalProps } = props;
const { title, onSuccess, enableConflictCheck, ...modalProps } = props;
const [nickname, setNickname] = useState("");
const [debouncedNickname, setDebouncedNickname] = useState("");
const { data: conflictResult } = useCheckNameConflictQuery(debouncedNickname);
const errorMessage = useMemo(() => {
if (conflictResult?.conflict) {
return "Already Exists";
}
return null;
}, [conflictResult?.conflict]);

useDebounce(
() => {
setDebouncedNickname(nickname);
},
500,
[nickname]
);

const handleCloseModal = () => {
modalProps?.onClose?.(new Event("Close Modal"), "escapeKeyDown");
Expand All @@ -32,6 +53,11 @@ function CreateModal(props: CreateModalProps) {
handleCloseModal();
};

const handleNicknameChange = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
if (!enableConflictCheck) return;
setNickname(e.target.value);
};

return (
<Modal disableAutoFocus {...modalProps}>
<Paper
Expand Down Expand Up @@ -68,8 +94,16 @@ function CreateModal(props: CreateModalProps) {
inputProps={{
maxLength: 255,
}}
onChange={handleNicknameChange}
error={Boolean(errorMessage)}
helperText={errorMessage}
/>
<Button type="submit" variant="contained" size="large">
<Button
type="submit"
variant="contained"
size="large"
disabled={Boolean(errorMessage)}
>
OK
</Button>
</Stack>
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/popovers/WorkspaceListPopover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ function WorkspaceListPopover(props: WorkspaceListPopoverProps) {
title="Workspace"
onClose={handleCreateWorkspaceModalOpen}
onSuccess={handleCreateWorkspace}
enableConflictCheck
/>
</>
);
Expand Down

0 comments on commit d208591

Please sign in to comment.