"Adding Pix Key input validation"
This commit is contained in:
parent
c5b16559ff
commit
8d3a8ae47a
@ -3,6 +3,7 @@ import { ref } from "vue";
|
|||||||
import CustomButton from "../CustomButton/CustomButton.vue";
|
import CustomButton from "../CustomButton/CustomButton.vue";
|
||||||
import { debounce } from "@/utils/debounce";
|
import { debounce } from "@/utils/debounce";
|
||||||
import { decimalCount } from "@/utils/decimalCount";
|
import { decimalCount } from "@/utils/decimalCount";
|
||||||
|
import { pixFormatValidation, postProcessKey } from "@/utils/pixKeyFormat";
|
||||||
import { useEtherStore } from "@/store/ether";
|
import { useEtherStore } from "@/store/ether";
|
||||||
import { storeToRefs } from "pinia";
|
import { storeToRefs } from "pinia";
|
||||||
import { connectProvider } from "@/blockchain/provider";
|
import { connectProvider } from "@/blockchain/provider";
|
||||||
@ -17,6 +18,7 @@ const pixKey = ref<string>("");
|
|||||||
const enableSelectButton = ref<boolean>(false);
|
const enableSelectButton = ref<boolean>(false);
|
||||||
const hasLiquidity = ref<boolean>(true);
|
const hasLiquidity = ref<boolean>(true);
|
||||||
const validDecimals = ref<boolean>(true);
|
const validDecimals = ref<boolean>(true);
|
||||||
|
const validPixFormat = ref<boolean>(true);
|
||||||
|
|
||||||
// Emits
|
// Emits
|
||||||
const emit = defineEmits(["approveTokens"]);
|
const emit = defineEmits(["approveTokens"]);
|
||||||
@ -35,10 +37,26 @@ const handleInputEvent = (event: any): void => {
|
|||||||
validDecimals.value = true;
|
validDecimals.value = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handlePixKeyInputEvent = (event: any): void => {
|
||||||
|
const { value } = event.target;
|
||||||
|
|
||||||
|
pixKey.value = value;
|
||||||
|
|
||||||
|
if (pixFormatValidation(pixKey.value)) {
|
||||||
|
validPixFormat.value = true;
|
||||||
|
enableSelectButton.value = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
enableSelectButton.value = false;
|
||||||
|
validPixFormat.value = false;
|
||||||
|
};
|
||||||
|
|
||||||
const handleButtonClick = async (
|
const handleButtonClick = async (
|
||||||
offer: string,
|
offer: string,
|
||||||
pixKey: string
|
pixKey: string
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
|
console.log(postProcessKey(pixKey));
|
||||||
if (walletAddress.value) emit("approveTokens", { offer, pixKey });
|
if (walletAddress.value) emit("approveTokens", { offer, pixKey });
|
||||||
else await connectProvider();
|
else await connectProvider();
|
||||||
};
|
};
|
||||||
@ -96,15 +114,22 @@ const handleButtonClick = async (
|
|||||||
>
|
>
|
||||||
<div class="flex justify-between w-full items-center">
|
<div class="flex justify-between w-full items-center">
|
||||||
<input
|
<input
|
||||||
|
@input="debounce(handlePixKeyInputEvent, 500)($event)"
|
||||||
type="text"
|
type="text"
|
||||||
v-model="pixKey"
|
v-model="pixKey"
|
||||||
class="border-none outline-none text-lg text-gray-900 w-fit"
|
class="border-none outline-none text-lg text-gray-900 w-fit"
|
||||||
placeholder="Digite a chave Pix"
|
placeholder="Digite a chave Pix"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="flex pt-2 justify-center" v-if="!validPixFormat">
|
||||||
|
<span class="text-red-500 font-normal text-sm"
|
||||||
|
>Por favor utilize telefone, CPF ou CNPJ</span
|
||||||
|
>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<CustomButton
|
<CustomButton
|
||||||
:text="walletAddress ? 'Aprovar tokens' : 'Conectar Carteira'"
|
:text="walletAddress ? 'Aprovar tokens' : 'Conectar Carteira'"
|
||||||
|
:isDisabled="!validDecimals || !validPixFormat"
|
||||||
@buttonClicked="handleButtonClick(offer, pixKey)"
|
@buttonClicked="handleButtonClick(offer, pixKey)"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
16
src/utils/pixKeyFormat.ts
Normal file
16
src/utils/pixKeyFormat.ts
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
export const pixFormatValidation = (pixKey: string): boolean => {
|
||||||
|
const cpf = /(^\d{3}\.?\d{3}\.?\d{3}-?\d{2}$)/g;
|
||||||
|
const cnpj = /(^\d{2}\.?\d{3}\.?\d{3}\/?\d{4}-?\d{2}$)/g;
|
||||||
|
const telefone = /(^[0-9]{2})?(\s|-)?(9?[0-9]{4})-?([0-9]{4}$)/g;
|
||||||
|
|
||||||
|
if (pixKey.match(cpf) || pixKey.match(cnpj) || pixKey.match(telefone)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const postProcessKey = (pixKey: string): string => {
|
||||||
|
pixKey = pixKey.replace(/[-.()/]/g, "");
|
||||||
|
return pixKey;
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user