1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-25 22:44:19 +00:00

Add BoxConditions and improve ConditionBadge and RepositoryEditor

This commit is contained in:
Stefano Pigozzi 2021-04-29 05:03:58 +02:00
parent c81f50e3df
commit 418a06f939
Signed by untrusted user who does not match committer: steffo
GPG key ID: 6965406171929D01
3 changed files with 62 additions and 6 deletions

View file

@ -0,0 +1,17 @@
import React, { useContext } from "react"
import BoxFull from "./BoxFull"
import ContextRepositoryEditor from "../contexts/ContextRepositoryEditor"
import ConditionBadge from "./ConditionBadge"
export default function BoxConditions({ ...props }) {
const {conditions} = useContext(ContextRepositoryEditor)
const badges = conditions.map((cond) => <ConditionBadge key={cond.id} {...cond}/>)
return (
<BoxFull header={"Conditions"} {...props}>
{badges}
</BoxFull>
)
}

View file

@ -1,21 +1,54 @@
import React from "react" import React, { useContext } from "react"
import Style from "./ConditionBadge.module.css" import Style from "./ConditionBadge.module.css"
import classNames from "classnames" import classNames from "classnames"
import {FontAwesomeIcon} from "@fortawesome/react-fontawesome" import {FontAwesomeIcon} from "@fortawesome/react-fontawesome"
import ButtonSmallX from "./ButtonSmallX" import ButtonSmallX from "./ButtonSmallX"
import { faClock, faHashtag, faMapPin, faUser } from "@fortawesome/free-solid-svg-icons"
import ContextRepositoryEditor from "../contexts/ContextRepositoryEditor"
export default function ConditionBadge({ color, icon, onDelete, children, className, ...props }) { const CONDITION_COLORS = {
0: "Grey", // Hashtag
1: "Red", // Location
2: "Yellow", // Time
3: "Green", // User
}
const CONDITION_ICONS = {
0: faHashtag, // Hashtag
1: faMapPin, // Location
2: faClock, // Time
3: faUser, // User
}
/**
* A small colored badge representing a Condition for a filter.
*
* @param condition - The Condition that this badge represents.
* @returns {JSX.Element}
* @constructor
*/
export default function ConditionBadge(condition) {
const { id, type, content } = condition
const color = CONDITION_COLORS[type]
const icon = CONDITION_ICONS[type]
const {removeCondition} = useContext(ContextRepositoryEditor)
return ( return (
<div className={classNames(Style.ConditionBadge, Style[`ConditionBadge${color}`], className)} {...props}> <div
title={`Condition ID: ${id}`}
className={classNames(Style.ConditionBadge, Style[`ConditionBadge${color}`])}
>
<div className={Style.Icon}> <div className={Style.Icon}>
<FontAwesomeIcon icon={icon}/> <FontAwesomeIcon icon={icon}/>
</div> </div>
<div> <div>
{children} {content}
</div> </div>
<div> <div>
<ButtonSmallX onClick={onDelete}/> <ButtonSmallX onClick={() => removeCondition(condition)}/>
</div> </div>
</div> </div>
) )

View file

@ -7,7 +7,12 @@ export default function RepositoryEditor({ children, id, name, start, end, condi
const [_name, setName] = useState(name) const [_name, setName] = useState(name)
const [_start, setStart] = useState(start) const [_start, setStart] = useState(start)
const [_end, setEnd] = useState(end) const [_end, setEnd] = useState(end)
const {_conditions, appendCondition, removeCondition} = useArrayState(conditions) const {
value: _conditions,
appendValue: appendCondition,
removeValue: removeCondition,
spliceValue: spliceCondition,
} = useArrayState(conditions)
return ( return (
<ContextRepositoryEditor.Provider value={{ <ContextRepositoryEditor.Provider value={{
@ -21,6 +26,7 @@ export default function RepositoryEditor({ children, id, name, start, end, condi
conditions: _conditions, conditions: _conditions,
appendCondition, appendCondition,
removeCondition, removeCondition,
spliceCondition,
}}> }}>
{children} {children}
</ContextRepositoryEditor.Provider> </ContextRepositoryEditor.Provider>