1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-24 14:04:19 +00:00
pds-2021-g2-nest/nest_frontend/components/providers/RepositoryEditor.js

172 lines
6 KiB
JavaScript
Raw Normal View History

2021-05-24 03:03:26 +00:00
import React, { useCallback, useContext, useState } from "react"
import ContextConditionEditor from "../../contexts/ContextConditionEditor"
2021-04-29 14:58:31 +00:00
import useArrayState from "../../hooks/useArrayState"
import Style from "./RepositoryEditor.module.css"
import BoxConditionLocation from "../interactive/BoxConditionLocation"
import BoxConditionHashtag from "../interactive/BoxConditionHashtag"
import BoxConditionUser from "../interactive/BoxConditionUser"
import BoxConditionDatetime from "../interactive/BoxConditionDatetime"
import BoxConditions from "../interactive/BoxConditions"
import BoxRepositoryCreate from "../interactive/BoxRepositoryCreate"
import classNames from "classnames"
2021-05-07 23:40:49 +00:00
import ContextUser from "../../contexts/ContextUser"
2021-05-24 03:02:07 +00:00
import { Condition } from "../../objects/Condition"
import useBackendViewset from "../../hooks/useBackendViewset"
import { Redirect } from "react-router"
export default function RepositoryEditor({
2021-05-11 14:37:15 +00:00
id = null,
name,
is_active: isActive,
start,
end,
conditions,
evaluation_mode: evaluationMode,
className,
}) {
2021-05-24 03:02:07 +00:00
/** The currently logged in user. */
const { user } = useContext(ContextUser)
/** The repository name. */
const [_name, setName] = useState(name ?? "")
/** The conditions of the data gathering. */
const {
2021-05-24 03:02:07 +00:00
value: rawConditions,
setValue: setRawConditions,
appendValue: appendRawCondition,
removeValue: removeRawCondition,
spliceValue: spliceRawCondition,
} = useArrayState(conditions)
2021-05-24 03:02:07 +00:00
const _conditions = rawConditions.map(cond => Condition.fromRaw(cond))
/** The operator the conditions should be evaluated with. */
2021-05-07 23:40:49 +00:00
const [_evaluationMode, setEvaluationMode] = useState(evaluationMode ?? 0)
2021-05-24 03:02:07 +00:00
/** The backend viewset to use to create / edit the repository. */
const {running, error, createResource, editResource} = useBackendViewset(
`/api/v1/repositories/`,
"id",
{
list: false,
create: true,
retrieve: false,
edit: true,
destroy: false,
command: false,
action: false,
}
)
2021-05-07 23:40:49 +00:00
2021-05-24 03:02:07 +00:00
/** If `true`, switches to the repository page on the next render. */
const [switchPage, setSwitchPage] = useState(false)
/**
* Save the current changes, creating or editing it as needed.
*
* @type {(function(): Promise<void>)|*}
*/
const save = useCallback(
async () => {
const body = {
2021-05-11 16:05:01 +00:00
"id": id,
"name": _name,
2021-05-11 17:42:04 +00:00
"start": null,
2021-05-24 03:02:07 +00:00
"is_active": true,
"end": null,
"owner": user,
"spectators": null,
"evaluation_mode": _evaluationMode,
"conditions": _conditions,
2021-05-11 16:05:01 +00:00
}
2021-05-11 16:05:01 +00:00
if(!id) {
2021-05-24 03:02:07 +00:00
console.info("Creating new repository with body: ", body)
await createResource(body)
}
else {
2021-05-24 03:02:07 +00:00
console.info("Editing repository ", id, " with body: ", body)
await editResource(id, body)
}
2021-05-24 03:02:07 +00:00
setSwitchPage(true)
},
2021-05-24 03:02:07 +00:00
[id, createResource, editResource, _conditions, _evaluationMode, _name, user],
)
2021-05-07 23:40:49 +00:00
/**
* Cancel the changes made so far to the repository.
2021-05-24 03:02:07 +00:00
*
* @type {(function(): void)|*}
*/
const revert = useCallback(
() => {
setName(name)
setRawConditions(conditions)
setEvaluationMode(evaluationMode)
},
2021-05-11 16:05:01 +00:00
[name, isActive, start, end, conditions, evaluationMode, setRawConditions],
)
/**
* Try to add a new condition, logging a message to the console if something goes wrong.
2021-05-24 03:02:07 +00:00
*
* @type {(function(): void)|*}
*/
const addCondition = useCallback(
(newCond) => {
// Check for duplicates
2021-05-11 14:37:15 +00:00
let duplicate = null
for(const oldCond of _conditions) {
if(newCond.type === oldCond.type && newCond.content === oldCond.content) {
duplicate = oldCond
break
}
}
if(duplicate) {
2021-05-24 03:02:07 +00:00
console.debug("Cannot add ", newCond, ": ", duplicate, " already exists.")
return
}
2021-05-24 03:02:07 +00:00
console.debug("Adding ", newCond, " to the repository conditions")
appendRawCondition(newCond)
},
2021-05-11 16:05:01 +00:00
[_conditions, appendRawCondition],
)
2021-05-24 03:02:07 +00:00
// Hack to switch page on success
if(!error && switchPage) {
return <Redirect to={"/repositories"}/>
}
return (
<ContextConditionEditor.Provider
2021-05-11 14:37:15 +00:00
value={{
conditions: _conditions, addCondition, appendRawCondition, removeRawCondition, spliceRawCondition,
}}
>
<div className={classNames(Style.RepositoryEditor, className)}>
<BoxConditionLocation className={Style.SearchByZone}/>
<BoxConditionHashtag className={Style.SearchByHashtags}/>
<BoxConditionUser className={Style.SearchByUser}/>
<BoxConditionDatetime className={Style.SearchByTimePeriod}/>
<BoxConditions className={Style.Conditions}/>
<BoxRepositoryCreate
className={Style.CreateDialog}
2021-05-25 02:41:49 +00:00
id={id}
name={_name}
setName={setName}
evaluationMode={_evaluationMode}
setEvaluationMode={setEvaluationMode}
running={running}
error={error}
revert={revert}
save={save}
/>
</div>
</ContextConditionEditor.Provider>
)
}