-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPublishButton.tsx
More file actions
48 lines (43 loc) · 1.34 KB
/
PublishButton.tsx
File metadata and controls
48 lines (43 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { HTMLAttributes, useState } from "react";
import { useUpdateProgramMutation } from "../../generated/graphql";
import { useSnackbar } from "../../notifications/SnackbarContext";
import { Button } from "../atomic";
import { useEditor } from "./EditorContext";
type PublishButtonProps = HTMLAttributes<HTMLButtonElement> & {
programId: string;
};
//TODO: Error notice when content fails to publish
const PublishButton = ({ programId, ...props }: PublishButtonProps) => {
const { uploadImagesAndGetHomepage, publishable, setPublishable } =
useEditor();
const [updateProgram] = useUpdateProgramMutation();
const [loading, setLoading] = useState(false);
const { setSnackbarMessage } = useSnackbar();
const save = async () => {
setLoading(true);
const contentState = await uploadImagesAndGetHomepage!();
updateProgram({
variables: { data: { homepage: contentState }, programId },
})
.then(() => {
setLoading(false);
setPublishable!(false);
setSnackbarMessage({ text: "Homepage saved!" });
})
.catch((err) => {
console.error("Fail: ", err);
setLoading(false);
});
};
return (
<Button
size="small"
disabled={!publishable || loading}
onClick={save}
{...props}
>
Publish
</Button>
);
};
export default PublishButton;