Skip to content

Commit

Permalink
Merge pull request #6 from amosmachora/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Amos Machora authored Dec 26, 2023
2 parents b3b16ea + 6a136e0 commit 5d9fa75
Show file tree
Hide file tree
Showing 15 changed files with 923 additions and 581 deletions.
5 changes: 5 additions & 0 deletions .changeset/spicy-waves-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-daraja": minor
---

removed frontend react hooks
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
node_modules/
dist
38 changes: 32 additions & 6 deletions components/QRCodeDisplay.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
import { faCircleNotch } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import React, { useEffect, useState } from "react";
import { useReactDaraja } from "../hooks/useReactDaraja";
import { useScannableQrCode } from "../hooks/useScannableQrCode";
import { ScannableQrParams } from "../types";
import { ScannableQrCodeResponse, ScannableQrParams } from "../types";
import axios from "axios";
import { BASE_URL } from "../src/env";
import { generateAccessToken } from "../src/access-token";

export const QRCodeDisplay = ({
scannableQrParams,
}: {
scannableQrParams: ScannableQrParams;
}) => {
const [qrString, setQrString] = useState<string | null>(null);
const getQRcode = useScannableQrCode(scannableQrParams);
const [fetchError, setFetchError] = useState(false);

const accessToken = generateAccessToken();

const fetchQrCode = async (): Promise<ScannableQrCodeResponse> => {
try {
const res: ScannableQrCodeResponse = await axios.post(
`${BASE_URL}/mpesa/qrcode/v1/generate`,
scannableQrParams,
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
);
return res;
} catch (err: any) {
throw new Error(
`Error occurred with status code ${err.response?.status}, ${err.response?.statusText}`
);
}
};

useEffect(() => {
getQRcode().then((res) => setQrString(res.QRCode));
fetchQrCode()
.then((res) => setQrString(res.QRCode))
.catch((err) => setFetchError(true));
}, []);

const qrImage = new Image();
Expand All @@ -24,7 +48,9 @@ export const QRCodeDisplay = ({
<div
className={`w-[${scannableQrParams.Size}] h-[${scannableQrParams.Size}]`}
>
{qrString ? (
{fetchError ? (
<p>An error occurred!</p>
) : qrString ? (
<img src={qrImage.src} alt="QR Code" className="w-full h-full" />
) : (
<FontAwesomeIcon
Expand Down
74 changes: 0 additions & 74 deletions hooks/useReactDaraja.tsx

This file was deleted.

50 changes: 0 additions & 50 deletions hooks/useSTKPush.tsx

This file was deleted.

30 changes: 0 additions & 30 deletions hooks/useScannableQrCode.tsx

This file was deleted.

Loading

0 comments on commit 5d9fa75

Please sign in to comment.