1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-23 05:24:18 +00:00
pds-2021-g2-nest/code/frontend/src/components/interactive/RepositorySummaryBase.js

112 lines
4 KiB
JavaScript
Raw Normal View History

import React, { useContext } from "react"
import Style from "./RepositorySummaryBase.module.css"
import classNames from "classnames"
import {FontAwesomeIcon} from "@fortawesome/react-fontawesome"
2021-04-29 14:58:31 +00:00
import Button from "../base/Button"
import { faArchive, faPencilAlt, faTrash } from "@fortawesome/free-solid-svg-icons"
2021-05-10 13:22:07 +00:00
import { useHistory } from "react-router"
import useData from "../../hooks/useData"
import ContextUser from "../../contexts/ContextUser"
/**
* A long line representing a repository in a list.
*
2021-05-10 13:22:07 +00:00
* @param id - The id of the repository.
2021-05-11 14:33:12 +00:00
* @param refresh - Function that can be called to refresh the repositories list.
2021-05-10 13:22:07 +00:00
* @param owner - The owner of the repository.
* @param icon - The FontAwesome IconDefinition that represents the repository.
2021-05-02 16:10:09 +00:00
* @param name - The title of the repository.
* @param start - The start date of the repository.
* @param end - The end date of the repository.
2021-05-11 14:33:12 +00:00
* @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.
* @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
*/
export default function RepositorySummaryBase(
2021-05-11 14:33:12 +00:00
{ id, refresh, owner, icon, name, start, end, is_active, canDelete, canEdit, canArchive, className, ...props }
) {
const {fetchDataAuth} = useContext(ContextUser)
2021-05-10 15:17:16 +00:00
const history = useHistory()
const {fetchNow: archiveThis} = useData(fetchDataAuth, "PATCH", `/api/v1/repositories/${id}`, {"close": true})
const {fetchNow: unarchiveThis} = useData(fetchDataAuth, "PATCH", `/api/v1/repositories/${id}`, {"open": true})
const {fetchNow: deletThis} = useData(fetchDataAuth, "DELETE", `/api/v1/repositories/${id}`)
2021-05-10 13:22:07 +00:00
2021-05-11 14:33:12 +00:00
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 (
<div className={classNames(Style.RepositorySummary, className)} {...props}>
<div className={Style.Left}>
<div className={Style.IconContainer}>
<FontAwesomeIcon icon={icon}/>
</div>
<div className={Style.Title}>
2021-05-02 16:10:09 +00:00
{name}
</div>
2021-05-10 13:22:07 +00:00
<div className={Style.Author}>
{owner["username"]}
</div>
</div>
<div className={Style.Middle}>
2021-05-10 13:22:07 +00:00
<div className={Style.StartDate}>
Start: {start}
</div>
<div className={Style.EndDate}>
End: {end}
</div>
</div>
<div className={Style.Right}>
{canDelete ?
2021-05-10 13:22:07 +00:00
<Button
color={"Red"}
icon={faTrash}
2021-05-11 14:33:12 +00:00
onClick={onDeleteClick}
2021-05-10 13:22:07 +00:00
>
Delete
</Button>
: null}
{canEdit ?
2021-05-10 13:22:07 +00:00
<Button
color={"Yellow"}
icon={faPencilAlt}
2021-05-11 14:33:12 +00:00
onClick={onEditClick}
2021-05-10 13:22:07 +00:00
>
Edit
</Button>
: null}
{canArchive ?
2021-05-10 13:22:07 +00:00
<Button
color={"Grey"}
icon={faArchive}
2021-05-11 14:33:12 +00:00
onClick={is_active ? onArchiveClick : onUnarchiveClick}
2021-05-10 13:22:07 +00:00
>
2021-05-11 14:33:12 +00:00
{is_active ? "Archive" : "Unarchive"}
2021-05-10 13:22:07 +00:00
</Button>
: null}
</div>
</div>
)
}