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

🐛 Fix bugs in RepositoryEditor

This commit is contained in:
Stefano Pigozzi 2021-05-11 18:05:01 +02:00
parent b7a7da8621
commit 841356adf5
Signed by untrusted user who does not match committer: steffo
GPG key ID: 6965406171929D01
9 changed files with 83 additions and 49 deletions

View file

@ -81,7 +81,7 @@ export default function BoxConditionMap({ ...props }) {
map.off("zoom", onZoom) map.off("zoom", onZoom)
} }
}, },
[map], [map, onMove, onZoom],
) )
const onButtonClick = () => { const onButtonClick = () => {

View file

@ -3,7 +3,7 @@ import BoxFull from "../base/BoxFull"
import FormLabelled from "../base/FormLabelled" import FormLabelled from "../base/FormLabelled"
import FormLabel from "../base/formparts/FormLabel" import FormLabel from "../base/formparts/FormLabel"
import InputWithIcon from "../base/InputWithIcon" import InputWithIcon from "../base/InputWithIcon"
import { faFolder, faPencilAlt, faPlus } from "@fortawesome/free-solid-svg-icons" import { faBackward, faFolder, faPencilAlt, faPlus } from "@fortawesome/free-solid-svg-icons"
import Radio from "../base/Radio" import Radio from "../base/Radio"
import Button from "../base/Button" import Button from "../base/Button"
import useRepositoryEditor from "../../hooks/useRepositoryEditor" import useRepositoryEditor from "../../hooks/useRepositoryEditor"
@ -27,6 +27,7 @@ export default function BoxRepositoryCreate({ ...props }) {
name, name,
setName, setName,
save, save,
revert,
error, error,
} = useRepositoryEditor() } = useRepositoryEditor()
@ -55,7 +56,7 @@ export default function BoxRepositoryCreate({ ...props }) {
onChange={() => setEvaluationMode(0)} onChange={() => setEvaluationMode(0)}
checked={evaluationMode === 0} checked={evaluationMode === 0}
/> />
At least one filter One filter
</label> </label>
&nbsp; &nbsp;
<label> <label>
@ -73,20 +74,30 @@ export default function BoxRepositoryCreate({ ...props }) {
</FormAlert> </FormAlert>
: null} : null}
{id ? {id ?
<>
<Button <Button
style={{ "gridColumn": "1 / 3" }} style={{ "gridColumn": "1" }}
icon={faBackward}
color={"Red"}
onClick={e => revert()}
>
Rollback edits
</Button>
<Button
style={{ "gridColumn": "2" }}
icon={faPencilAlt} icon={faPencilAlt}
color={"Green"} color={"Green"}
onClick={e => goToOnSuccess(save, history, "/repositories")} onClick={_ => goToOnSuccess(save, history, "/repositories")()}
> >
Edit repository Save changes
</Button> </Button>
</>
: :
<Button <Button
style={{ "gridColumn": "1 / 3" }} style={{ "gridColumn": "1 / 3" }}
icon={faPlus} icon={faPlus}
color={"Green"} color={"Green"}
onClick={e => goToOnSuccess(save, history, "/repositories")} onClick={_ => goToOnSuccess(save, history, "/repositories")()}
> >
Create repository Create repository
</Button> </Button>

View file

@ -41,7 +41,6 @@ export default function ConditionBadge({ ...condition }) {
let displayedContent = content let displayedContent = content
if(type === 3) { if(type === 3) {
let split = displayedContent.split(" ") let split = displayedContent.split(" ")
let type = split[0]
let radius = Number.parseFloat(split[1]).toFixed(0) let radius = Number.parseFloat(split[1]).toFixed(0)
let radiusType = "m" let radiusType = "m"
if(radius >= 2000) { if(radius >= 2000) {

View file

@ -70,11 +70,17 @@ export default function RepositorySummaryBase(
</div> </div>
</div> </div>
<div className={Style.Middle}> <div className={Style.Middle}>
<div className={Style.StartDate}> <div className={classNames(Style.MiddleLabel, Style.MiddleTop)}>
Start: {start} Start:
</div> </div>
<div className={Style.EndDate}> <div className={classNames(Style.MiddleValue, Style.MiddleTop)}>
End: {end} {start}
</div>
<div className={classNames(Style.MiddleLabel, Style.MiddleBot)}>
End:
</div>
<div className={classNames(Style.MiddleValue, Style.MiddleBot)}>
{end}
</div> </div>
</div> </div>
<div className={Style.Right}> <div className={Style.Right}>

View file

@ -59,22 +59,30 @@
.Middle { .Middle {
flex-grow: 1; flex-grow: 1;
height: 60px; height: 60px;
padding: 5px;
background-color: var(--bg-light); background-color: var(--bg-light);
display: flex; display: grid;
flex-direction: column; grid-template-columns: auto 1fr;
justify-content: space-between; grid-template-rows: 1fr 1fr;
align-items: stretch;
} }
.StartDate { .MiddleLabel {
grid-column: 1;
} }
.EndDate { .MiddleValue {
grid-column: 2;
}
.MiddleTop {
grid-row: 1;
}
.MiddleBot {
grid-row: 2;
} }
.Right { .Right {

View file

@ -1,4 +1,4 @@
import React, { useCallback, useContext, useState } from "react" import React, { useCallback, useContext, useMemo, useState } from "react"
import ContextRepositoryEditor from "../../contexts/ContextRepositoryEditor" import ContextRepositoryEditor from "../../contexts/ContextRepositoryEditor"
import useArrayState from "../../hooks/useArrayState" import useArrayState from "../../hooks/useArrayState"
import Style from "./RepositoryEditor.module.css" import Style from "./RepositoryEditor.module.css"
@ -51,7 +51,9 @@ export default function RepositoryEditor({
const method = id ? "PUT" : "POST" const method = id ? "PUT" : "POST"
const path = id ? `/api/v1/repositories/${id}` : `/api/v1/repositories/` const path = id ? `/api/v1/repositories/${id}` : `/api/v1/repositories/`
const body = { const body = useMemo(
() => {
return {
"conditions": _conditions, "conditions": _conditions,
"end": _end, "end": _end,
"evaluation_mode": _evaluationMode, "evaluation_mode": _evaluationMode,
@ -61,11 +63,14 @@ export default function RepositoryEditor({
"owner": user, "owner": user,
"start": _start, "start": _start,
} }
},
[_conditions, _end, _evaluationMode, id, _name, user, _start],
)
const { error, loading, fetchNow } = useBackend(fetchDataAuth, method, path, body) const { error, loading, fetchNow } = useBackend(fetchDataAuth, method, path, body)
const save = useCallback( const save = useCallback(
() => { () => {
if(id) { if(!id) {
console.info("Creating new repository with body: ", body) console.info("Creating new repository with body: ", body)
} }
else { else {
@ -89,7 +94,7 @@ export default function RepositoryEditor({
setRawConditions(conditions) setRawConditions(conditions)
setEvaluationMode(evaluationMode) setEvaluationMode(evaluationMode)
}, },
[name, isActive, start, end, conditions, evaluationMode], [name, isActive, start, end, conditions, evaluationMode, setRawConditions],
) )
/** /**
@ -119,7 +124,7 @@ export default function RepositoryEditor({
console.debug("Adding ", newCond, " to the Repository Conditions") console.debug("Adding ", newCond, " to the Repository Conditions")
appendRawCondition(newCond) appendRawCondition(newCond)
}, },
[_conditions], [_conditions, appendRawCondition],
) )
return ( return (

View file

@ -6,14 +6,19 @@ import RepositoryEditor from "../components/providers/RepositoryEditor"
import useBackendImmediately from "../hooks/useBackendImmediately" import useBackendImmediately from "../hooks/useBackendImmediately"
import ContextUser from "../contexts/ContextUser" import ContextUser from "../contexts/ContextUser"
import renderContents from "../utils/renderContents" import renderContents from "../utils/renderContents"
import { useParams } from "react-router"
export default function PageEdit({ id, className, ...props }) { export default function PageEdit({ className, ...props }) {
const { id } = useParams()
const { fetchDataAuth } = useContext(ContextUser) const { fetchDataAuth } = useContext(ContextUser)
const repositoryRequest = useBackendImmediately(fetchDataAuth, "GET", `/api/v1/repositories/${id}`) const repositoryRequest = useBackendImmediately(fetchDataAuth, "GET", `/api/v1/repositories/${id}`)
const contents = renderContents( const contents = renderContents(
repositoryRequest, repositoryRequest,
data => <RepositoryEditor className={Style.RepositoryEditor} {...data}/>, data => {
console.debug("Data: ", data)
return <RepositoryEditor className={Style.RepositoryEditor} {...data}/>
},
) )
return ( return (

View file

@ -7,17 +7,17 @@
* @returns {(function(): void)|*} * @returns {(function(): void)|*}
*/ */
export default function goToOnSuccess(func, history, destination) { export default function goToOnSuccess(func, history, destination) {
return ([...args]) => { return (...args) => {
let success = false let result
try { try {
func(...args) console.debug("Trying to run: ", func)
success = true result = func(...args)
history.push(destination)
return result
} }
catch(e) { catch(e) {
success = false console.debug("Failed to run ", func, ", not doing anything.")
} throw e
if(success) {
history.push(destination)
} }
} }
} }

View file

@ -10,7 +10,7 @@ export default function renderContents(requestHookResults, renderFunction) {
return <Loading/> return <Loading/>
} }
if(error) { if(error) {
return <BoxAlert color={"Red"}>{error}</BoxAlert> return <BoxAlert color={"Red"}>{error.toString()}</BoxAlert>
} }
if(data) { if(data) {
return renderFunction(data) return renderFunction(data)