From 8bc5014b19409e18330085bbab865d072f6f01b6 Mon Sep 17 00:00:00 2001 From: Izabela Date: Sun, 18 Feb 2024 01:42:15 +0100 Subject: [PATCH] Refactor CouponsContainer and UserProfileContainer components --- src/components/CouponsContainer.tsx | 2 - src/components/UserProfileContainer.tsx | 137 +++++++++++++----------- 2 files changed, 75 insertions(+), 64 deletions(-) diff --git a/src/components/CouponsContainer.tsx b/src/components/CouponsContainer.tsx index 7b4735c..c0df58c 100644 --- a/src/components/CouponsContainer.tsx +++ b/src/components/CouponsContainer.tsx @@ -34,14 +34,12 @@ const CouponsContainer: React.FC = ({ }) => { const [coupons, setCoupons] = useState([]); const [error, setError] = useState(null); - console.log(coupons, "coupons"); useEffect(() => { const fetchData = async () => { try { const data = await getCoupons(); // Fetch data from the API using getCoupons function setCoupons(data.activeCoupons); // Set the fetched data to the state - console.log(data.activeCoupons, "data"); } catch (error) { console.error("Error fetching data:", error); setError("Error fetching data. Please try again."); diff --git a/src/components/UserProfileContainer.tsx b/src/components/UserProfileContainer.tsx index 64f41f9..a019821 100644 --- a/src/components/UserProfileContainer.tsx +++ b/src/components/UserProfileContainer.tsx @@ -1,74 +1,87 @@ -import './CouponsContainer.css'; -import React, { useEffect, useState } from 'react'; +import "./CouponsContainer.css"; +import React, { useEffect, useState } from "react"; import { - IonButton, - IonCard, - IonCardContent, - IonCardHeader, - IonCardSubtitle, - IonCardTitle, - IonInput, - IonItem, - IonLabel, - IonList, - IonThumbnail, -} from '@ionic/react'; -import { signCoupon } from '../helpers/signCoupon'; -import { getData, setData } from '../data/helpers/localStorageService'; -import { DataKey } from '../data/const/localStorageConst'; + IonButton, + IonCard, + IonCardContent, + IonCardHeader, + IonCardSubtitle, + IonCardTitle, + IonInput, + IonItem, + IonLabel, + IonList, + IonThumbnail, +} from "@ionic/react"; +import { signCoupon } from "../helpers/signCoupon"; +import { getData, setData } from "../data/helpers/localStorageService"; +import { DataKey } from "../data/const/localStorageConst"; +import { getShares } from "../services/loyaltyService"; -interface UserProfileProps { - -} +interface UserProfileProps {} const changeName = (newName: string | number | null | undefined) => { - if (!!newName) { - setData(DataKey.Name, `${newName}`); - } -} + if (!!newName) { + setData(DataKey.Name, `${newName}`); + } +}; const UserProfileContainer: React.FC = () => { + const [shares, setShares] = useState(0); + const [error, setError] = useState(null); - const [shares, setShares] = useState(0); - const [error, setError] = useState(null); - - useEffect(() => { - const fetchData = async () => { - try { - setShares(84); - } catch (error) { - console.error('Error fetching data:', error); - setError('Error fetching data. Please try again.'); // You might want to handle the error more gracefully - } - }; + useEffect(() => { + const fetchData = async () => { + console.log(shares); + try { + const data = await getShares(); // Call the getShares function + setShares(data); // Assuming that the returned data has a 'shares' property + console.log("Shares data:", data); + } catch (error) { + console.error("Error fetching data:", error); + setError("Error fetching data. Please try again."); + } + }; - fetchData(); - }, []); + fetchData(); + }, []); - return ( - <> - - - My name: - - changeName(e.target.value)}> - - - - - - My shares: - {shares} - - - - - 0x932890843298943298 - - - - - ); + return ( + <> + + + My name: + + changeName(e.target.value)} + > + + + + + + My shares: + + {shares.numberOfShares !== null + ? shares.numberOfShares + : "Loading..."} + + + + + + 0x932890843298943298 + + + + + ); }; export default UserProfileContainer;