2021-05-07 23:40:49 +00:00
|
|
|
import React, { useCallback, useContext, 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"
|
|
|
|
import useData from "../../hooks/useData"
|
2021-04-29 02:28:33 +00:00
|
|
|
|
|
|
|
|
2021-05-07 01:44:05 +00:00
|
|
|
export default function RepositoryEditor({
|
|
|
|
id = null,
|
|
|
|
name,
|
|
|
|
is_active: isActive,
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
conditions,
|
|
|
|
evaluation_mode: evaluationMode,
|
2021-05-07 15:41:25 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
const {user, fetchDataAuth} = useContext(ContextUser)
|
|
|
|
|
|
|
|
const method = id ? "PUT" : "POST"
|
|
|
|
const path = id ? `/api/v1/repositories/${id}` : `/api/v1/repositories/`
|
|
|
|
const body = {
|
|
|
|
"conditions": _conditions,
|
|
|
|
"end": _end,
|
|
|
|
"evaluation_mode": _evaluationMode,
|
|
|
|
"id": id,
|
|
|
|
"is_active": true,
|
|
|
|
"name": _name,
|
|
|
|
"owner": user,
|
|
|
|
"start": _start,
|
|
|
|
}
|
|
|
|
const {error, loading, fetchNow} = useData(fetchDataAuth, method, path, body)
|
2021-05-07 01:44:05 +00:00
|
|
|
|
|
|
|
const save = useCallback(
|
|
|
|
() => {
|
2021-05-07 23:40:49 +00:00
|
|
|
if(id) {
|
|
|
|
console.info("Creating new repository with body: ", body)
|
2021-05-07 01:44:05 +00:00
|
|
|
}
|
|
|
|
else {
|
2021-05-07 23:40:49 +00:00
|
|
|
console.info("Editing repository ", id, " with body: ", body)
|
2021-05-07 01:44:05 +00:00
|
|
|
}
|
2021-05-07 23:40:49 +00:00
|
|
|
fetchNow()
|
2021-05-07 01:44:05 +00:00
|
|
|
},
|
2021-05-07 23:40:49 +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)
|
|
|
|
},
|
|
|
|
[name, isActive, start, end, conditions, evaluationMode]
|
|
|
|
)
|
|
|
|
|
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) {
|
|
|
|
console.debug("Refusing to add ", newCond, ": content is empty.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for duplicates
|
|
|
|
let duplicate = null;
|
|
|
|
for(const oldCond of _conditions) {
|
|
|
|
if(newCond.type === oldCond.type && newCond.content === oldCond.content) {
|
|
|
|
duplicate = oldCond
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(duplicate) {
|
|
|
|
console.debug("Refusing to add ", newCond, ": ", duplicate, " already exists.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
console.debug("Adding ", newCond, " to the Repository Conditions")
|
|
|
|
appendRawCondition(newCond)
|
|
|
|
},
|
|
|
|
[_conditions]
|
|
|
|
)
|
|
|
|
|
2021-04-29 02:28:33 +00:00
|
|
|
return (
|
|
|
|
<ContextRepositoryEditor.Provider value={{
|
|
|
|
id,
|
2021-05-07 01:44:05 +00:00
|
|
|
name: _name, setName,
|
|
|
|
isActive: _isActive, setActive,
|
|
|
|
start: _start, setStart,
|
|
|
|
end: _end, setEnd,
|
2021-05-07 02:48:25 +00:00
|
|
|
conditions: _conditions, addCondition, appendRawCondition, removeRawCondition, spliceRawCondition,
|
2021-05-07 01:44:05 +00:00
|
|
|
evaluationMode: _evaluationMode, setEvaluationMode,
|
2021-05-07 23:40:49 +00:00
|
|
|
error, loading,
|
2021-05-07 01:44:05 +00:00
|
|
|
revert, save,
|
2021-04-29 02:28:33 +00:00
|
|
|
}}>
|
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}/>
|
|
|
|
<BoxRepositoryCreate className={Style.CreateDialog}/>
|
|
|
|
</div>
|
2021-04-29 02:28:33 +00:00
|
|
|
</ContextRepositoryEditor.Provider>
|
|
|
|
)
|
|
|
|
}
|