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
|
|
|
}
|
|
|
|
},
|
|
|
|
[_conditions, _end, _evaluationMode, id, _name, user, _start],
|
|
|
|
)
|
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-07 23:40:49 +00:00
|
|
|
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-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) {
|
|
|
|
console.debug("Refusing to add ", newCond, ": content is empty.")
|
|
|
|
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) {
|
|
|
|
console.debug("Refusing to add ", newCond, ": ", duplicate, " already exists.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
console.debug("Adding ", newCond, " to the Repository Conditions")
|
|
|
|
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}/>
|
|
|
|
<BoxRepositoryCreate className={Style.CreateDialog}/>
|
|
|
|
</div>
|
2021-04-29 02:28:33 +00:00
|
|
|
</ContextRepositoryEditor.Provider>
|
|
|
|
)
|
|
|
|
}
|