Last active
April 8, 2022 19:06
-
-
Save polluterofminds/4016906024504954469a730eaaa52269 to your computer and use it in GitHub Desktop.
Members Only Step 10
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from 'react' | |
import { signOut, useSession } from "next-auth/react" | |
import Link from 'next/link'; | |
const Gallery = () => { | |
const { data: session, status } = useSession() | |
const [loading, setLoading] = useState(true); | |
const [address, setAddress] = useState(""); | |
const [index, setIndex] = useState([]); | |
const [imageUrls, setImageUrls] = useState([]); | |
useEffect(() => { | |
if(session && session.user && session.user.name && address !== session.user.name) { | |
setAddress(session.user.name); | |
loadFiles(); | |
} | |
}, [session]); | |
const loadFiles = async () => { | |
const res = await fetch(`/api/media`); | |
const data = await res.json(); | |
setIndex(data.imageIndex); | |
getUrls(data.imageIndex); | |
setLoading(false); | |
}; | |
const getUrls = async (indexArray) => { | |
let urls = []; | |
for(const indexFile of indexArray) { | |
const res = await fetch(`/api/accessToken?id=${indexFile.id}&cid=${indexFile.cid}`); | |
const url = await res.text(); | |
urls.push(url); | |
} | |
setImageUrls(urls); | |
} | |
if(loading) { | |
<div> | |
<h1>Loading...</h1> | |
</div> | |
} else { | |
return ( | |
<div> | |
{ | |
!session ? | |
<div> | |
<h1>You need to sign in</h1> | |
<Link href="/">Sign in</Link> | |
</div> : | |
<div> | |
<h1>The Gallery</h1> | |
<div className="grid-container"> | |
{imageUrls.map((url) => { | |
return ( | |
<div key={url} className="grid-item"> | |
<img className="img" src={url} loading="auto" /> | |
</div> | |
); | |
})} | |
</div> | |
</div> | |
} | |
</div> | |
) | |
} | |
} | |
export default Gallery |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment