2022-07-18 04:10:33 +00:00
|
|
|
import { faAsterisk, faPencil, faSave } from "@fortawesome/free-solid-svg-icons"
|
2022-06-11 03:08:49 +00:00
|
|
|
import { useTranslation } from "next-i18next"
|
|
|
|
import { useDefinedContext } from "../../../utils/definedContext"
|
|
|
|
import { EditingContext, EditingMode } from "../../generic/editable/base"
|
2022-07-18 04:10:33 +00:00
|
|
|
import { usePromise, UsePromiseStatus } from "../../generic/loading/promise"
|
2022-06-11 03:08:49 +00:00
|
|
|
import { FestaIcon } from "../../generic/renderers/fontawesome"
|
|
|
|
import { Tool } from "../../generic/toolbar/tool"
|
2022-07-18 04:10:33 +00:00
|
|
|
import cursor from "../../../styles/cursor.module.css"
|
2022-06-11 03:08:49 +00:00
|
|
|
|
|
|
|
|
2022-07-16 17:21:42 +00:00
|
|
|
export type ToolToggleEditingProps = {
|
2022-07-18 04:10:33 +00:00
|
|
|
save: () => Promise<void>,
|
2022-07-16 17:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-06-11 03:08:49 +00:00
|
|
|
/**
|
2022-07-16 16:03:37 +00:00
|
|
|
* ToolBar {@link Tool} which switches between {@link EditingMode}s of the surrounding context.
|
2022-07-18 04:10:33 +00:00
|
|
|
*
|
|
|
|
* 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.
|
2022-06-11 03:08:49 +00:00
|
|
|
*/
|
2022-07-18 04:10:33 +00:00
|
|
|
export function ToolToggleEditing(props: ToolToggleEditingProps) {
|
2022-06-11 03:08:49 +00:00
|
|
|
const { t } = useTranslation()
|
|
|
|
const [editing, setEditing] = useDefinedContext(EditingContext)
|
|
|
|
|
2022-07-18 04:10:33 +00:00
|
|
|
const { run: save, status: saveStatus } = usePromise<void, void>(props.save)
|
|
|
|
|
|
|
|
if (saveStatus === UsePromiseStatus.PENDING) {
|
|
|
|
return (
|
|
|
|
<Tool
|
|
|
|
aria-label={t("toolToggleEditingSaving")}
|
|
|
|
disabled
|
|
|
|
className={cursor.loadingBlocking}
|
|
|
|
>
|
|
|
|
<FestaIcon icon={faAsterisk} spin />
|
|
|
|
</Tool>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
else if (editing === EditingMode.EDIT) {
|
2022-06-11 03:08:49 +00:00
|
|
|
return (
|
|
|
|
<Tool
|
2022-07-18 03:06:39 +00:00
|
|
|
aria-label={t("toolToggleEditingSave")}
|
2022-07-16 17:21:42 +00:00
|
|
|
onClick={() => {
|
2022-07-18 03:06:39 +00:00
|
|
|
save()
|
2022-07-16 17:21:42 +00:00
|
|
|
setEditing(EditingMode.VIEW)
|
|
|
|
}}
|
2022-07-18 03:08:08 +00:00
|
|
|
className={"positive"}
|
2022-06-11 03:08:49 +00:00
|
|
|
>
|
2022-07-18 03:06:39 +00:00
|
|
|
<FestaIcon icon={faSave} />
|
2022-06-11 03:08:49 +00:00
|
|
|
</Tool>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return (
|
|
|
|
<Tool
|
2022-07-18 03:06:39 +00:00
|
|
|
aria-label={t("toolToggleEditingEdit")}
|
2022-07-16 17:21:42 +00:00
|
|
|
onClick={() => {
|
|
|
|
setEditing(EditingMode.EDIT)
|
|
|
|
}}
|
2022-06-11 03:08:49 +00:00
|
|
|
>
|
|
|
|
<FestaIcon icon={faPencil} />
|
|
|
|
</Tool>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|