Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
refactor: added contact details and remove email and phone number com…
Browse files Browse the repository at this point in the history
…ponents
  • Loading branch information
gorhom committed Jan 10, 2022
1 parent ed84431 commit 1a5f662
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 140 deletions.
5 changes: 4 additions & 1 deletion apps/expo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@capsizecss/core": "^3.0.0",
"@dripsy/gradient": "^3.5.3",
"@gorhom/bottom-sheet": "^4.1.4",
"@hookform/resolvers": "^2.8.5",
"@magic-sdk/react-native": "^6.2.1",
"@radix-ui/react-accordion": "^0.1.5",
"@radix-ui/react-tabs": "^0.1.1",
Expand Down Expand Up @@ -121,7 +122,9 @@
"use-unmount-signal": "^1.0.0",
"util": "~0.12.4",
"uuid": "^8.3.2",
"vm-browserify": "1.1.2"
"vm-browserify": "1.1.2",
"yup": "^0.32.11",
"yup-phone": "^1.3.2"
},
"devDependencies": {
"@babel/core": "^7.16.0",
Expand Down
37 changes: 23 additions & 14 deletions packages/app/components/login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,16 @@ import { mixpanel } from "app/lib/mixpanel";
import { View, Text, Button, ButtonLabel, Pressable } from "design-system";
import { useRouter } from "app/navigation/use-router";
import { setRefreshToken } from "app/lib/refresh-token";
import {
LoginPhoneNumberField,
LoginPhoneNumberFieldValues,
} from "./login-phone-number-field";
import { LoginEmailField, LoginEmailFieldValues } from "./login-email-field";
import { LoginContactDetailsField } from "./login-contact-details-field";

const magic = new Magic(process.env.NEXT_PUBLIC_MAGIC_PUB_KEY);

// TODO: loading state
export function Login() {
const router = useRouter();
const context = useContext(AppContext);
const connector = useWalletConnect();
const [loading, setLoading] = useState(false);
const [loading, setLoading] = useState(true);
const [signaturePending, setSignaturePending] = useState(false);
const [walletName, setWalletName] = useState("");
const { mutate } = useSWRConfig();
Expand All @@ -45,6 +43,11 @@ export function Login() {
const web3 = new Web3Provider(magic.rpcProvider);
context.setWeb3(web3);

console.log({
url: "/v1/login_magic",
method: "POST",
data: payload,
});
const response = await axios({
url: "/v1/login_magic",
method: "POST",
Expand All @@ -55,6 +58,8 @@ export function Login() {
const refreshToken = response?.refresh;
const validResponse = accessToken && refreshToken;

console.log(response);

if (validResponse) {
// TODO:
// const sealedRefreshToken = await Iron.seal(
Expand Down Expand Up @@ -86,6 +91,8 @@ export function Login() {
}, []);

const handleLoginError = useCallback((error) => {
setLoading(false);

if (process.env.NODE_ENV === "development") {
console.error(error);
}
Expand All @@ -101,11 +108,11 @@ export function Login() {

//#region callbacks
const handleSubmitEmail = useCallback(
async ({ email }: LoginEmailFieldValues) => {
async (email: string) => {
try {
setLoading(true);
mixpanel.track("Login - email button click");

const magic = new Magic(process.env.NEXT_PUBLIC_MAGIC_PUB_KEY);
const did = await magic.auth.loginWithMagicLink({ email });

await handleLogin({
Expand All @@ -122,11 +129,10 @@ export function Login() {
[handleLogin, handleLoginError]
);
const handleSubmitPhoneNumber = useCallback(
async ({ phoneNumber }: LoginPhoneNumberFieldValues) => {
async (phoneNumber: string) => {
try {
mixpanel.track("Login - phone number button click");

const magic = new Magic(process.env.NEXT_PUBLIC_MAGIC_PUB_KEY);
const did = await magic.auth.loginWithSMS({
phoneNumber,
});
Expand Down Expand Up @@ -318,15 +324,18 @@ export function Login() {
</Text>
</View>

<LoginEmailField onSubmit={handleSubmitEmail} />

<LoginContactDetailsField
onSubmitEmail={handleSubmitEmail}
onSubmitPhoneNumber={handleSubmitPhoneNumber}
/>
{/*
<View tw="mb-[16px] mx-[-16px] bg-gray-100 dark:bg-gray-900">
<Text tw="my-[8px] font-bold text-sm text-gray-600 dark:text-gray-400 text-center">
— or —
</Text>
</View>
</View> */}

<LoginPhoneNumberField onSubmit={handleSubmitPhoneNumber} />
{/* <LoginPhoneNumberField onSubmit={handleSubmitPhoneNumber} /> */}
</View>
)}

Expand Down
87 changes: 87 additions & 0 deletions packages/app/components/login/login-contact-details-field.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import React, { useCallback } from "react";
import { Button, ButtonLabel, Text, TextInput, View } from "design-system";
import { useForm, Controller } from "react-hook-form";
import { yupResolver } from "@hookform/resolvers/yup";
import { yup } from "app/lib/yup";

const validationSchema = yup
.object({
// @ts-ignore
contact: yup.string().or([yup.string().email(), yup.string().phone()]),
})
.required();

type FormData = {
contact?: string;
};

interface LoginEmailFieldProps {
onSubmitEmail: (value: string) => void;
onSubmitPhoneNumber: (value: string) => void;
}

export function LoginContactDetailsField({
onSubmitEmail,
onSubmitPhoneNumber,
}: LoginEmailFieldProps) {
const {
control,
handleSubmit,
formState: { errors },
} = useForm<FormData>({
resolver: yupResolver(validationSchema),
mode: "onBlur",
reValidateMode: "onChange",
});

const handleSubmitContact = useCallback(
({ contact }: FormData) => {
console.log("contact", contact);
if (contact.includes("@")) {
onSubmitEmail(contact);
} else {
onSubmitPhoneNumber(contact);
}
},
[onSubmitEmail, onSubmitPhoneNumber]
);
return (
<>
<View tw="p-[16px] mb-[16px] rounded-[16px] bg-gray-100 dark:bg-gray-900">
<Text tw="mb-[8px] font-bold text-sm text-gray-900 dark:text-white">
Contact details
</Text>
<Controller
control={control}
render={({ field: { onChange, onBlur, value } }) => (
<TextInput
tw="w-full text-black dark:text-gray-300 rounded-lg focus:outline-none focus-visible:ring-1"
onBlur={onBlur}
onChangeText={(value) => onChange(value)}
value={value}
placeholder="Enter your email or phone number"
autoCapitalize="none"
autoCorrect={false}
returnKeyType="go"
/>
)}
name="contact"
/>
{errors.contact && (
<Text tw="text-xs text-red-500 font-semibold mt-[8px]">
Please enter a valid email address or phone number.
</Text>
)}
</View>

<Button
onPress={handleSubmit(handleSubmitContact)}
variant="tertiary"
size="regular"
tw="mb-[16px]"
>
<ButtonLabel tw="text-black dark:text-white">Sign in</ButtonLabel>
</Button>
</>
);
}
60 changes: 0 additions & 60 deletions packages/app/components/login/login-email-field.tsx

This file was deleted.

63 changes: 0 additions & 63 deletions packages/app/components/login/login-phone-number-field.tsx

This file was deleted.

20 changes: 20 additions & 0 deletions packages/app/lib/yup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as yup from "yup";
import "yup-phone";

yup.addMethod(yup.string, "or", function (schemas, msg) {
return this.test({
name: "or",
message: "Please enter valid value" || msg,
test: (value) => {
if (Array.isArray(schemas) && schemas.length > 1) {
const resee = schemas.map((schema) => schema.isValidSync(value));
return resee.some((res) => res);
} else {
throw new TypeError("Schemas is not correct array schema");
}
},
exclusive: false,
});
});

export { yup };
Loading

0 comments on commit 1a5f662

Please sign in to comment.