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

feat: change input of name TNS register #978

Merged
merged 5 commits into from
Feb 22, 2024
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
123 changes: 123 additions & 0 deletions packages/components/inputs/AvailableNamesInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import React from "react";
import { Control, FieldValues, Path } from "react-hook-form";
import { TextInputProps, View, ViewStyle } from "react-native";

import { TextInputCustom } from "./TextInputCustom";
import { fontSemibold14 } from "../../utils/style/fonts";
import { BrandText } from "../BrandText";

import { useNSAvailability } from "@/hooks/useNSAvailability";
import { useSelectedNetworkInfo } from "@/hooks/useSelectedNetwork";
import { NetworkKind, getCosmosNetwork } from "@/networks";
import {
neutral33,
neutral77,
primaryColor,
redDefault,
} from "@/utils/style/colors";
import { NameFinderFormType } from "@/utils/types/tns";

interface TextInputCustomProps<T extends FieldValues>
extends Omit<TextInputProps, "accessibilityRole" | "defaultValue"> {
name: Path<T>;
nameValue: string;
label: string;
placeHolder: string;
value?: string;
onChangeText?: (value: string) => void;
onPressEnter?: () => void;
control?: Control<T>;
style?: ViewStyle;
}

export const AvailableNamesInput = <T extends FieldValues>({
nameValue,
label,
name,
placeHolder,
value,
onChangeText = () => {},
onPressEnter = () => {},
control,
style,
}: TextInputCustomProps<T>) => {
const selectedNetwork = useSelectedNetworkInfo();
const cosmosNetwork = getCosmosNetwork(selectedNetwork?.id);
const nameAvailability = useNSAvailability(selectedNetwork?.id, nameValue);
const price =
nameAvailability.availability === "mint"
? nameAvailability.prettyPrice
: "";
const usdPrice =
nameAvailability.availability === "mint" ? nameAvailability?.usdPrice : 0;

let availabilityInfo = <></>;
if (nameValue && selectedNetwork?.kind === NetworkKind.Cosmos) {
if (nameAvailability.availability === "invalid") {
availabilityInfo = (
<BrandText style={{ color: redDefault, ...fontSemibold14 }}>
Invalid
</BrandText>
);
} else if (nameAvailability.availability === "mint") {
availabilityInfo = (
<View style={{ flexDirection: "row" }}>
{!!usdPrice && (
<>
<BrandText style={{ color: neutral77, ...fontSemibold14 }}>
${usdPrice?.toFixed(2)}
</BrandText>
<BrandText style={{ color: neutral33, ...fontSemibold14 }}>
{" - "}
</BrandText>
</>
)}
<BrandText style={{ color: primaryColor, ...fontSemibold14 }}>
{price}
</BrandText>
</View>
);
} else if (
nameAvailability.availability === "market" ||
nameAvailability.availability === "none"
) {
availabilityInfo = (
<BrandText style={{ color: redDefault, ...fontSemibold14 }}>
Taken
</BrandText>
);
}
}

return (
<TextInputCustom<NameFinderFormType>
noBrokenCorners
isLoading={nameAvailability.availability === "loading"}
variant="labelOutside"
label={`${label}${
nameValue
? `: ${
selectedNetwork?.kind === NetworkKind.Gno
? "gno.land/r/demo/" + name
: nameValue + cosmosNetwork?.nameServiceTLD
}`
: ""
}`}
placeHolder={placeHolder}
name={name as "name" | "associatedHandle"}
onChangeText={(val: string) => onChangeText(val)}
onPressEnter={onPressEnter}
value={value}
rules={{ required: true }}
regexp={new RegExp(/^[a-zA-Z]+$/)}
style={style}
control={
control
? (control as unknown as Control<NameFinderFormType>)
: undefined
}
>
{availabilityInfo}
</TextInputCustom>
);
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't create a specific component, you just copy what they done in CreateDAOSection.tsx but it's better to have an input component that check available names, that call the TextInputCustom etc. and called this component in CreateDAOSection and TNSNameFinderModal

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean use TextInputCustom in both file (CreateDAOSection and TNSNameFinderModal)?

and write logic in TextInputCustom ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, create a specific input components in packages/components/inputs that called TextInputCustom and write logic in it. And after that call this specific component in CreateDAOSection and TNSNameFinderModal

Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import {
neutral77,
} from "../../../utils/style/colors";
import { fontSemibold14 } from "../../../utils/style/fonts";
import { NameFinderFormType } from "../../../utils/types/tns";
import { BrandText } from "../../BrandText";
import { PrimaryButton } from "../../buttons/PrimaryButton";
import { TextInputCustom } from "../../inputs/TextInputCustom";
import ModalBase from "../ModalBase";

import { AvailableNamesInput } from "@/components/inputs/AvailableNamesInput";

const AVAILABLE_DOMAINS = [".tori"];
const COMING_SOON_DOMAINS = [".rioter"];

Expand Down Expand Up @@ -124,14 +124,14 @@ export const TNSNameFinderModal: React.FC<{
childrenBottom={<DomainsAvailability />}
width={372}
>
<TextInputCustom<NameFinderFormType>
name="name"
<AvailableNamesInput
nameValue={name}
label="NAME"
name="name"
placeHolder="Type name here"
onPressEnter={onPressEnter}
onChangeText={setName}
value={name}
regexp={new RegExp(/^[a-zA-Z]+$/)}
onPressEnter={onPressEnter}
onChangeText={(value: string) => setName(value)}
style={{ marginBottom: 20, width: "100%" }}
/>
<PrimaryButton
Expand Down
98 changes: 19 additions & 79 deletions packages/screens/Organizations/components/CreateDAOSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,14 @@ import { RadioDescriptionSelector } from "./RadioDescriptionSelector";

import { BrandText } from "@/components/BrandText";
import { PrimaryButton } from "@/components/buttons/PrimaryButton";
import { AvailableNamesInput } from "@/components/inputs/AvailableNamesInput";
import { TextInputCustom } from "@/components/inputs/TextInputCustom";
import { SpacerColumn, SpacerRow } from "@/components/spacer";
import { useNSAvailability } from "@/hooks/useNSAvailability";
import { useSelectedNetworkInfo } from "@/hooks/useSelectedNetwork";
import { getCosmosNetwork, NetworkKind } from "@/networks";
import {
neutral33,
neutral77,
primaryColor,
redDefault,
} from "@/utils/style/colors";
import {
fontSemibold14,
fontSemibold20,
fontSemibold28,
} from "@/utils/style/fonts";
import { NetworkKind } from "@/networks";
import { neutral33, neutral77 } from "@/utils/style/colors";
import { fontSemibold20, fontSemibold28 } from "@/utils/style/fonts";
import { layout } from "@/utils/style/layout";
import {
CreateDaoFormType,
Expand Down Expand Up @@ -54,66 +46,19 @@ export const CreateDAOSection: React.FC<CreateDAOSectionProps> = ({
});

const selectedNetwork = useSelectedNetworkInfo();
const cosmosNetwork = getCosmosNetwork(selectedNetwork?.id);
const selectedRadioStructure = watch("structure");
const uri = watch("imageUrl");
const name = watch("associatedHandle");

const nameAvailability = useNSAvailability(selectedNetwork?.id, name);

useEffect(() => {
setValue("nameAvailability", nameAvailability);
}, [setValue, nameAvailability]);

let availabilityInfo = <></>;
if (name && selectedNetwork?.kind === NetworkKind.Cosmos) {
switch (nameAvailability.availability) {
case "invalid": {
availabilityInfo = (
<BrandText style={{ color: redDefault, ...fontSemibold14 }}>
Invalid
</BrandText>
);
break;
}
case "mint": {
availabilityInfo = (
<View style={{ flexDirection: "row" }}>
{!!nameAvailability?.usdPrice && (
<>
<BrandText style={{ color: neutral77, ...fontSemibold14 }}>
${nameAvailability.usdPrice?.toFixed(2)}
</BrandText>
<BrandText style={{ color: neutral33, ...fontSemibold14 }}>
{" - "}
</BrandText>
</>
)}
<BrandText style={{ color: primaryColor, ...fontSemibold14 }}>
{nameAvailability.prettyPrice}
</BrandText>
</View>
);
break;
}
case "none":
case "market": {
// TODO: handle market case
availabilityInfo = (
<BrandText style={{ color: redDefault, ...fontSemibold14 }}>
Taken
</BrandText>
);
break;
}
}
}

// functions
const onErrorImageLoading = () =>
setError("imageUrl", {
type: "pattern",
message: "This image is invalid",
});
setError("imageUrl", { type: "pattern", message: "This image is invalid" });

return (
<View style={fillCStyle}>
Expand Down Expand Up @@ -142,30 +87,25 @@ export const CreateDAOSection: React.FC<CreateDAOSectionProps> = ({
</View>
<SpacerRow size={2.5} />
<View style={fillCStyle}>
<TextInputCustom<CreateDaoFormType>
noBrokenCorners
isLoading={nameAvailability.availability === "loading"}
variant="labelOutside"
<AvailableNamesInput
nameValue={name}
onChangeText={(val: string) => {
setValue("associatedHandle", val, {
shouldDirty: true,
shouldTouch: true,
shouldValidate: true,
});
}}
label="Associated Handle"
name="associatedHandle"
control={control}
label={`Associated Handle${
name
? `: ${
selectedNetwork?.kind === NetworkKind.Gno
? "gno.land/r/demo/" + name
: name + cosmosNetwork?.nameServiceTLD
}`
: ""
}`}
placeHolder={
selectedNetwork?.kind === NetworkKind.Gno
? "your_organization"
: "your-organization"
}
name="associatedHandle"
rules={{ required: true }}
>
{availabilityInfo}
</TextInputCustom>
value={name}
/>
</View>
</View>

Expand Down
1 change: 1 addition & 0 deletions packages/utils/types/tns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type TNSSendFundsFormType = {

export type NameFinderFormType = {
name: string;
associatedHandle: string;
};

export type NSAvailability =
Expand Down
Loading