-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Display house points values from Google Sheets (#50)
* Display house points values from Google Sheets - Dynamically fetch house points data from Google Sheets to display together with each house * Use dash for default house points value - Display a dash instead of zero to indicate the value is loading Co-authored-by: Ryan Yang <[email protected]> * Remove unnecessary commented `row-gap` style * Use global value for initial `EMPTY_HOUSE_POINTS` --------- Co-authored-by: Ryan Yang <[email protected]>
- Loading branch information
Showing
3 changed files
with
82 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
const FEED_URL = "https://docs.google.com/spreadsheets/d/"; | ||
const SPREADSHEET_KEY = "1AV98P5Fs3INBtbwT69oJvfN0eAgj9daVw6o_6XIGEiI"; | ||
const MODE = "pub"; | ||
const SHEET_GID = "1705286477"; | ||
|
||
const EMPTY_HOUSE_POINTS = {}; | ||
|
||
function useHousePoints() { | ||
const [housePoints, setHousePoints] = useState(EMPTY_HOUSE_POINTS); | ||
|
||
useEffect(() => { | ||
const dataURL = new URL(`${FEED_URL}${SPREADSHEET_KEY}/${MODE}`); | ||
dataURL.searchParams.set("gid", SHEET_GID); | ||
dataURL.searchParams.set("single", "true"); | ||
dataURL.searchParams.set("output", "tsv"); | ||
|
||
const getHousePoints = async () => { | ||
try { | ||
const response = await fetch(dataURL); | ||
const text = await response.text(); | ||
|
||
const data = {}; | ||
// Parse each tab-separated line | ||
for (const line of text.split("\n")) { | ||
const [key, value] = line.split("\t"); | ||
data[key] = value; | ||
} | ||
|
||
setHousePoints(data); | ||
} catch (err) { | ||
console.error("Error occurred while fetching sheets data:", err); | ||
} | ||
}; | ||
|
||
getHousePoints(); | ||
}, []); | ||
|
||
return housePoints; | ||
} | ||
|
||
export default useHousePoints; |