Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added onSuccessAction prop to Upload Widget #487

Merged
merged 6 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/pages/clduploadwidget/basic-usage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const UnsignedUpload = ({ options }) => {
return (
<div className={`grid gap-6 ${resource ? 'grid-cols-2' : 'grid-cols-1'}`}>
<CldUploadWidget
uploadPreset="next-cloudinary-unsigned"
uploadPreset="<Your Upload Preset>"
onSuccess={(result, { widget }) => {
setResource(result?.info);
widget.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { triggerOnIdle } from '../../lib/util';

import {
CldUploadEventAction,
CldUploadEventCallback,
CldUploadWidgetCloudinaryInstance,
CldUploadWidgetProps,
Expand Down Expand Up @@ -57,6 +58,8 @@ const CldUploadWidget = ({

const signed = !!signatureEndpoint;

const { onSuccessAction } = props;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont think this is needed anymore


const [error, setError] = useState<CloudinaryUploadWidgetError | undefined>(undefined);
const [results, setResults] = useState<CloudinaryUploadWidgetResults | undefined>(undefined);
const [isScriptLoading, setIsScriptLoading] = useState(true);
Expand Down Expand Up @@ -251,7 +254,7 @@ const CldUploadWidget = ({
}

if ( typeof uploadResult?.event === 'string' ) {
if ( WIDGET_WATCHED_EVENTS.includes(uploadResult?.event) ) {
if ( WIDGET_WATCHED_EVENTS.includes(uploadResult?.event as string) ) {
setResults(uploadResult);
}

Expand All @@ -264,6 +267,13 @@ const CldUploadWidget = ({
...instanceMethods
});
}

const widgetEventAction = `${widgetEvent}Action` as keyof typeof props;

if ( widgetEventAction && typeof props[widgetEventAction] === 'function' ) {
const action = props[widgetEventAction] as CldUploadEventAction;
uploadResult.event === 'success' && action(uploadResult);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we can remove the 'success' check here right? as long as the function exists and its under the watched events, which at this point i believe both should be true, we should be good, right?

}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ export interface CldUploadWidgetProps {
options?: CloudinaryUploadWidgetOptions;
signatureEndpoint?: URL | RequestInfo;
uploadPreset?: string;
onSuccessAction?: CldUploadEventAction;
onUploadAction?: CldUploadEventAction;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the onUpload function is deprecated and will be removed in a later version (likely a major?), so thinking we probably shouldnt add this as an option. while it will technically work if someone tries because the actions are automatically created, we don't want to document or promote its use as a valid option only for it to be pulled out soon

onAbortAction?: CldUploadEventAction;
onBatchCancelledAction?: CldUploadEventAction;
onCloseAction?: CldUploadEventAction;
onDisplayChangedAction?: CldUploadEventAction;
onPublicIdAction?: CldUploadEventAction;
onQueuesEndAction?: CldUploadEventAction;
onQueuesStartAction?: CldUploadEventAction;
onRetryAction?: CldUploadEventAction;
onShowCompletedAction?: CldUploadEventAction;
onSourceChangedAction?: CldUploadEventAction;
onTagsAction?: CldUploadEventAction;
onUploadAddedAction?: CldUploadEventAction;
}

export type CldUploadWidgetPropsChildren = {
Expand All @@ -45,6 +59,7 @@ export type CldUploadWidgetPropsChildren = {
} & CloudinaryUploadWidgetInstanceMethods;

export type CldUploadEventCallback = (results: CloudinaryUploadWidgetResults, widget: CldUploadEventCallbackWidget) => void;
export type CldUploadEventAction = (results: CloudinaryUploadWidgetResults) => void;
export type CldUploadEventCallbackNoOptions = (results: CloudinaryUploadWidgetResults, widget: CldUploadWidgetWidgetInstance) => void;
export type CldUploadEventCallbackWidgetOnly = (widget: CldUploadWidgetWidgetInstance) => void;
export type CldUploadEventCallbackError = (error: CloudinaryUploadWidgetError, widget: CldUploadEventCallbackWidget) => void;
Expand Down
Loading