Skip to content
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

add input validation for create survey page #107

Merged
merged 1 commit into from
Mar 23, 2023
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
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