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

64 lines
1.9 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 { 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"
import mood from "../../../styles/mood.module.css"
2022-07-19 18:25:11 +00:00
import { Dispatch } from "react"
2022-06-11 03:08:49 +00:00
2022-07-16 17:21:42 +00:00
export type ToolToggleEditingProps = {
2022-07-19 18:25:11 +00:00
editing: boolean,
setEditing: Dispatch<boolean>,
save: () => Promise<void>,
2022-07-16 17:21:42 +00:00
}
2022-06-11 03:08:49 +00:00
/**
2022-07-19 18:25:11 +00:00
* ToolBar {@link Tool} which allows the user to start editing an event and then save their changes.
2022-06-11 03:08:49 +00:00
*/
2022-07-19 18:25:11 +00:00
export function ToolToggleEditing({ editing, setEditing, save: upperSave }: ToolToggleEditingProps) {
2022-06-11 03:08:49 +00:00
const { t } = useTranslation()
2022-07-19 18:25:11 +00:00
const { run: save, status: saveStatus } = usePromise<void, void>(upperSave)
if (saveStatus === UsePromiseStatus.PENDING) {
return (
<Tool
aria-label={t("toolToggleEditingSaving")}
disabled
className={cursor.loadingBlocking}
>
<FestaIcon icon={faAsterisk} spin />
</Tool>
)
}
2022-07-19 18:25:11 +00:00
else if (editing) {
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-19 18:25:11 +00:00
setEditing(false)
2022-07-16 17:21:42 +00:00
}}
className={mood.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={() => {
2022-07-19 18:25:11 +00:00
setEditing(true)
2022-07-16 17:21:42 +00:00
}}
2022-06-11 03:08:49 +00:00
>
<FestaIcon icon={faPencil} />
</Tool>
)
}
}