Skip to content

Commit

Permalink
fixed settings api change
Browse files Browse the repository at this point in the history
  • Loading branch information
alimaktabi committed Jan 26, 2024
1 parent fbf1fd4 commit 099b9c9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
8 changes: 7 additions & 1 deletion context/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ import { GlobalContextProvider } from "./globalProvider";
import { UserContextProvider } from "./userProfile";
import { Settings } from "@/types";
import WalletProvider from "./walletProvider";
import { parseFieldSetting, snakeToCamel } from "@/utils/api";

export const UnitapProvider: FC<PropsWithChildren> = async ({ children }) => {
const settings: Settings = await fetch(
const settingsRes: { index: string; value: string }[] = await fetch(
process.env.NEXT_PUBLIC_API_URL! + "/api/gastap/settings/",
{ next: { revalidate: 180 } }
).then((res) => res.json());

const settings: Settings = settingsRes.reduce((prev, curr) => {
(prev as any)[snakeToCamel(curr.index)] = parseFieldSetting(curr.value);
return prev;
}, {} as Settings);

return (
<ErrorsProvider>
<GlobalContextProvider>
Expand Down
29 changes: 27 additions & 2 deletions utils/api/gastap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,34 @@ export const convertFaucetToChain = (faucet: Faucet) => {
} as Chain;
};

export const snakeToCamel = (str: string) => {
return str.replace(/_([a-z])/g, function (match, group) {
return group.toUpperCase();
});
};

export const parseFieldSetting = (value: string) => {
if (value === "True") return true;
if (value === "False") return false;

if (!isNaN(value as any)) return Number(value);

return value;
};

export async function getWeeklyChainClaimLimitAPI() {
const response = await axiosInstance.get<Settings>("/api/gastap/settings/");
return response.data;
const response = await axiosInstance.get<{ index: string; value: string }[]>(
"/api/gastap/settings/"
);

const result: Settings = response.data.reduce((prev, curr) => {
(prev as any)[snakeToCamel(curr.index)] = parseFieldSetting(curr.value);
return prev;
}, {} as Settings);

console.log(result);

return result;
}

export async function getRemainingClaimsAPI(token: string) {
Expand Down

0 comments on commit 099b9c9

Please sign in to comment.