Skip to content
This repository was archived by the owner on Jan 30, 2024. It is now read-only.

Commit 626643b

Browse files
authored
Questionnaire details (#181)
* Changing email label and send button logic * Validate on blur * Prettier * Barteks review * Prettier:
1 parent a90b7be commit 626643b

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

assets/src/features/leaving-page/components/Questionnaire.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,17 @@ const Questionnaire: FC<QuestionnaireProps> = ({ onSubmitClick }) => {
3131
watch,
3232
handleSubmit,
3333
formState: { errors },
34-
} = useForm<Inputs>();
34+
} = useForm<Inputs>({ mode: "onTouched" });
3535
const onSubmit: SubmitHandler<Inputs> = (data) => {
3636
sendForm(data);
3737
onSubmitClick();
3838
};
3939

40-
const emailInput = watch("email");
41-
const emailFilled = !!emailInput && !errors.email;
4240
const emailPattern =
4341
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
4442

4543
const ratingNames = ["video", "audio", "screensharing"] as const;
4644
const isOneQualityRated = ratingNames.map(watch).some(isNotNil);
47-
const canSubmit = emailFilled && isOneQualityRated;
4845

4946
return (
5047
<form
@@ -90,22 +87,24 @@ const Questionnaire: FC<QuestionnaireProps> = ({ onSubmitClick }) => {
9087
},
9188
required: "Email is required",
9289
}}
93-
render={({ field: { value, onChange }, fieldState: { error } }) => (
90+
render={({ field: { value, onChange, onBlur }, fieldState: { error } }) => (
9491
<Input
9592
type="text"
96-
label="Your e-mail"
93+
label="Your e-mail (required)"
9794
name="email"
9895
placeholder="Your e-mail"
9996
value={value ?? ""}
10097
onChange={(v) => onChange(v)}
98+
onBlur={() => {
99+
onBlur();
100+
}}
101101
error={!!error}
102102
additionalText={error?.message}
103-
required
104103
/>
105104
)}
106105
/>
107106
</div>
108-
<SubmitButton isSmartphone={!!isSmartphone} disabled={!canSubmit} />
107+
<SubmitButton isSmartphone={!!isSmartphone} disabled={!isOneQualityRated} />
109108
</div>
110109
</form>
111110
);

assets/src/features/leaving-page/components/Rating.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type StarButtonProps = {
1414
type RatingProps<V extends FieldValues> = {
1515
name: Path<V>;
1616
value: number | null;
17-
onChange: (newValue: number) => void;
17+
onChange: (newValue: number | null) => void;
1818
};
1919

2020
const StarOption = ({ isActive, isHoverActive, onClick, onHover, onLeave }: StarButtonProps) => {
@@ -38,7 +38,7 @@ const StarOption = ({ isActive, isHoverActive, onClick, onHover, onLeave }: Star
3838
};
3939

4040
const Rating = <V extends FieldValues>({ name, value, onChange }: RatingProps<V>) => {
41-
const [rateValue, setRateValue] = useState<number>(value ?? 0);
41+
const [rateValue, setRateValue] = useState<number | null>(value);
4242
const [hoverRateValue, setHoverRateValue] = useState<number | null>(null);
4343

4444
const ratingsFromOneToFive = Array.from({ length: 5 }, (_, i) => i + 1);
@@ -47,10 +47,10 @@ const Rating = <V extends FieldValues>({ name, value, onChange }: RatingProps<V>
4747
const title = `${capitalize(name)} Quality`;
4848

4949
const handleOnClick = (newValue: number) => {
50-
const newValueOrZero = newValue === rateValue ? 0 : newValue;
50+
const newValueOrNull = newValue === rateValue ? null : newValue;
5151

52-
onChange(newValueOrZero);
53-
setRateValue(newValueOrZero);
52+
onChange(newValueOrNull);
53+
setRateValue(newValueOrNull);
5454
setHoverRateValue(null);
5555
};
5656

@@ -61,7 +61,7 @@ const Rating = <V extends FieldValues>({ name, value, onChange }: RatingProps<V>
6161
{ratingsFromOneToFive.map((rateIndex) => (
6262
<StarOption
6363
key={rateIndex}
64-
isActive={hoverRateValue === null && rateIndex <= rateValue}
64+
isActive={hoverRateValue === null && rateValue !== null && rateIndex <= rateValue}
6565
isHoverActive={hoverRateValue !== null && rateIndex <= hoverRateValue}
6666
onClick={() => {
6767
handleOnClick(rateIndex);

assets/src/features/shared/components/Input.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type TextInputProps = BaseInputProps & {
2929
value?: string;
3030
required?: boolean;
3131
onChange?: (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => void;
32+
onBlur?: (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => void;
3233
};
3334

3435
type InputProps = SelectInputProps | TextInputProps;
@@ -37,6 +38,7 @@ const Input: React.FC<InputProps> = (props) => {
3738
const {
3839
value,
3940
onChange,
41+
onBlur,
4042
type,
4143
placeholder,
4244
label,
@@ -69,6 +71,7 @@ const Input: React.FC<InputProps> = (props) => {
6971
<Select
7072
value={value}
7173
onChange={onChange}
74+
onBlur={onBlur}
7275
options={props.options}
7376
isDisabled={disabled}
7477
controlClassName={inputClassName}
@@ -83,6 +86,7 @@ const Input: React.FC<InputProps> = (props) => {
8386
id={name}
8487
value={value}
8588
onChange={onChange}
89+
onBlur={onBlur}
8690
className={inputClassName}
8791
name={name}
8892
type={type}

0 commit comments

Comments
 (0)