Hive React Dev – Day 11: Getting Cash backs details Like a Boss & Having Fun 🍕💸

in HiveDevs11 days ago

🎉 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! ❤️


Sort:  

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.

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