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"
|
2021-05-07 15:41:25 +00:00
|
|
|
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"
|
2021-04-29 02:28:33 +00:00
|
|
|
|
|
|
|
|
2021-05-07 01:44:05 +00:00
|
|
|
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-07 01:44:05 +00:00
|
|
|
/** The repository name. */
|
2021-05-10 15:24:32 +00:00
|
|
|
const [_name, setName] = useState(name ?? "")
|
2021-05-07 01:44:05 +00:00
|
|
|
|
|
|
|
/** The repository state (active / archived). */
|
2021-05-10 15:24:32 +00:00
|
|
|
const [_isActive, setActive] = useState(isActive ?? true)
|
2021-05-07 01:44:05 +00:00
|
|
|
|
|
|
|
/** The start date of the data gathering. */
|
2021-05-10 15:24:32 +00:00
|
|
|
const [_start, setStart] = useState(start ?? new Date().toISOString())
|
2021-05-07 01:44:05 +00:00
|
|
|
|
|
|
|
/** The end date of the data gathering. */
|
2021-05-10 15:24:32 +00:00
|
|
|
const [_end, setEnd] = useState(end ?? new Date().toISOString())
|
2021-05-07 01:44:05 +00:00
|
|
|
|
|
|
|
/** The conditions of the data gathering. */
|
2021-04-29 03:03:58 +00:00
|
|
|
const {
|
|
|
|
value: _conditions,
|
2021-05-07 02:48:25 +00:00
|
|
|
setValue: setRawConditions,
|
|
|
|
appendValue: appendRawCondition,
|
|
|
|
removeValue: removeRawCondition,
|
|
|
|
spliceValue: spliceRawCondition,
|
2021-04-29 03:03:58 +00:00
|
|
|
} = useArrayState(conditions)
|
2021-04-29 02:28:33 +00:00
|
|
|
|
2021-05-07 01:44:05 +00:00
|
|
|
/** 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)
|
2021-05-07 01:44:05 +00:00
|
|
|
|
|
|
|
const save = useCallback(
|
2021-05-11 17:42:04 +00:00
|
|
|
async () => {
|
2021-05-11 16:05:01 +00:00
|
|
|
if(!id) {
|
2021-05-14 09:31:14 +00:00
|
|
|
console.info("Creando una nuova repository avente come corpo: ", body)
|
2021-05-07 01:44:05 +00:00
|
|
|
}
|
|
|
|
else {
|
2021-05-14 09:31:14 +00:00
|
|
|
console.info("Modificando la repository ", id, " con corpo: ", body)
|
2021-05-07 01:44:05 +00:00
|
|
|
}
|
2021-05-11 17:42:04 +00:00
|
|
|
await fetchNow()
|
2021-05-07 01:44:05 +00:00
|
|
|
},
|
2021-05-11 14:37:15 +00:00
|
|
|
[id, body, fetchNow],
|
2021-05-07 01:44:05 +00:00
|
|
|
)
|
|
|
|
|
2021-05-07 23:40:49 +00:00
|
|
|
|
2021-05-07 01:44:05 +00:00
|
|
|
/**
|
|
|
|
* Cancel the changes made so far to the repository.
|
|
|
|
*/
|
|
|
|
const revert = useCallback(
|
|
|
|
() => {
|
|
|
|
setName(name)
|
|
|
|
setActive(isActive)
|
|
|
|
setStart(start)
|
|
|
|
setEnd(end)
|
2021-05-07 02:48:25 +00:00
|
|
|
setRawConditions(conditions)
|
2021-05-07 01:44:05 +00:00
|
|
|
setEvaluationMode(evaluationMode)
|
|
|
|
},
|
2021-05-11 16:05:01 +00:00
|
|
|
[name, isActive, start, end, conditions, evaluationMode, setRawConditions],
|
2021-05-07 01:44:05 +00:00
|
|
|
)
|
|
|
|
|
2021-05-07 02:48:25 +00:00
|
|
|
/**
|
|
|
|
* 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) {
|
2021-05-14 09:31:14 +00:00
|
|
|
console.debug("Impossibile aggiungere ", newCond, ": l'oggetto è vuoto.")
|
2021-05-07 02:48:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for duplicates
|
2021-05-11 14:37:15 +00:00
|
|
|
let duplicate = null
|
2021-05-07 02:48:25 +00:00
|
|
|
for(const oldCond of _conditions) {
|
|
|
|
if(newCond.type === oldCond.type && newCond.content === oldCond.content) {
|
|
|
|
duplicate = oldCond
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(duplicate) {
|
2021-05-14 09:31:14 +00:00
|
|
|
console.debug("Impossibile aggiungere ", newCond, ": ", duplicate, " è già esistente.")
|
2021-05-07 02:48:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-14 09:31:14 +00:00
|
|
|
console.debug("Aggiungendo ", newCond, " alle condizioni del repository")
|
2021-05-07 02:48:25 +00:00
|
|
|
appendRawCondition(newCond)
|
|
|
|
},
|
2021-05-11 16:05:01 +00:00
|
|
|
[_conditions, appendRawCondition],
|
2021-05-07 02:48:25 +00:00
|
|
|
)
|
|
|
|
|
2021-04-29 02:28:33 +00:00
|
|
|
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,
|
|
|
|
}}
|
|
|
|
>
|
2021-05-07 15:41:25 +00:00
|
|
|
<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}/>
|
2021-05-17 13:58:24 +00:00
|
|
|
<BoxRepositoryCreate running={loading} className={Style.CreateDialog}/>
|
2021-05-07 15:41:25 +00:00
|
|
|
</div>
|
2021-04-29 02:28:33 +00:00
|
|
|
</ContextRepositoryEditor.Provider>
|
|
|
|
)
|
|
|
|
}
|