#am i doing this correctly and what am i doing wrong?

1 messages · Page 1 of 1 (latest)

timid igloo
#

@fickle tangle hey

fickle tangle
#

why is home red?

#

and am i doing it correctly?

timid igloo
#

whats the problem

fickle tangle
#

n im wondering if im doing swr correctly

timid igloo
#

hover over the Home component and take a screenshot

#

@fickle tangle specifically I want to know whats the error

fickle tangle
timid igloo
#

well it basically says you're trying to use undefined instead of an Element.
is this like a Next.js thing?

fickle tangle
#

matt was helping me yesterday lol

twilit timber
#

Unfortunately I don't actually know any Next.js

fickle tangle
#

no way

#

i really thought u knew

timid igloo
twilit timber
#

I know TypeScript but not Next.js haha

#

Looks like the issue is the return type of your function

timid igloo
#

shit

#

you're not returning JSX i guess

fickle tangle
#

if i remove this it works

#

see

twilit timber
#

Type (props: Props) => Element | undefined is not assignable to type NextPage<Props, Props> means that you've told it that Home will be a NextPage, but given it something else. I believe that the thing you've given it is unable to be coerced into NextPage because it sometimes returns undefined.

timid igloo
#

yeah, use a ternary operator to send a fallback component

fickle tangle
twilit timber
#

Does it work if you change the "Refreshing...." into JSX?

fickle tangle
#

is it a huge problem or we can ignore it?

timid igloo
#

try:

if (!data) return <p>NOOO!!!!</p>
return (
  <>
  <Head>...
  <Head/>
  </>
);
timid igloo
twilit timber
#

Yeah so you just have to turn "Refreshing...." into an Element like <span>Refreshing....</span> instead of a string

fickle tangle
#

thank you myy brother

timid igloo
#

may god bless Ben Awad

fickle tangle
#

waitttttttttt

#

wait wait

timid igloo
#

what??

fickle tangle
#

it broke

#

suppose to be this

timid igloo
#

yeah this is bc you're sending <p></p> when there's no data
once u get data it renders the latter

timid igloo
#

probably, if that's an issue try react-query

fickle tangle
#

hmm i just want it to update when someone donates lol

timid igloo
#

SWR is more like a wrapper around fetch

fickle tangle
#

eah

#

yeah*

fickle tangle
#

like is it bad

timid igloo
#

yeah that might work. the issue with that is you're re-fetching every n-seconds and the data might have not changed during that time.
not sure if SWR has cashing built-in

fickle tangle
#

or can we ignore it?

fickle tangle
#

bc it isnt really a eror

#

erroor

#

error

timid igloo
#

if it isn't critical I'd just re-fetch on demand (refresh the page)
i think there's a way to make the page static and still have updated data

fickle tangle
timid igloo
#

u could set up a fake api

fickle tangle
#

it turns out tht is a big problem

timid igloo
#

send the code with triple ` surrounding it

fickle tangle
#
import type { NextPage } from 'next'
import Head from 'next/head'
import Image from 'next/image'
import qrcode from "../public/assets/qrcode_bank.hackclub.com.png"
import useSWR from 'swr'


const api_url = 'https://bank.hackclub.com/api/v3/organizations/unite-hacks/donations'


interface Donation {
  id: string;
  object: string;
  href: string;
  donor: {
    name: string;
  };
  amount_cents: number;
}

interface Props {
  donations: Donation[];
}


const Home: NextPage<Props> = (props) => {
  const { data } = useSWR(api_url);

  if (!data) return (
<>
    <Head>
        <title>Unite Hacks Bake Sale</title>
        <link rel="icon" href="/favicon.ico" />
      </Head>
    <div className="flex min-h-screen flex-col items-center justify-center py-2">
      <div className='font-extrabold text-[24px]'>Unite Hacks Bake Sale🍪</div>
      <Image src={qrcode} alt="qrcode" width={300} height={300}/>
      {props.donations.map((donation: Donation) => (
        <div key={donation.id}>
           <div className='flex text-center background2'>
             <span className='text-[18px] background'>DONATION FROM {donation.donor.name}</span>
             <span className='text-[18px] background'>${donation.amount_cents/100}.00</span>
           </div> 
         </div>
      ))}
    </div>  
</>
  )
}

export const getServerSideProps = async () => {
  const res = await fetch(api_url);
  const data = await  res.json();

  return {
    props: {donations: data}
  }
}

export default Home;
timid igloo
#
const Home: NextPage<Props> = (props) => {
  const { data } = useSWR(api_url);

  if (!data) {
    return <div>
        We're sorry there's no data.... (Make a spinner of something)
    </div>
  }

    return (
        <>
            <Head>
                <title>Unite Hacks Bake Sale</title>
                <link rel="icon" href="/favicon.ico" />
            </Head>
            <div className="flex min-h-screen flex-col items-center justify-center py-2">
            <div className='font-extrabold text-[24px]'>Unite Hacks Bake Sale🍪</div>
            <Image src={qrcode} alt="qrcode" width={300} height={300}/>
            {props.donations.map((donation: Donation) => (
                <div key={donation.id}>
                <div className='flex text-center background2'>
                    <span className='text-[18px] background'>DONATION FROM {donation.donor.name}</span>
                    <span className='text-[18px] background'>${donation.amount_cents/100}.00</span>
                </div> 
                </div>
            ))}
            </div>  
        </>
    );
}
fickle tangle
timid igloo
#

my bad, you're getting the data from the server

timid igloo
#

like you don't need this const { data } = useSWR(api_url);

#

bc you are using const res = await fetch(api_url);

fickle tangle
timid igloo
#

and in theory you're getting props: {donations: data} inside props

timid igloo
#

is more a problem with typescript rather than a fetch problem

const Home: NextPage<Props> = (props) => {
    const { donations } = props;
    return (
       <>
            <Head>
                <title>Unite Hacks Bake Sale</title>
                <link rel="icon" href="/favicon.ico" />
            </Head>
            <div className="flex min-h-screen flex-col items-center justify-center py-2">
            <div className='font-extrabold text-[24px]'>Unite Hacks Bake Sale🍪</div>
            <Image src={qrcode} alt="qrcode" width={300} height={300}/>
            
            {donations.length > 0 && donations.map((donation) => (
                <div key={donation.id}>
                <div className='flex text-center background2'>
                    <span className='text-[18px] background'>DONATION FROM {donation.donor.name}</span>
                    <span className='text-[18px] background'>${donation.amount_cents/100}.00</span>
                </div> 
                </div>
            ))}
            </div>  
        </>
    );
}
fickle tangle
#

wait wat did u change lol

fickle tangle
timid igloo
# fickle tangle so it should update when someone donates?

let's say someone donates, how in the world is the client (your react code) going to know that?
(you need a way to trigger an "event" on the client)
you could use: websockets to have a real-time update and I'll let u decide if that's too much work

fickle tangle
timid igloo
#

a good-enough solution would be this #1021224125978656829 message
literally fetch the api every 10 minutes, setup a cashing mechanism and you're done

timid igloo
fickle tangle
#

thank you really appreciate it

timid igloo
#

np

#

I haven't done stuff in next lately. Hopefully some React chad might be able to help next time

fickle tangle
#

wanna donate to my hackathon lol

fickle tangle
timid igloo
fickle tangle
timid igloo
#

u should pay me a dollar

fickle tangle
fickle tangle
timid igloo
#

heroes don't ask for rewards (I'm vengeance)

timid igloo
fickle tangle
#

so we need alot of money

#

now where going to do a bakesale but i have to fix this

#

to make sure it updates

timid igloo
#

cool. Feel free to ask if you need help again 😎 🤘