Hive React Dev โ€“ Day 11: Getting Cash backs details Like a Boss & Having Fun ๐Ÿ•๐Ÿ’ธ

๐ŸŽ‰ Hello Awesome Hive Community!

Iโ€™m back with day 11 of our caffeinated ReactJS adventure! This weekโ€™s flavor? Integrating the Distriator cashback claim API (yes, more cash for you!).


๐Ÿ’ก Epic Learning Journey Recap

Missed a day? Here's the trail of my misadventures:


๐Ÿ’ต Day 11: Cashback Claim API โ€“ Implementation

Todayโ€™s Fun Plan:

  • Fetch Distriator cashback claim data when the user lands on the dashboard or switches accounts. So easy, even a blockchain squirrel could do it ๐Ÿฟ๏ธ.

๐Ÿ“ฆ Step 1: Define the Types!

Do you like types? I like types!

  • In your types folder, add a new file: UserClaimsApiResponse.tsx
  • Pop in these types:
export interface UserClaimResponseDTO {
  claim?: UserClaimResponseClaimDTO;
  claims?: UserClaimResponseClaimDTO[];
  monthly: UserClaimResponseBiweeklyDTO[];
  biweekly: UserClaimResponseBiweeklyDTO[];
}

export interface UserClaimResponseBiweeklyDTO {
  amount: string;
  memo: string;
  invoice: string;
  timestamp: Date;
}

export interface UserClaimResponseClaimDTO {
  amount: string;
  memo: string;
  invoice: string;
  timestamp: Date;
  business: string;
  guides: UserClaimResponseOnborderDTO[];
  country: string;
  onborder: UserClaimResponseOnborderDTO;
  percentage: string;
  claimValue: string;
  transactionAmount: string;
  percentAsPerTransactionAmount: string;
}

export interface UserClaimResponseOnborderDTO {
  name: string;
  percent: string;
  guidesPercent?: string;
  value: string;
}

Screenshot for above snippet

Fun fact: If you think my naming conventions are quirky, tell me yours! Sharing is caring.


๐Ÿ”Œ Step 2: Add the User Claims API

  • Under your api folder, create a file: UserClaimsApiResponse.tsx
  • Drop this code for a token-powered API call:
import type { UserClaimResponseDTO } from "@/types/UserClaimsApiResponse";
import { DISTRIATOR_API_BASE_URL } from "@/api/constants";
import { getCurrentUser } from "@/utils/LocalStorageUtils";

export const fetchUserClaimsApi = async (): Promise<UserClaimResponseDTO> => {
  const user = getCurrentUser();
  if (user === null) {
    throw new Error("Session may have expired");
  }
  const response = await fetch(`${DISTRIATOR_API_BASE_URL}/claims/v2`, {
    method: "GET",
    headers: {
      Accept: "*/*",
      "Content-Type": "application/json",
      Authorization: user.token,
    },
  });
  if (!response.ok) {
    throw new Error("Failed to fetch user claims");
  }
  return response.json();
};

Screenshot for visual learners indicating folder structure and code snippet


๐Ÿšฆ Step 3: Hook API Into Your Dashboard Page

  • Import like a pro:

Let's add necessary imports

import { fetchUserClaimsApi } from "@/api/UserClaimsApi";
import type { UserClaimResponseDTO } from "@/types/UserClaimsApiResponse";
import React, { useEffect, useState } from "react";

const DashboardPage = () => {
  • Call the API and manage the data (and donโ€™t forget error-handling for when the bugs come out to party):
const getUserClaimsData = async () => {
  const fetchClaims = async () => {
    try {
      const data = await fetchUserClaimsApi();
      setClaims(data);
    } catch {
      setErrorMessage("Failed to fetch claims");
      setToastStyle(ToastStyle.Regular);
      setToastType(ToastType.Error);
      setShowToast(true);
    }
    setIsLoading(false);
  };
  fetchClaims();
};

useEffect(() => {
  getUserClaimsData();
}, []);

Now your dashboard revels in cashback glory!

screenshot for visual learners - dashboard with folder structure and file


๐Ÿคฏ Feeling Confused? Itโ€™s Open Tomato Sauce! ๐Ÿฅซ

Confused by code snippets? Donโ€™t stressโ€”I run on โ€œopen sauceโ€! Itโ€™s open source, but with more randomness.

open source


๐Ÿ”ฎ Whatโ€™s Next?

Stay tuned! Next episode:

  • Weโ€™ll ditch the homebrew snackbar for a sonnar-based toast ๐Ÿ””
  • UI for this dashboard coming soon!
  • And thenโ€ฆ the grand reveal: Distriator cashback claim integration!

๐Ÿ™ Thank You for Surviving My Tech Rant!

I hope youโ€™ve smiled, learned, and maybe grabbed some cashback on the way! It's all open source, for Hive & for fun.

More Power to the Hive Community ๐Ÿš€
More Power to Hive Blockchain
Hive to the moon and beyond! ๐ŸŒ•โœจ

See you in the next post! ๐Ÿฅณ


๐Ÿ“ Final Note


๐Ÿš€ My Contributions to โ™ฆ๏ธ Hive Ecosystem

ContributionToHiveEcosystem
Hive Witness NodeHive API Node (in progress)3Speak Video Encoder Node Operator (highest number of nodes)3Speak Mobile App Developer
3Speak Podcast App Developer3Speak Shorts App Developer3Speak Support & Maintenance TeamDistriator Developer
CheckinWithXYZHive InboxHiFindHive Donate App
Contributed to HiveAuth Mobile AppEcency โ†” 3Speak IntegrationEcency โ†” InLeo IntegrationEcency โ†” Actifit Integration
Hive Stats AppVote for Witness AppHiveFlutterKitNew 3Speak App

๐Ÿ™Œ Support Back

โค๏ธ Appreciate my work? Consider supporting @threespeak & @sagarkothari88! โค๏ธ

VoteForWitness
sagarkothari88@sagarkothari88
threespeak@threespeak



0
0
0.000
2 comments
avatar

This post has been manually curated by @bhattg from Indiaunited community. Join us on our Discord Server.

Do you know that you can earn a passive income by delegating to @indiaunited. We share more than 100 % of the curation rewards with the delegators in the form of IUC tokens. HP delegators and IUC token holders also get upto 20% additional vote weight.

Here are some handy links for delegations: 100HP, 250HP, 500HP, 1000HP.

image.png

100% of the rewards from this comment goes to the curator for their manual curation efforts. Please encourage the curator @bhattg by upvoting this comment and support the community by voting the posts made by @indiaunited..

This post received an extra 7.51% vote for delegating HP / holding IUC tokens.

0
0
0.000
avatar

Hello sagarkothari88!

It's nice to let you know that your article will take 8th place.
Your post is among 15 Best articles voted 7 days ago by the @hive-lu | King Lucoin Curator by keithtaylor

You receive ๐ŸŽ– 0.1 unique LUBEST tokens as a reward. You can support Lu world and your curator, then he and you will receive 10x more of the winning token. There is a buyout offer waiting for him on the stock exchange. All you need to do is reblog Daily Report 756 with your winnings.

2.png


Invest in the Lu token (Lucoin) and get paid. With 50 Lu in your wallet, you also become the curator of the @hive-lu which follows your upvote.
Buy Lu on the Hive-Engine exchange | World of Lu created by szejq

If you no longer want to receive notifications, reply to this comment with the word STOP or to resume write a word START

0
0
0.000