mirror of
https://github.com/pds-nest/nest.git
synced 2024-11-25 14:34:19 +00:00
🔧 Create useBackend hook
This commit is contained in:
parent
eda9ab227f
commit
a3d23661a3
5 changed files with 63 additions and 41 deletions
18
code/frontend/src/components/base/Starting.js
Normal file
18
code/frontend/src/components/base/Starting.js
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
import React from "react"
|
||||||
|
import { faSpinner } from "@fortawesome/free-solid-svg-icons"
|
||||||
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A div with a static dots icon and a "Starting..." text.
|
||||||
|
*
|
||||||
|
* @returns {JSX.Element}
|
||||||
|
* @constructor
|
||||||
|
*/
|
||||||
|
export default function Starting() {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<FontAwesomeIcon icon={faSpinner}/> Starting...
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
|
@ -2,13 +2,12 @@
|
||||||
display: grid;
|
display: grid;
|
||||||
|
|
||||||
grid-template-areas:
|
grid-template-areas:
|
||||||
"b c"
|
"b c d"
|
||||||
"b d"
|
"b e e"
|
||||||
"b e"
|
"b f f"
|
||||||
"b f"
|
"b g g";
|
||||||
"b g";
|
grid-template-columns: 400px 1fr 1fr;
|
||||||
grid-template-columns: 400px 1fr;
|
grid-template-rows: auto auto 1fr auto;
|
||||||
grid-template-rows: auto auto auto 1fr auto;
|
|
||||||
|
|
||||||
grid-gap: 10px;
|
grid-gap: 10px;
|
||||||
|
|
||||||
|
|
|
@ -5,24 +5,16 @@ import BoxHeader from "../components/base/BoxHeader"
|
||||||
import RepositoryEditor from "../components/providers/RepositoryEditor"
|
import RepositoryEditor from "../components/providers/RepositoryEditor"
|
||||||
import useBackendImmediately from "../hooks/useBackendImmediately"
|
import useBackendImmediately from "../hooks/useBackendImmediately"
|
||||||
import ContextUser from "../contexts/ContextUser"
|
import ContextUser from "../contexts/ContextUser"
|
||||||
import BoxAlert from "../components/base/BoxAlert"
|
import renderContents from "../utils/renderContents"
|
||||||
import Loading from "../components/base/Loading"
|
|
||||||
|
|
||||||
|
|
||||||
export default function PageEdit({ id, className, ...props }) {
|
export default function PageEdit({ id, className, ...props }) {
|
||||||
const { fetchDataAuth } = useContext(ContextUser)
|
const { fetchDataAuth } = useContext(ContextUser)
|
||||||
const { data, error } = useBackendImmediately(fetchDataAuth, "GET", `/api/v1/repositories/${id}`)
|
const repositoryRequest = useBackendImmediately(fetchDataAuth, "GET", `/api/v1/repositories/${id}`)
|
||||||
|
const contents = renderContents(
|
||||||
let contents
|
repositoryRequest,
|
||||||
if(error) {
|
data => <RepositoryEditor className={Style.RepositoryEditor} {...data}/>,
|
||||||
contents = <BoxAlert color={"Red"}>{error.toString()}</BoxAlert>
|
)
|
||||||
}
|
|
||||||
else if(data) {
|
|
||||||
contents = <RepositoryEditor className={Style.RepositoryEditor} {...data}/>
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
contents = <Loading/>
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={classNames(Style.PageHome, className)} {...props}>
|
<div className={classNames(Style.PageHome, className)} {...props}>
|
||||||
|
|
|
@ -5,30 +5,24 @@ import BoxRepositoriesActive from "../components/interactive/BoxRepositoriesActi
|
||||||
import BoxRepositoriesArchived from "../components/interactive/BoxRepositoriesArchived"
|
import BoxRepositoriesArchived from "../components/interactive/BoxRepositoriesArchived"
|
||||||
import useBackendImmediately from "../hooks/useBackendImmediately"
|
import useBackendImmediately from "../hooks/useBackendImmediately"
|
||||||
import ContextUser from "../contexts/ContextUser"
|
import ContextUser from "../contexts/ContextUser"
|
||||||
import BoxAlert from "../components/base/BoxAlert"
|
import renderContents from "../utils/renderContents"
|
||||||
import Loading from "../components/base/Loading"
|
|
||||||
|
|
||||||
|
|
||||||
export default function PageRepositories({ children, className, ...props }) {
|
export default function PageRepositories({ children, className, ...props }) {
|
||||||
const { fetchDataAuth } = useContext(ContextUser)
|
const { fetchDataAuth } = useContext(ContextUser)
|
||||||
const { data, error, fetchNow: refresh } = useBackendImmediately(fetchDataAuth, "GET", "/api/v1/repositories/")
|
const repositoryRequest = useBackendImmediately(fetchDataAuth, "GET", "/api/v1/repositories/")
|
||||||
|
const contents = renderContents(
|
||||||
let contents
|
repositoryRequest,
|
||||||
if(error) {
|
data => {
|
||||||
contents = <BoxAlert color={"Red"}>{error}</BoxAlert>
|
const repositories = [...data["owner"], ...data["spectator"]]
|
||||||
}
|
const active = repositories.filter(r => r.is_active)
|
||||||
else if(data) {
|
const archived = repositories.filter(r => !r.is_active)
|
||||||
const repositories = [...data["owner"], ...data["spectator"]]
|
return <>
|
||||||
const active = repositories.filter(r => r.is_active)
|
<BoxRepositoriesActive repositories={active} refresh={repositoryRequest.fetchNow}/>
|
||||||
const archived = repositories.filter(r => !r.is_active)
|
<BoxRepositoriesArchived repositories={archived} refresh={repositoryRequest.fetchNow}/>
|
||||||
contents = <>
|
</>
|
||||||
<BoxRepositoriesActive repositories={active} refresh={refresh}/>
|
},
|
||||||
<BoxRepositoriesArchived repositories={archived} refresh={refresh}/>
|
)
|
||||||
</>
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
contents = <Loading/>
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={classNames(Style.PageRepositories, className)} {...props}>
|
<div className={classNames(Style.PageRepositories, className)} {...props}>
|
||||||
|
|
19
code/frontend/src/utils/renderContents.js
Normal file
19
code/frontend/src/utils/renderContents.js
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
import Loading from "../components/base/Loading"
|
||||||
|
import BoxAlert from "../components/base/BoxAlert"
|
||||||
|
import Starting from "../components/base/Starting"
|
||||||
|
|
||||||
|
|
||||||
|
export default function renderContents(requestHookResults, renderFunction) {
|
||||||
|
const { data, error, loading } = requestHookResults
|
||||||
|
|
||||||
|
if(loading) {
|
||||||
|
return <Loading/>
|
||||||
|
}
|
||||||
|
if(error) {
|
||||||
|
return <BoxAlert color={"Red"}>{error}</BoxAlert>
|
||||||
|
}
|
||||||
|
if(data) {
|
||||||
|
return renderFunction(data)
|
||||||
|
}
|
||||||
|
return <Starting/>
|
||||||
|
}
|
Loading…
Reference in a new issue