generated from kristiania-pg6301-2022/pg6301-exam-template
-
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.
- Loading branch information
1 parent
19e7fec
commit 2b3af50
Showing
5 changed files
with
88 additions
and
145 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,30 @@ | ||
import {useEffect, useState} from "react"; | ||
import { useEffect, useState } from "react"; | ||
|
||
export async function fetchJSON(url) { | ||
const res = await fetch(url); | ||
if (!res.ok) { | ||
throw new Error(`Failed ${res.status}`); | ||
} | ||
return await res.json(); | ||
export async function fetchJSON(url, options = { method: "GET" }) { | ||
const res = await fetch(url, options); | ||
|
||
if (!res.ok) { | ||
throw new Error(`Failed ${res.status}`); | ||
} | ||
return await res.json(); | ||
} | ||
|
||
export function useLoader(loadingFn) { | ||
const [loading, setLoading] = useState(true); | ||
const [data, setData] = useState(); | ||
const [error, setError] = useState(); | ||
const [loading, setLoading] = useState(true); | ||
const [data, setData] = useState(); | ||
const [error, setError] = useState(); | ||
|
||
async function load() { | ||
try { | ||
setLoading(true); | ||
setData(await loadingFn()); | ||
} catch (error) { | ||
setError(error); | ||
} finally { | ||
setLoading(false); | ||
} | ||
async function load() { | ||
try { | ||
setLoading(true); | ||
setData(await loadingFn()); | ||
} catch (error) { | ||
setError(error); | ||
} finally { | ||
setLoading(false); | ||
} | ||
} | ||
|
||
useEffect(() => load(), []); | ||
return {loading, data, error}; | ||
useEffect(() => load(), []); | ||
return { loading, data, error }; | ||
} |
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,4 @@ | ||
export function getHumanDate(timestamp) { | ||
const humanDate = new Date(parseInt(timestamp)).toLocaleString(); | ||
return humanDate; | ||
} |
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