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

Update event payloads #14

Merged
merged 9 commits into from
Jan 4, 2023
Merged
Changes from 1 commit
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
106 changes: 44 additions & 62 deletions src/classes/VendorCommunicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,111 +14,81 @@ export enum VendorEvent {
UnauthorizedToken = 'UnauthorizedToken',
}

interface AddToCartMetadata {
source?: string;
title: string;
brand: string;
style: string;
color: string;
thumbnailUrl: string;
area?: number;
price?: number;
retailPrice?: number;
}

interface AddToCartPayload {
schema: string;
interface CommonPayload {
Copy link
Collaborator

Choose a reason for hiding this comment

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

@NothingButThyme I took a pass at creating general/common interfaces that are extended for specific events. This commit is a proposal for us to discuss, not intended to unilaterally override the commit you pushed up (didn't think a whole separate PR for it was warranted). Thoughts appreciated!

The main point to get across here is the requirement for token and customerId be included in the majority of event payloads (excluding non-customer-centric events like tracking) so we can validate the token and make sure it's intended for the right customer.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The changes look good. I don't have any objections.

I am curious what future-proofing is being accomplished by CommonMedata vs CabinetsMetadata, because 'source' and 'area' seem like they would apply to other things besides cabinets.

message?: string;
token: string;
customerId: string;
projectId: string;
versionId: string;
metadata: AddToCartMetadata;
bom?: any;
}

interface DesignerMetadata {
source?: string;
interface CommonMetadata {
title: string;
brand: string;
style: string;
color: string;
thumbnailUrl: string;
price: number;
retailPrice?: number;
thumbnailUrl?: string;
[key: string]: string | number | undefined;
}

interface CabinetsMetadata extends CommonMetadata {
source?: string;
area?: number;
}

interface ConsultDesignerPayload {
interface SnapshotPayload<M extends CommonMetadata> extends CommonPayload {
schema: string;
token: string;
customerId: string;
projectId: string;
versionId: string;
metadata: ConsultDesignerMetadata;
bom?: any;
}

interface TrackingEvent {
actionName: string;
actionData: Record<string, unknown>;
metadata: M;
bom: string;
}

interface ProjectSavedPayload {
message: string;
customerId: string;
interface ProjectSavedPayload extends CommonPayload {
projectId: string;
versionId: string;
versionId?: string;
}

interface ProjectDeletedPayload {
message: string;
customerId: string;
interface ProjectDeletedPayload extends CommonPayload {
projectId: string;
}

interface DirtyStateChangedPayload {
message: string;
interface DirtyStateChangedPayload extends CommonPayload {
projectId: string;
isDirty: boolean;
}

interface iFrameRedirectPayload {
message: string;
interface iFrameRedirectPayload extends CommonPayload {
app: string;
key: string;
}

interface TokenRefreshRequestedPayload {
message: string;
interface TokenRefreshRequestedPayload extends CommonPayload {
requestId: string;
}

interface TrackingEventPayload {
actionName: string;
actionData: Record<string, unknown>;
}

export class VendorCommunicator extends Communicator {
constructor(origin: string) {
super(origin);
this.target = window.parent;
this.origin = origin;
}

addToCart(payload: AddToCartPayload): void {
this.post({type: VendorEvent.AddToCart, payload});
}

appInitialized(message: string): void {
this.post({type: VendorEvent.AddToCart, payload: message});
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I just noticed this should be type: VendorEvent.AppInitialized

}

contactDesigner(payload: ConsultDesignerPayload): void {
this.post({type: VendorEvent.ContactDesigner, payload});
}

dirtyStateChanged(payload: DirtyStateChangedPayload): void {
this.post({type: VendorEvent.DirtyStateChanged, payload});
addToCart(payload: SnapshotPayload<CabinetsMetadata>): void {
this.post({type: VendorEvent.AddToCart, payload});
}

iframeLoaded(message: string): void {
this.post({type: VendorEvent.iFrameLoaded, payload: message});
}

iframeRedirect(payload: iFrameRedirectPayload): void {
this.post({type: VendorEvent.iFrameRedirect, payload});
contactDesigner(payload: SnapshotPayload<CabinetsMetadata>): void {
this.post({type: VendorEvent.ContactDesigner, payload});
}

projectSaved(payload: ProjectSavedPayload): void {
Expand All @@ -129,8 +99,8 @@ export class VendorCommunicator extends Communicator {
this.post({type: VendorEvent.ProjectDeleted, payload});
}

trackEvent(payload: TrackingEvent): void {
this.post({type: VendorEvent.TrackingEvent, payload});
dirtyStateChanged(payload: DirtyStateChangedPayload): void {
this.post({type: VendorEvent.DirtyStateChanged, payload});
}

tokenRefreshRequested(payload: TokenRefreshRequestedPayload): void {
Expand All @@ -140,4 +110,16 @@ export class VendorCommunicator extends Communicator {
unauthorizedToken(error: string): void {
this.post({type: VendorEvent.UnauthorizedToken, payload: error});
}

iframeLoaded(message: string): void {
this.post({type: VendorEvent.iFrameLoaded, payload: message});
}

iframeRedirect(payload: iFrameRedirectPayload): void {
this.post({type: VendorEvent.iFrameRedirect, payload});
}

trackEvent(payload: TrackingEventPayload): void {
this.post({type: VendorEvent.TrackingEvent, payload});
}
}