Skip to content

Commit

Permalink
fix: faq card with question submission
Browse files Browse the repository at this point in the history
  • Loading branch information
in-mai-space committed Mar 24, 2024
1 parent d8ae378 commit 6c25695
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 13 deletions.
3 changes: 2 additions & 1 deletion frontend/sac-mobile/app/(auth)/welcome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const Welcome = () => {
<Button
size="lg"
variant="default"
onPress={() => router.push('/(auth)/login')}
// onPress={() => router.push('/(auth)/login')}
onPress={() => router.push('/(app)/')}
>
Get Started
</Button>
Expand Down
68 changes: 59 additions & 9 deletions frontend/sac-mobile/components/faq-card.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import React from 'react';
import { Text, TouchableOpacity, View } from 'react-native';

import React, {useState} from 'react';

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused import useState.
import {Alert, Text, TouchableOpacity, View } from 'react-native';
import { ZodError, z } from 'zod';
import Input from '@/components/input';
import { FAQ } from '@/types/item';
import { Controller, useForm } from 'react-hook-form';
import Error from '@/components/error';

type FAQData = {
question: string,
}

const FAQSchema = z.object({
question: z.string(),
});

const FAQCard = ({ faq }: { faq: FAQ }) => {
const length = () => {
Expand All @@ -12,6 +22,28 @@ const FAQCard = ({ faq }: { faq: FAQ }) => {
return faq.clubName;
}
};

const {
control,
handleSubmit,
formState: { errors },
reset
} = useForm<FAQData>();

const onSubmit = ({question}: FAQData) => {
try {
FAQSchema.parse({question});
Alert.alert('Form Submitted', JSON.stringify(question));
reset()
} catch (error) {
if (error instanceof ZodError) {
Alert.alert('Validation Error', error.errors[0].message);
} else {
console.error('An unexpected error occurred:', error);
}
}
};

return (
<TouchableOpacity className="bg-gray-200 rounded-2xl my-[2%] p-[5%]">
<View className="flex-row">
Expand All @@ -29,17 +61,35 @@ const FAQCard = ({ faq }: { faq: FAQ }) => {
</View>
</View>
<View className="mt-[7%]">
<Text className="text-sm font-bold">Questions:</Text>
<Text className="text-base font-bold">Question:</Text>
<Text>{faq.question}</Text>
<Text className="text-sm font-bold mt-[4%]">Answer:</Text>
<Text className="text-base font-bold mt-[4%]">Answer:</Text>
<Text numberOfLines={2} ellipsizeMode="tail">
{faq.answer}
</Text>
<View className="mt-[6%] mb-[1.5%]">
<Input
variant="faq"
placeholder={'Submit a question for ' + length()}
/>
<Controller
control={control}
render={({ field: { onChange, value } }) => (
<Input
variant="faq"
placeholder={'Submit a question for ' + length()}
onSubmitEditing={handleSubmit(onSubmit)}
autoCorrect={false}
onChangeText={onChange}
value={value}
/>
)}
name="question"
rules={{
required: 'Cannot submit form if empty'
}}
/>
{errors.question && (
<View className="mt-[2%]">
<Error message={errors.question.message} />
</View>
)}
</View>
</View>
</TouchableOpacity>
Expand Down
6 changes: 3 additions & 3 deletions frontend/sac-mobile/components/input.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Text, TextInput, TextInputProps, View } from 'react-native';
import { GestureResponderEvent, Text, TextInput, TextInputProps, TouchableOpacity, View } from 'react-native';

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused import TouchableOpacity.

import { VariantProps, cva } from 'class-variance-authority';

Expand Down Expand Up @@ -52,9 +52,9 @@ const Input = ({ title, error, variant, ...props }: InputProps) => {
onChangeText={props.onChangeText}
value={props.value}
secureTextEntry={props.secureTextEntry || false}
onSubmitEditing={props.onSubmitEditing}
/>
<Button size="faq" variant="faq" />
{/* @ts-ignore */}
<Button size="faq" variant="faq" onPress={(e: GestureResponderEvent) => props.onSubmitEditing!(e)} />
</View>
);
} else {
Expand Down

0 comments on commit 6c25695

Please sign in to comment.