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

Solve buy button functionality, card number formatting, and success - error messages #20

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 10 additions & 2 deletions src/app/(app)/payment.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { router } from 'expo-router';
import { useRef } from 'react';
import { Platform } from 'react-native';
import { showMessage } from 'react-native-flash-message';
import { KeyboardAvoidingView } from 'react-native-keyboard-controller';

Expand Down Expand Up @@ -60,7 +61,14 @@ export default function Payment() {
},
{
onSuccess: () => {
showMessage({ message: 'success', type: 'success' });
showMessage({
message: 'success',
type: 'success',
floating: true,
style: {
marginTop: Platform.OS === 'ios' ? 50 : 0,
},
});
router.navigate('/(app)');
},
onError: (error) => {
Expand All @@ -80,7 +88,7 @@ export default function Payment() {
<BuyForm ref={buyFormRef} onSubmitHandler={onSubmitHandler} />
<FormButtons
handleSubmit={() => {
buyFormRef.current?.submit;
buyFormRef.current?.submit();
}}
/>
</ScrollView>
Expand Down
8 changes: 4 additions & 4 deletions src/components/payment/buy-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type SectionFormProps = {
titleClassname?: string;
inputClassname?: string;
textClassname?: string;
isCreditCard?: boolean;
};

export const _addressSchema = z.object({
Expand Down Expand Up @@ -62,10 +63,7 @@ export const additionalAddressSchema = z.object({
}),
});

const schema = _addressSchema
.and(_cardSchema)
.and(dateSchema)
.and(additionalAddressSchema);
const schema = _addressSchema.and(_cardSchema).and(dateSchema);

export type TBuyFormFields = z.infer<typeof schema>;

Expand Down Expand Up @@ -144,12 +142,14 @@ const cardFields = [
label: purchaseForm.labels.cardNumber,
required: true,
placeholder: purchaseForm.placeholders.cardNumber,
isCardNumber: true,
},
{
name: 'cvcCode',
label: purchaseForm.placeholders.cvcCode,
required: true,
placeholder: purchaseForm.placeholders.cvcCode,
isCVC: true,
},
];

Expand Down
34 changes: 20 additions & 14 deletions src/core/hoc/multiple-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ type FieldProps = {
label: string;
required?: boolean;
placeholder?: string;
isCardNumber?: boolean;
isCVC?: boolean;
};

type FromInputsProps = {
Expand All @@ -30,20 +32,24 @@ export const FormInputs = ({
return (
<View>
{title && <Text className={titleClassname}>{title}</Text>}
{fields.map(({ label, name, placeholder, required }) => (
<View key={name} className="">
<Text className={textClassname}>
{label} {required ? symbols.required : ' '}
</Text>
<ControlledInput
className={inputClassname}
control={control}
name={name}
placeholder={placeholder}
editable={editable}
/>
</View>
))}
{fields.map(
({ label, name, placeholder, required, isCardNumber, isCVC }) => (
<View key={name} className="">
<Text className={textClassname}>
{label} {required ? symbols.required : ' '}
</Text>
<ControlledInput
className={inputClassname}
control={control}
name={name}
placeholder={placeholder}
editable={editable}
isCardNumber={isCardNumber}
isCVC={isCVC}
/>
</View>
)
)}
</View>
);
};
32 changes: 30 additions & 2 deletions src/ui/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export interface NInputProps extends TextInputProps {
error?: string;
labelClassname?: string;
showError?: boolean;
isCardNumber?: boolean;
isCVC?: boolean;
}

interface ShowPasswordIconProps {
Expand Down Expand Up @@ -200,14 +202,40 @@ export function ControlledInput<T extends FieldValues>(
props: ControlledInputProps<T>
) {
const { name, control, rules, ...inputProps } = props;

const { field, fieldState } = useController({ control, name, rules });

const formatCardNumber = (text: string) => {
const cleaned = text.replace(/\D/g, '');

const truncated = cleaned.slice(0, 16);

return truncated.replace(/(\d{4})(?=\d)/g, '$1 ');
};

const handleChangeText = (text: string) => {
const formattedText = props.isCardNumber ? formatCardNumber(text) : text;

field.onChange(formattedText);
};

const getCharacterLimit = () => {
if (props.isCardNumber) {
return 19;
}
if (props.isCVC) {
return 3;
}
return undefined;
};

return (
<Input
ref={field.ref}
autoCapitalize="none"
onChangeText={field.onChange}
onChangeText={handleChangeText}
value={(field.value as string) || ''}
keyboardType={props.isCardNumber || props.isCVC ? 'numeric' : 'default'}
maxLength={getCharacterLimit()}
{...inputProps}
error={fieldState.error?.message}
/>
Expand Down
6 changes: 5 additions & 1 deletion src/ui/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const HEIGHT = height;

// for onError react queries and mutations
export const showError = (error: AxiosError) => {
console.log(JSON.stringify(error?.response?.data));
// console.log(JSON.stringify(error?.response?.data));
const description = extractError(error?.response?.data).trimEnd();

showMessage({
Expand All @@ -27,6 +27,10 @@ export const showErrorMessage = (message: string = 'Something went wrong ') => {
message,
type: 'danger',
duration: 4000,
floating: true,
style: {
marginTop: Platform.OS === 'ios' ? 50 : 0,
},
});
};

Expand Down
Loading