Refactor GalleryContext.submit() to not take a callback.
rodzic
0c98010e20
commit
6bfd84add6
|
@ -158,7 +158,7 @@ export const FirebaseGalleryProvider: React.FC<{}> = ({ children }) => {
|
|||
lastRefresh,
|
||||
lastSubmission,
|
||||
submitStatus,
|
||||
submit(props, onSuccess) {
|
||||
submit(props) {
|
||||
if (!(appCtx && submitStatus === "idle")) return;
|
||||
|
||||
const doc: FirebaseCompositionDocument = {
|
||||
|
@ -174,7 +174,6 @@ export const FirebaseGalleryProvider: React.FC<{}> = ({ children }) => {
|
|||
setSubmitStatus("idle");
|
||||
setCompositions([comp, ...compositions]);
|
||||
setLastSubmission(comp);
|
||||
onSuccess(docRef.id);
|
||||
})
|
||||
.catch((e) => {
|
||||
setSubmitStatus("error");
|
||||
|
|
|
@ -54,10 +54,7 @@ export interface GalleryContext {
|
|||
* If already in the process of submitting a composition, this
|
||||
* will do nothing.
|
||||
*/
|
||||
submit(
|
||||
composition: Omit<GalleryComposition, "id" | "createdAt">,
|
||||
onSuccess: (id: string) => void
|
||||
): void;
|
||||
submit(composition: Omit<GalleryComposition, "id" | "createdAt">): void;
|
||||
|
||||
/** The most recent submission made via `submit()`, if any. */
|
||||
lastSubmission?: GalleryComposition;
|
||||
|
|
|
@ -52,28 +52,27 @@ const PublishWidget: React.FC<GalleryWidgetProps> = (props) => {
|
|||
const user = assertNotNull(authCtx.loggedInUser, "User must be logged in");
|
||||
const galleryCtx = useContext(GalleryContext);
|
||||
const [title, setTitle] = useState("");
|
||||
const [publishedId, setPublishedId] = useState("");
|
||||
const [lastSerializedValue, setLastSerializedValue] = useState("");
|
||||
const handlePublish = () => {
|
||||
galleryCtx.submit(
|
||||
{
|
||||
title,
|
||||
kind: props.kind,
|
||||
serializedValue: props.serializeValue(),
|
||||
owner: user.id,
|
||||
ownerName: user.name,
|
||||
},
|
||||
setPublishedId
|
||||
);
|
||||
const serializedValue = props.serializeValue();
|
||||
setLastSerializedValue(serializedValue);
|
||||
galleryCtx.submit({
|
||||
title,
|
||||
kind: props.kind,
|
||||
serializedValue,
|
||||
owner: user.id,
|
||||
ownerName: user.name,
|
||||
});
|
||||
};
|
||||
const isSubmitting = galleryCtx.submitStatus === "submitting";
|
||||
|
||||
if (galleryCtx.lastSubmission?.id === publishedId) {
|
||||
if (galleryCtx.lastSubmission?.serializedValue === lastSerializedValue) {
|
||||
return (
|
||||
<>
|
||||
<p>Your composition "{title}" has been published!</p>
|
||||
<button
|
||||
onClick={() => {
|
||||
setPublishedId("");
|
||||
setLastSerializedValue("");
|
||||
setTitle("");
|
||||
}}
|
||||
>
|
||||
|
|
Ładowanie…
Reference in New Issue