import { faAsterisk, faPencil, faSave } from "@fortawesome/free-solid-svg-icons" import { useTranslation } from "next-i18next" import { useDefinedContext } from "../../../utils/definedContext" import { EditingContext, EditingMode } from "../../generic/editable/base" import { usePromise, UsePromiseStatus } from "../../generic/loading/promise" import { FestaIcon } from "../../generic/renderers/fontawesome" import { Tool } from "../../generic/toolbar/tool" import cursor from "../../../styles/cursor.module.css" import mood from "../../../styles/mood.module.css" export type ToolToggleEditingProps = { save: () => Promise, } /** * ToolBar {@link Tool} which switches between {@link EditingMode}s of the surrounding context. * * It calls an async function to save data when switching from edit mode to view mode, preventing the user from switching back again until the data is saved, but allowing them to view the updated resource. */ export function ToolToggleEditing(props: ToolToggleEditingProps) { const { t } = useTranslation() const [editing, setEditing] = useDefinedContext(EditingContext) const { run: save, status: saveStatus } = usePromise(props.save) if (saveStatus === UsePromiseStatus.PENDING) { return ( ) } else if (editing === EditingMode.EDIT) { return ( { save() setEditing(EditingMode.VIEW) }} className={mood.positive} > ) } else { return ( { setEditing(EditingMode.EDIT) }} > ) } }