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/nest_frontend/components/interactive/BoxConditionUser.js

76 lines
2.3 KiB
JavaScript
Raw Normal View History

2021-05-18 00:04:06 +00:00
import React, { useContext, useState } from "react"
2021-04-29 14:58:31 +00:00
import BoxFull from "../base/BoxFull"
2021-05-11 14:37:15 +00:00
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"
2021-04-29 14:58:31 +00:00
import { faAt, faPlus } from "@fortawesome/free-solid-svg-icons"
import InputWithIcon from "../base/InputWithIcon"
import FormInline from "../base/FormInline"
2021-04-30 20:10:00 +00:00
import Style from "./BoxConditionUser.module.css"
2021-04-29 14:58:31 +00:00
import ButtonIconOnly from "../base/ButtonIconOnly"
import useRepositoryEditor from "../../hooks/useRepositoryEditor"
2021-05-07 03:02:20 +00:00
import Condition from "../../utils/Condition"
2021-05-18 00:04:06 +00:00
import ContextLanguage from "../../contexts/ContextLanguage"
2021-04-29 14:23:11 +00:00
const INVALID_USER_CHARACTERS = /[^a-zA-Z0-9]/g
2021-04-29 14:58:31 +00:00
/**
* A {@link BoxFull} that allows the user to select a Twitter user to search for, and then to add it as a Condition
* to the {@link ContextRepositoryEditor}.
*
* @param props - Additional props to pass to the box.
* @returns {JSX.Element}
* @constructor
*/
2021-04-29 14:23:11 +00:00
export default function BoxConditionUser({ ...props }) {
const [user, setUser] = useState("")
2021-05-11 14:37:15 +00:00
const { addCondition } = useRepositoryEditor()
2021-05-18 00:48:34 +00:00
const { strings } = useContext(ContextLanguage)
2021-04-29 14:23:11 +00:00
const onInputChange = event => {
let text = event.target.value
text = text.replace(INVALID_USER_CHARACTERS, "")
return setUser(text)
}
2021-05-07 22:35:13 +00:00
const onButtonClick = e => {
2021-05-07 03:02:20 +00:00
addCondition(new Condition("USER", user))
2021-04-29 14:23:11 +00:00
setUser("")
2021-05-07 22:35:13 +00:00
// Prevent reloading the page!
e.preventDefault()
2021-04-29 14:23:11 +00:00
}
return (
2021-05-17 23:33:08 +00:00
<BoxFull
header={
<span>
2021-05-18 00:04:06 +00:00
{strings.searchBy}
2021-05-17 23:33:08 +00:00
&nbsp;
<FontAwesomeIcon icon={faAt}/>
&nbsp;
2021-05-18 00:04:06 +00:00
{strings.byUser}
2021-05-17 23:33:08 +00:00
</span>
}
{...props}
>
2021-05-07 22:35:13 +00:00
<FormInline onSubmit={onButtonClick}>
2021-04-29 14:23:11 +00:00
<InputWithIcon
className={Style.Input}
id={"condition-hashtag"}
icon={faAt}
value={user}
onChange={onInputChange}
placeholder={"jack"}
/>
<ButtonIconOnly
className={Style.Button}
icon={faPlus}
color={"Green"}
onClick={onButtonClick}
/>
</FormInline>
</BoxFull>
)
}