Skip to content

Commit

Permalink
Fix: fix the fake api
Browse files Browse the repository at this point in the history
  • Loading branch information
Ngha-Boris committed Apr 18, 2024
1 parent f2dcc0e commit 927fd24
Showing 1 changed file with 48 additions and 46 deletions.
94 changes: 48 additions & 46 deletions power-pay-frontend/src/components/fake_api.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,63 @@
import axios, { AxiosError, AxiosResponse } from 'axios';
import React, { useState } from 'react';
import axios from 'axios';

export interface ApiResponse {
phone: string;
amount: string;
}

export interface ErrorResponse {
message: string;
}

const FakeApi = () => {
const FakeApi: React.FC = () => {
const baseURL = 'http://localhost:5000';
const api = axios.create({
baseURL,
const api = axios.create({
baseURL,
timeout: 5000,
});

const [recipientPhoneNumber, setRecipientPhoneNumber] = useState('');
const [amount, setAmount] = useState('');
const [errorMessage, setErrorMessage] = useState('');

const mockSend_MoneyAPI = async (
recipientPhoneNumber: string,
recipientPhoneNumber: string,
amount: number
): Promise<ApiResponse | undefined> => {
) => {
try {
if (!recipientPhoneNumber || !amount) {
throw new Error("Please fill in all fields");
}

const response: AxiosResponse<ApiResponse> = await api.post('/UserInfo', {
recipientPhoneNumber,
amount,
});

console.log("Transaction Successful");
console.log("Recipient Phone Number:", recipientPhoneNumber);
console.log("Amount:", amount);

return response.data;
} catch (error) {
if (axios.isAxiosError(error)) {
const axiosError = error as AxiosError<ErrorResponse>;
if (axiosError.response) {
// Server returned an error response
console.log(axiosError.response.data.message || "An error occurred while processing the transaction.");
} else if (axiosError.request) {
// No response received, possibly due to network issues
console.log("Network error. Please check your internet connection.");
} else {
// Something else went wrong
console.log("An error occurred while processing the transaction.");
}
setErrorMessage("Please fill in all fields");
} else {
// Non-Axios errors
console.log("An error occurred while processing the transaction.");
console.log("Transaction Successful");
console.log("Recipient Phone Number:", recipientPhoneNumber);
console.log("Amount:", amount);
setErrorMessage('');
await api.post('/UserInfo', {
recipientPhoneNumber,
amount
});
}
} catch (error) {
setErrorMessage("An error occurred while processing the transaction.");
console.error("Error:", error);
}
};

return mockSend_MoneyAPI; // Return the function directly
}

const handleSendMoney = () => {
mockSend_MoneyAPI(recipientPhoneNumber, parseInt(amount));
}

return (
<div>
<p>FAKED API</p>
<input
type="text"
value={recipientPhoneNumber}
onChange={(e) => setRecipientPhoneNumber(e.target.value)}
placeholder="Recipient Phone Number"
/>
<input
type="text"
value={amount}
onChange={(e) => setAmount(e.target.value)}
placeholder="Amount"
/>
<button onClick={handleSendMoney}>Send Money</button>
{errorMessage && <p>{errorMessage}</p>}
</div>
);
};

export default FakeApi;

0 comments on commit 927fd24

Please sign in to comment.