1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-22 21:14:18 +00:00
pds-2021-g2-nest/code/frontend/src/components/interactive/SummaryRepository.js

114 lines
4.1 KiB
JavaScript
Raw Normal View History

import React, { useContext } from "react"
import Style from "./RepositorySummaryBase.module.css"
import classNames from "classnames"
2021-05-11 14:37:15 +00:00
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"
2021-04-29 14:58:31 +00:00
import Button from "../base/Button"
2021-05-11 16:24:51 +00:00
import { faArchive, faFolder, faFolderOpen, faPencilAlt, faTrash } from "@fortawesome/free-solid-svg-icons"
2021-05-10 13:22:07 +00:00
import { useHistory } from "react-router"
2021-05-11 14:38:56 +00:00
import useBackend from "../../hooks/useBackend"
import ContextUser from "../../contexts/ContextUser"
/**
* A long line representing a repository in a list.
*
2021-05-11 16:24:51 +00:00
* @param repo - The repository object.
2021-05-11 14:33:12 +00:00
* @param refresh - Function that can be called to refresh the repositories list.
* @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.
* @param className - Additional class(es) to be added to the outer box.
* @param props - Additional props to pass to the outer box.
* @returns {JSX.Element}
* @constructor
*/
2021-05-11 16:24:51 +00:00
export default function SummaryRepository(
{ repo, refresh, canDelete, canEdit, canArchive, className, ...props },
) {
2021-05-11 14:37:15 +00:00
const { fetchDataAuth } = useContext(ContextUser)
2021-05-10 15:17:16 +00:00
const history = useHistory()
2021-05-11 16:24:51 +00:00
const { fetchNow: archiveThis } = useBackend(fetchDataAuth, "PATCH", `/api/v1/repositories/${repo.id}`, { "close": true })
const { fetchNow: unarchiveThis } = useBackend(fetchDataAuth, "PATCH", `/api/v1/repositories/${repo.id}`, { "open": true })
const { fetchNow: deletThis } = useBackend(fetchDataAuth, "DELETE", `/api/v1/repositories/${repo.id}`)
2021-05-10 13:22:07 +00:00
2021-05-11 16:24:51 +00:00
const onEditClick = () => {
history.push(`/repositories/${repo.id}/edit`)
2021-05-11 14:33:12 +00:00
}
2021-05-11 16:24:51 +00:00
const onArchiveClick = async () => {
2021-05-11 14:33:12 +00:00
await archiveThis()
await refresh()
}
2021-05-11 16:24:51 +00:00
const onUnarchiveClick = async () => {
2021-05-11 14:33:12 +00:00
await unarchiveThis()
await refresh()
}
2021-05-11 16:24:51 +00:00
const onDeleteClick = async () => {
2021-05-11 14:33:12 +00:00
await deletThis()
await refresh()
}
return (
<div className={classNames(Style.RepositorySummary, className)} {...props}>
<div className={Style.Left}>
<div className={Style.IconContainer}>
2021-05-11 16:24:51 +00:00
<FontAwesomeIcon
icon={repo.is_active ? faFolderOpen : faFolder}
/>
</div>
<div className={Style.Title}>
2021-05-11 16:24:51 +00:00
{repo.name}
</div>
2021-05-10 13:22:07 +00:00
<div className={Style.Author}>
2021-05-11 16:24:51 +00:00
{repo.owner.username}
</div>
</div>
<div className={Style.Middle}>
2021-05-11 16:05:01 +00:00
<div className={classNames(Style.MiddleLabel, Style.MiddleTop)}>
Start:
2021-05-10 13:22:07 +00:00
</div>
2021-05-11 16:05:01 +00:00
<div className={classNames(Style.MiddleValue, Style.MiddleTop)}>
2021-05-11 16:24:51 +00:00
{repo.start}
2021-05-11 16:05:01 +00:00
</div>
<div className={classNames(Style.MiddleLabel, Style.MiddleBot)}>
End:
</div>
<div className={classNames(Style.MiddleValue, Style.MiddleBot)}>
2021-05-11 16:24:51 +00:00
{repo.end}
2021-05-10 13:22:07 +00:00
</div>
</div>
<div className={Style.Right}>
{canDelete ?
2021-05-11 14:37:15 +00:00
<Button
color={"Red"}
icon={faTrash}
onClick={onDeleteClick}
>
Delete
</Button>
: null}
{canEdit ?
2021-05-11 14:37:15 +00:00
<Button
color={"Yellow"}
icon={faPencilAlt}
onClick={onEditClick}
>
Edit
</Button>
: null}
{canArchive ?
2021-05-11 14:37:15 +00:00
<Button
color={"Grey"}
icon={faArchive}
2021-05-11 16:24:51 +00:00
onClick={repo.is_active ? onArchiveClick : onUnarchiveClick}
2021-05-11 14:37:15 +00:00
>
2021-05-11 16:24:51 +00:00
{repo.is_active ? "Archive" : "Unarchive"}
2021-05-11 14:37:15 +00:00
</Button>
: null}
</div>
</div>
)
}