Skip to content

Commit

Permalink
migrating utils to ts
Browse files Browse the repository at this point in the history
  • Loading branch information
tahmid-saj committed Sep 21, 2024
1 parent a8a9a57 commit 867b54c
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 18 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { CurrencyConversionInput, ExchangeRateInput, MortgageInput } from "../../contexts/shared/useful-tools/useful-tools.types";
import { errorOnMortgageResult, errorOnCurrencyResult } from "../errors/useful-tools.errors";

// mortgage calculator
export async function getMortgageResult(mortgageInput) {
export async function getMortgageResult(mortgageInput: MortgageInput): Promise<any> {
try {

const response = await fetch(`${process.env.REACT_APP_API_URL_USEFUL_TOOLS}${process.env.REACT_APP_API_URL_USEFUL_TOOLS_MORTGAGE_CALCULATOR}`, {
Expand All @@ -22,7 +23,7 @@ export async function getMortgageResult(mortgageInput) {

// currency converter
// exchange rate
export async function getExchangeRate(currencyInput) {
export async function getExchangeRate(currencyInput: ExchangeRateInput): Promise<any> {
try {

const response = await fetch(`${process.env.REACT_APP_API_URL_USEFUL_TOOLS}${process.env.REACT_APP_API_URL_USEFUL_TOOLS_EXCHANGE_RATE}`, {
Expand Down
File renamed without changes.
6 changes: 0 additions & 6 deletions src/utils/constants/useful-tools.constants.js

This file was deleted.

6 changes: 6 additions & 0 deletions src/utils/constants/useful-tools.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// useful tools constants

export enum DOWNPAYMENT_FLAG_OPTIONS {
yes = "Yes",
no = "No"
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// user errors

export const errorOnCreatingUser = (error) => {
export const errorOnCreatingUser = (error: Error) => {
alert(`Error creating the user ${error.message}`)
};

export const errorOnUserSignIn = (error) => {
export const errorOnUserSignIn = (error: Error) => {
alert(`Error signing in ${error.message}`);
};

export const errorOnEmailAlreadyInUse = () => {
alert(`Email already in use`);
};

export const errorOnUserCreation = (error) => {
export const errorOnUserCreation = (error: Error) => {
alert(`User creation ${error.message}`);
};
2 changes: 1 addition & 1 deletion src/utils/firebase/firebase.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export const createUserDocumentFromAuth = async (
...additionalInformation,
});
} catch (error) {
errorOnCreatingUser(error);
errorOnCreatingUser(error as Error);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { errorOnInvalidMortgageInput,

import { DOWNPAYMENT_FLAG_OPTIONS } from "../constants/useful-tools.constants";
import { REGEX_PATTERNS } from "../constants/regex.constants";
import { CurrencyConversionInput, ExchangeRateInput, MortgageInput } from "../../contexts/shared/useful-tools/useful-tools.types";

// useful tools validation functions

// mortgage calculator
export const validateMortgageInput = (mortgageInput) => {
export const validateMortgageInput = (mortgageInput: MortgageInput): boolean => {
if (mortgageInput.downpaymentFlag === DOWNPAYMENT_FLAG_OPTIONS.no) {
if (!(REGEX_PATTERNS.floatNumbers.test(String(mortgageInput.loanAmount))) || Number(mortgageInput.loanAmount) <= 0) {
errorOnInvalidMortgageInput();
Expand All @@ -28,9 +29,9 @@ export const validateMortgageInput = (mortgageInput) => {
return true;
}

if ((mortgageInput.monthlyHoa !== "" && (!(REGEX_PATTERNS.floatNumbers.test(String(mortgageInput.monthlyHoa))) || Number(mortgageInput.monthlyHoa) <= 0)) ||
(mortgageInput.annualPropertyTax !== "" && (!(REGEX_PATTERNS.floatNumbers.test(String(mortgageInput.annualPropertyTax))) || Number(mortgageInput.annualPropertyTax) <= 0)) ||
(mortgageInput.annualHomeInsurance !== "" && (!(REGEX_PATTERNS.floatNumbers.test(String(mortgageInput.annualHomeInsurance))) || Number(mortgageInput.annualHomeInsurance) <= 0))) {
if ((mortgageInput.monthlyHoa && (!(REGEX_PATTERNS.floatNumbers.test(String(mortgageInput.monthlyHoa))) || Number(mortgageInput.monthlyHoa) <= 0)) ||
(mortgageInput.annualPropertyTax && (!(REGEX_PATTERNS.floatNumbers.test(String(mortgageInput.annualPropertyTax))) || Number(mortgageInput.annualPropertyTax) <= 0)) ||
(mortgageInput.annualHomeInsurance && (!(REGEX_PATTERNS.floatNumbers.test(String(mortgageInput.annualHomeInsurance))) || Number(mortgageInput.annualHomeInsurance) <= 0))) {
errorOnInvalidMortgageInput();
return true;
}
Expand All @@ -39,7 +40,7 @@ export const validateMortgageInput = (mortgageInput) => {
}

// currency converter
export const validateCurrencyConverterInput = (currencyInput) => {
export const validateCurrencyConverterInput = (currencyInput: CurrencyConversionInput): boolean => {
if (!(REGEX_PATTERNS.floatNumbers.test(String(currencyInput.fromCurrencyAmount))) || Number(currencyInput.fromCurrencyAmount) <= 0) {
errorOnInvalidCurrencyConverterInput()
return true
Expand All @@ -55,7 +56,7 @@ export const validateCurrencyConverterInput = (currencyInput) => {
}

// exchange rate
export const validateExchangeRateInput = (currencyInput) => {
export const validateExchangeRateInput = (currencyInput: ExchangeRateInput): boolean => {
// strings
if (!(REGEX_PATTERNS.currency.test(String(currencyInput.fromCurrency))) || !(REGEX_PATTERNS.currency.test(String(currencyInput.toCurrency)))) {
errorOnInvalidExchangeRateInput();
Expand Down

0 comments on commit 867b54c

Please sign in to comment.