Skip to content

Commit

Permalink
Feature/Add input validation for create survey page (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-lukasiewicz authored Mar 23, 2023
1 parent eee9bc8 commit efc0634
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/features/surveys/managers/createSurveyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import useCopyToClipboard from 'shared/hooks/useCopyToClipboard';
export const useCreateSurveyManager = () => {
const [title, setTitle] = useState('');
const [pack, setPack] = useState<string[]>([]);
const [error, setError] = useState('');

const { user } = useApplicationContext();
const [buttonDisable, setButtonDisable] = useState(false);
const [isCreating, setIsCreating] = useState(false);

const router = useRouter();
Expand Down Expand Up @@ -47,7 +47,8 @@ export const useCreateSurveyManager = () => {
}, [startDate, endDate]);

const createSurvey = async () => {
setButtonDisable(true);
if (!title) return setError('Required field');

setIsCreating(true);

try {
Expand All @@ -72,7 +73,6 @@ export const useCreateSurveyManager = () => {
toast.error('Survey creation failed');
}
setIsCreating(false);
setButtonDisable(false);
};

const filterPassedTime = (time: string | number | Date) => {
Expand All @@ -90,6 +90,7 @@ export const useCreateSurveyManager = () => {
};
return {
title,
error,
pack,
handleChangeTitle,
startDate,
Expand All @@ -100,7 +101,6 @@ export const useCreateSurveyManager = () => {
filterPassedSelectedTime,
handleEmotePick,
createSurvey,
buttonDisable,
isCreating,
};
};
4 changes: 2 additions & 2 deletions src/pages/survey/create/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function SurveyCreatePage() {
const {
title,
pack,
error,
handleChangeTitle,
startDate,
setStartDate,
Expand All @@ -21,7 +22,6 @@ function SurveyCreatePage() {
filterPassedSelectedTime,
handleEmotePick,
createSurvey,
buttonDisable,
isCreating,
} = useCreateSurveyManager();

Expand All @@ -39,6 +39,7 @@ function SurveyCreatePage() {
label="Survey title"
placeholder="Title..."
value={title}
error={!title ? error : undefined}
onChange={handleChangeTitle}
/>
<div className="mt-10 mb-4 block text-left font-semibold">
Expand Down Expand Up @@ -106,7 +107,6 @@ function SurveyCreatePage() {
<Button
onClick={createSurvey}
className="z-0 mt-12 w-full sm:w-auto"
disabled={!title.length || buttonDisable}
variant={ButtonVariant.PRIMARY}
isLoading={isCreating}
>
Expand Down
2 changes: 1 addition & 1 deletion src/shared/components/Input/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function Input({
)}
/>
{!!error && (
<p className="mb-2 max-w-sm text-right text-sm text-red-400">{error}</p>
<p className="mb-2 text-right text-sm text-red-400">{error}</p>
)}
</>
);
Expand Down

0 comments on commit efc0634

Please sign in to comment.