Skip to content

Commit

Permalink
fix: visible error on trying send empty comment
Browse files Browse the repository at this point in the history
  • Loading branch information
nyet-ty committed Jul 4, 2024
1 parent 4fde81d commit 1213d12
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"Comment": "Comment",
"Update state": "Update state"
"Update state": "Update state",
"Comments's description is required": ""
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"Comment": "Написать",
"Update state": "Обновить статус"
"Update state": "Обновить статус",
"Comments's description is required": "Текст комментария обязателен"
}
8 changes: 8 additions & 0 deletions src/components/CommentCreateForm/CommentCreateForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const CommentCreateForm: React.FC<CommentCreateFormProps> = ({
const descriptionRef = useLatest(description);
const [focused, setFocused] = useState(Boolean(currentDescription));
const [busy, setBusy] = useState(false);
const [error, setError] = useState<{ message: string } | undefined>();

const onCommentFocus = useCallback(() => {
setFocused(true);
Expand All @@ -59,13 +60,19 @@ const CommentCreateForm: React.FC<CommentCreateFormProps> = ({
const onCommentChange = useCallback(
({ description }: { description: string }) => {
setDescription(description);
setError(undefined);
onChange?.({ description, stateId: pushState?.id });
},
[onChange, pushState?.id],
);

const onCommentSubmit = useCallback(
async (form: CommentSchema & { stateId?: string }) => {
if (!form.description.length) {
setError({ message: tr("Comments's description is required") });
return;
}

setBusy(true);
setFocused(false);

Expand Down Expand Up @@ -131,6 +138,7 @@ const CommentCreateForm: React.FC<CommentCreateFormProps> = ({
onSubmit={onCommentFormSubmit('pushState')}
onCancel={onCancelCreate}
onFocus={onCommentFocus}
passedError={error}
actionButton={
<>
<Button view="primary" disabled={busy} onClick={onCommentСlick} text={tr('Comment')} />
Expand Down
4 changes: 3 additions & 1 deletion src/components/CommentForm/CommentForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface CommentFormProps {
autoFocus?: boolean;
busy?: boolean;
description?: string;
passedError?: { message: string };

onSubmit: (form: CommentSchema) => void | Promise<void>;
onChange?: (form: CommentSchema) => void;
Expand All @@ -35,6 +36,7 @@ export const CommentForm: React.FC<CommentFormProps> = ({
onSubmit,
onFocus,
onCancel,
passedError,
}) => {
const [error, setError] = useState<{ message: string } | undefined>();
const ref = useRef(null);
Expand Down Expand Up @@ -86,7 +88,7 @@ export const CommentForm: React.FC<CommentFormProps> = ({
outline
{...commentFormDescription.attr}
/>
{nullable(error, (err) => (
{nullable(error || passedError, (err) => (
<FormControlError error={err} />
))}
</FormControl>
Expand Down

0 comments on commit 1213d12

Please sign in to comment.