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

155 lines
5.7 KiB
JavaScript
Raw Normal View History

2021-05-11 16:05:01 +00:00
import React, { useCallback, useContext, useMemo, useState } from "react"
2021-04-29 14:58:31 +00:00
import ContextRepositoryEditor from "../../contexts/ContextRepositoryEditor"
import useArrayState from "../../hooks/useArrayState"
import Style from "./RepositoryEditor.module.css"
import BoxConditionMap from "../interactive/BoxConditionMap"
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-11 14:38:56 +00:00
import useBackend from "../../hooks/useBackend"
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,
}) {
/** The repository name. */
const [_name, setName] = useState(name ?? "")
/** The repository state (active / archived). */
const [_isActive, setActive] = useState(isActive ?? true)
/** The start date of the data gathering. */
const [_start, setStart] = useState(start ?? new Date().toISOString())
/** The end date of the data gathering. */
const [_end, setEnd] = useState(end ?? new Date().toISOString())
/** The conditions of the data gathering. */
const {
value: _conditions,
setValue: setRawConditions,
appendValue: appendRawCondition,
removeValue: removeRawCondition,
spliceValue: spliceRawCondition,
} = useArrayState(conditions)
/** The operator the conditions should be evaluated with. */
2021-05-07 23:40:49 +00:00
const [_evaluationMode, setEvaluationMode] = useState(evaluationMode ?? 0)
2021-05-11 14:37:15 +00:00
const { user, fetchDataAuth } = useContext(ContextUser)
2021-05-07 23:40:49 +00:00
const method = id ? "PUT" : "POST"
const path = id ? `/api/v1/repositories/${id}` : `/api/v1/repositories/`
2021-05-11 16:05:01 +00:00
const body = useMemo(
() => {
return {
"conditions": _conditions,
2021-05-11 17:42:04 +00:00
"end": null,
2021-05-11 16:05:01 +00:00
"evaluation_mode": _evaluationMode,
"id": id,
"is_active": true,
"name": _name,
"owner": user,
2021-05-11 17:42:04 +00:00
"start": null,
2021-05-11 16:05:01 +00:00
}
},
2021-05-12 02:10:36 +00:00
[_conditions, _evaluationMode, id, _name, user],
2021-05-11 16:05:01 +00:00
)
2021-05-11 14:38:56 +00:00
const { error, loading, fetchNow } = useBackend(fetchDataAuth, method, path, body)
const save = useCallback(
2021-05-11 17:42:04 +00:00
async () => {
2021-05-11 16:05:01 +00:00
if(!id) {
console.info("Creando una nuova repository avente come corpo: ", body)
}
else {
console.info("Modificando la repository ", id, " con corpo: ", body)
}
2021-05-11 17:42:04 +00:00
await fetchNow()
},
2021-05-11 14:37:15 +00:00
[id, body, fetchNow],
)
2021-05-07 23:40:49 +00:00
/**
* Cancel the changes made so far to the repository.
*/
const revert = useCallback(
() => {
setName(name)
setActive(isActive)
setStart(start)
setEnd(end)
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.
*/
const addCondition = useCallback(
(newCond) => {
// Check for content
if(!newCond.content) {
console.debug("Impossibile aggiungere ", newCond, ": l'oggetto è vuoto.")
return
}
// 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) {
console.debug("Impossibile aggiungere ", newCond, ": ", duplicate, " è già esistente.")
return
}
console.debug("Aggiungendo ", newCond, " alle condizioni del repository")
appendRawCondition(newCond)
},
2021-05-11 16:05:01 +00:00
[_conditions, appendRawCondition],
)
return (
2021-05-11 14:37:15 +00:00
<ContextRepositoryEditor.Provider
value={{
id,
name: _name, setName,
isActive: _isActive, setActive,
start: _start, setStart,
end: _end, setEnd,
conditions: _conditions, addCondition, appendRawCondition, removeRawCondition, spliceRawCondition,
evaluationMode: _evaluationMode, setEvaluationMode,
error, loading,
revert, save,
}}
>
<div className={classNames(Style.RepositoryEditor, className)}>
<BoxConditionMap className={Style.SearchByZone}/>
<BoxConditionHashtag className={Style.SearchByHashtags}/>
<BoxConditionUser className={Style.SearchByUser}/>
<BoxConditionDatetime className={Style.SearchByTimePeriod}/>
<BoxConditions className={Style.Conditions}/>
<BoxRepositoryCreate className={Style.CreateDialog}/>
</div>
</ContextRepositoryEditor.Provider>
)
}