Skip to content

Commit 763b7c8

Browse files
authored
Merge pull request #110 from dharmesh03/intergrate-api
Integrate create and fetch API
2 parents a488a91 + 7938ceb commit 763b7c8

File tree

3 files changed

+95
-7
lines changed

3 files changed

+95
-7
lines changed

.env.example

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
COGNITO_DOMAIN=
2+
COGNITO_CLIENT_ID=
3+
NEXTAUTH_URL=
4+
COGNITO_CLIENT_SECRET=
5+
NEXT_PUBLIC_LOGIN_REGISTER_COGNITO_CLIENT_ID=
6+
NEXT_PUBLIC_COGNITO_REGION=
7+
NEXT_PUBLIC_COGNITO_USER_POOL_ID=
8+
SECRET=
9+
NEXT_PUBLIC_X_API_KEY=
10+
NEXT_PUBLIC_APP_ENV=dev // prod,staging

src/components/common/apiCalls/jiffyApis.tsx

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,9 @@ const performApiCall = (network: string): boolean => {
301301

302302
const PRO_API = "https://api.jiffyscan.xyz"
303303
const DEV_API = "https://api-dev.jiffyscan.xyz"
304-
const API_URL = process.env.ENV === 'production' ? PRO_API : DEV_API;
304+
const API_URL = process.env.NEXT_PUBLIC_APP_ENV === 'production' ? PRO_API : DEV_API;
305+
const X_API_Key = process.env.NEXT_PUBLIC_X_API_KEY ;
306+
305307

306308
console.log(API_URL)
307309

@@ -316,6 +318,15 @@ export const showToast = (toast: any, message: string, type?: string) => {
316318
pauseOnHover: true,
317319
theme: 'colored',
318320
});
321+
} else if (type == 'success') {
322+
toast.success(message, {
323+
position: 'bottom-left',
324+
autoClose: 5000,
325+
hideProgressBar: false,
326+
closeOnClick: true,
327+
pauseOnHover: true,
328+
theme: 'colored',
329+
});
319330
} else {
320331
toast.error(message, {
321332
position: 'bottom-left',
@@ -810,7 +821,6 @@ export const getPayMasterDetails = async (
810821
}
811822
return data.paymasterDetail as PayMasterActivity;
812823
}
813-
814824
return {} as PayMasterActivity;
815825
};
816826
export const getPoweredBy = async (beneficiary: string, paymaster: string, toast: any): Promise<PoweredBy> => {
@@ -923,7 +933,6 @@ export const getBundleDetails = async (
923933
if (Object.keys(data.bundleDetails).length == 0) {
924934
showToast(toast, 'Error fetching data');
925935
}
926-
927936
return data.bundleDetails as Bundle;
928937
}
929938
return {} as Bundle;
@@ -963,3 +972,41 @@ export const getBundlerDetails = async (
963972
}
964973
return {} as Bundle;
965974
};
975+
976+
977+
978+
export const fetchAPIKeyList = async (Authorization :string, sub :string) => {
979+
let config = {
980+
method: 'get',
981+
maxBodyLength: Infinity,
982+
url: `${API_URL}/user/getxapikey?usersub=${sub}`,
983+
headers: {
984+
Authorization: Authorization,
985+
'x-api-key': X_API_Key,
986+
},
987+
};
988+
return await axios.request(config)
989+
}
990+
991+
export const createAPIKey = async (Authorization :string, toast:any) => {
992+
let config = {
993+
method: 'post',
994+
maxBodyLength: Infinity,
995+
url: `${API_URL}/user/createxapikey`,
996+
headers: {
997+
Authorization,
998+
'x-api-key': X_API_Key,
999+
},
1000+
};
1001+
try {
1002+
return await axios.request(config).then(()=>{
1003+
showToast(toast, `API Key Created SuccessFully` , "success");
1004+
});
1005+
} catch (error: any) {
1006+
if (error.request) {
1007+
showToast(toast, `No response received, ${error.request}`);
1008+
} else {
1009+
console.error('Error:', error.message);
1010+
}
1011+
}
1012+
};

src/views/ApiKeys/index.tsx

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,52 @@
11
import Footer from '@/components/global/footer/Footer';
22
import Navbar from '@/components/global/navbar/Navbar';
33
import React, { useEffect, useState } from 'react';
4+
import { useSession } from "next-auth/react"
45
import Table, { tableDataT } from '@/components/common/table/Table';
56
import table_data from './table_data.json';
6-
import { ToastContainer } from 'react-toastify';
7+
import moment from 'moment';
8+
import { ToastContainer , toast} from 'react-toastify';
79
import 'react-toastify/dist/ReactToastify.css';
810

11+
import { createAPIKey, fetchAPIKeyList } from "@/components/common/apiCalls/jiffyApis";
12+
913

1014
function APIKeys() {
11-
const [apiKeysTable] = useState<tableDataT>( table_data as tableDataT);
15+
const [apiKeysTable, setApiKeysTable] = useState<tableDataT>(table_data as tableDataT);
16+
1217
const [tableLoading] = useState(false);
1318
const [captionText, setCaptionText] = useState('');
19+
const { data: sessions } = useSession()
20+
21+
const { id_token, sub } = sessions?.user as { id_token: string; sub: string } || {};;
1422

1523
useEffect(() => {
16-
setCaptionText(`${apiKeysTable.rows.length} API Keys found`);
17-
}, []);
24+
const fetchData = async () => {
25+
if (!sub) return;
26+
try {
27+
const { data } = await fetchAPIKeyList(id_token, sub);
28+
if (Array.isArray(data.data.apiKeys)) {
29+
const newRows = data.data.apiKeys.map((item :any) => ({
30+
keys: item.value,
31+
created: moment(item.createdDate).format('MM/DD/yyyy'),
32+
}));
33+
setApiKeysTable((prevState) => ({ ...prevState, rows: newRows }));
34+
}
35+
} catch (error) {
36+
console.error(error);
37+
}
38+
};
1839

40+
fetchData();
41+
}, [sub]);
42+
43+
useEffect(() => {
44+
setCaptionText(`${apiKeysTable.rows.length} API Keys found`);
45+
}, [apiKeysTable]);
1946

47+
const handleCreateApiKey = () => {
48+
createAPIKey(id_token, toast);
49+
}
2050

2151
return (
2252
<div className="">
@@ -31,6 +61,7 @@ function APIKeys() {
3161
<button
3262
className="text-sm border border-dark-200 text-md gap-2 inline-flex tracking-[1.25px] pt-[9px] pb-[9px] px-4 uppercase rounded bg-black text-white"
3363
style={{ display: 'flex', flexDirection: 'row', alignItems: 'center', justifyContent: 'center' }}
64+
onClick={handleCreateApiKey}
3465
>
3566
<img className="-translate-y-[1px]" src="/images/plus.svg" alt="" />
3667
<span>CREATE NEW KEY</span>

0 commit comments

Comments
 (0)