1
Fork 0
mirror of https://github.com/Steffo99/festa.git synced 2024-12-23 15:14:23 +00:00
festa/components/events/toolbar/toolToggleEditing.tsx

65 lines
2.1 KiB
TypeScript
Raw Normal View History

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"
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"
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 = {
save: () => Promise<void>,
2022-07-16 17:21:42 +00:00
}
2022-06-11 03:08:49 +00:00
/**
* 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.
2022-06-11 03:08:49 +00:00
*/
export function ToolToggleEditing(props: ToolToggleEditingProps) {
2022-06-11 03:08:49 +00:00
const { t } = useTranslation()
const [editing, setEditing] = useDefinedContext(EditingContext)
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
aria-label={t("toolToggleEditingSave")}
2022-07-16 17:21:42 +00:00
onClick={() => {
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
>
<FestaIcon icon={faSave} />
2022-06-11 03:08:49 +00:00
</Tool>
)
}
else {
return (
<Tool
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>
)
}
}