From 70fe072b5e119213e5c2022570a85e6ecf9e5b4c Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi <256895@studenti.unimore.it> Date: Tue, 11 May 2021 16:33:12 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Tweak=20state=20location?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../interactive/BoxRepositoriesActive.js | 49 +++++++------------ .../interactive/BoxRepositoriesArchived.js | 46 +++++++---------- .../interactive/RepositorySummaryBase.js | 32 +++++++++--- code/frontend/src/routes/PageRepositories.js | 29 +++++++++-- 4 files changed, 87 insertions(+), 69 deletions(-) diff --git a/code/frontend/src/components/interactive/BoxRepositoriesActive.js b/code/frontend/src/components/interactive/BoxRepositoriesActive.js index e4cecf7..7389589 100644 --- a/code/frontend/src/components/interactive/BoxRepositoriesActive.js +++ b/code/frontend/src/components/interactive/BoxRepositoriesActive.js @@ -1,51 +1,38 @@ import React, { useContext } from "react" import BoxFull from "../base/BoxFull" -import ContextUser from "../../contexts/ContextUser" -import useData from "../../hooks/useData" import RepositorySummaryBase from "./RepositorySummaryBase" -import Loading from "../base/Loading" -import BoxAlert from "../base/BoxAlert" import { faSearch } from "@fortawesome/free-solid-svg-icons" -import useDataImmediately from "../../hooks/useDataImmediately" +import ContextUser from "../../contexts/ContextUser" /** * A {@link BoxFull} listing all the user's active repositories. * + * @param repositories - Array of repositories to display in the box. + * @param refresh - Function that can be called to refresh the repositories list. * @param props - Additional props to pass to the box. * @returns {JSX.Element} * @constructor */ -export default function BoxRepositoriesActive({ ...props }) { - const {user, fetchDataAuth} = useContext(ContextUser) - const {data, error} = useDataImmediately(fetchDataAuth, "GET", "/api/v1/repositories/", { - "onlyActive": true, - }) +export default function BoxRepositoriesActive({ repositories, refresh, ...props }) { + const {user} = useContext(ContextUser) let contents; - if(error) { - contents = {error.toString()} - } - else if(data) { - let repositories = [...data["owner"], ...data["spectator"]] - if(repositories.length > 0) { - contents = repositories.map(repo => ( - - )) - } - else { - contents = There's nothing here. - } + if(repositories.length > 0) { + contents = repositories.map(repo => ( + + )) } else { - contents = + contents = There's nothing here. } return ( diff --git a/code/frontend/src/components/interactive/BoxRepositoriesArchived.js b/code/frontend/src/components/interactive/BoxRepositoriesArchived.js index 8416b9b..6398c07 100644 --- a/code/frontend/src/components/interactive/BoxRepositoriesArchived.js +++ b/code/frontend/src/components/interactive/BoxRepositoriesArchived.js @@ -2,49 +2,37 @@ import React, { useContext } from "react" import BoxFull from "../base/BoxFull" import ContextUser from "../../contexts/ContextUser" import RepositorySummaryBase from "./RepositorySummaryBase" -import Loading from "../base/Loading" -import BoxAlert from "../base/BoxAlert" import { faSearch } from "@fortawesome/free-solid-svg-icons" -import useDataImmediately from "../../hooks/useDataImmediately" /** * A {@link BoxFull} listing all the user's archived repositories. * + * @param repositories - Array of repositories to display in the box. + * @param refresh - Function that can be called to refresh the repositories list. * @param props - Additional props to pass to the box. * @returns {JSX.Element} * @constructor */ -export default function BoxRepositoriesArchived({ ...props }) { - const {user, fetchDataAuth} = useContext(ContextUser) - const {data, error} = useDataImmediately(fetchDataAuth, "GET", "/api/v1/repositories/", { - "onlyDead": true, - }) +export default function BoxRepositoriesArchived({ repositories, refresh, ...props }) { + const {user} = useContext(ContextUser) let contents; - if(error) { - contents = {error.toString()} - } - else if(data) { - let repositories = [...data["owner"], ...data["spectator"]] - if(repositories.length > 0) { - contents = repositories.map(repo => ( - - )) - } - else { - contents = There's nothing here. - } + if(repositories.length > 0) { + contents = repositories.map(repo => ( + + )) } else { - contents = + contents = There's nothing here. } return ( diff --git a/code/frontend/src/components/interactive/RepositorySummaryBase.js b/code/frontend/src/components/interactive/RepositorySummaryBase.js index 23a0277..dbba1f0 100644 --- a/code/frontend/src/components/interactive/RepositorySummaryBase.js +++ b/code/frontend/src/components/interactive/RepositorySummaryBase.js @@ -13,12 +13,13 @@ import ContextUser from "../../contexts/ContextUser" * A long line representing a repository in a list. * * @param id - The id of the repository. + * @param refresh - Function that can be called to refresh the repositories list. * @param owner - The owner of the repository. * @param icon - The FontAwesome IconDefinition that represents the repository. * @param name - The title of the repository. * @param start - The start date of the repository. * @param end - The end date of the repository. - * @param isActive - Whether the repository is active or not. + * @param is_active - Whether the repository is active or not. * @param canDelete - If the Delete button should be displayed or not. * @param canEdit - If the Edit button should be displayed or not. * @param canArchive - If the Archive button should be displayed or not. @@ -28,7 +29,7 @@ import ContextUser from "../../contexts/ContextUser" * @constructor */ export default function RepositorySummaryBase( - { id, owner, icon, name, start, end, isActive, canDelete, canEdit, canArchive, className, ...props } + { id, refresh, owner, icon, name, start, end, is_active, canDelete, canEdit, canArchive, className, ...props } ) { const {fetchDataAuth} = useContext(ContextUser) const history = useHistory() @@ -36,6 +37,25 @@ export default function RepositorySummaryBase( const {fetchNow: unarchiveThis} = useData(fetchDataAuth, "PATCH", `/api/v1/repositories/${id}`, {"open": true}) const {fetchNow: deletThis} = useData(fetchDataAuth, "DELETE", `/api/v1/repositories/${id}`) + const onEditClick = event => { + history.push(`/repositories/${id}/edit`) + } + + const onArchiveClick = async event => { + await archiveThis() + await refresh() + } + + const onUnarchiveClick = async event => { + await unarchiveThis() + await refresh() + } + + const onDeleteClick = async event => { + await deletThis() + await refresh() + } + return (
@@ -62,7 +82,7 @@ export default function RepositorySummaryBase( @@ -71,7 +91,7 @@ export default function RepositorySummaryBase( @@ -80,9 +100,9 @@ export default function RepositorySummaryBase( : null}
diff --git a/code/frontend/src/routes/PageRepositories.js b/code/frontend/src/routes/PageRepositories.js index c064790..f44ac3c 100644 --- a/code/frontend/src/routes/PageRepositories.js +++ b/code/frontend/src/routes/PageRepositories.js @@ -1,15 +1,38 @@ -import React from "react" +import React, { useContext } from "react" import Style from "./PageRepositories.module.css" import classNames from "classnames" import BoxRepositoriesActive from "../components/interactive/BoxRepositoriesActive" import BoxRepositoriesArchived from "../components/interactive/BoxRepositoriesArchived" +import useDataImmediately from "../hooks/useDataImmediately" +import ContextUser from "../contexts/ContextUser" +import BoxAlert from "../components/base/BoxAlert" +import Loading from "../components/base/Loading" export default function PageRepositories({ children, className, ...props }) { + const {fetchDataAuth} = useContext(ContextUser) + const {data, error, fetchNow: refresh} = useDataImmediately(fetchDataAuth, "GET", "/api/v1/repositories/") + + let contents; + if(error) { + contents = {error} + } + else if(data) { + const repositories = [...data["owner"], ...data["spectator"]] + const active = repositories.filter(r => r.is_active) + const archived = repositories.filter(r => !r.is_active) + contents = <> + + + + } + else { + contents = + } + return (
- - + {contents}
) }