Skip to content

Commit

Permalink
lol
Browse files Browse the repository at this point in the history
  • Loading branch information
ishaan812 committed Jun 30, 2024
1 parent 606f7e4 commit fee40d0
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 28 deletions.
37 changes: 18 additions & 19 deletions packages/nextjs/app/api/orchestrator/[frameId]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,30 @@ async function getResponse(req: NextRequest): Promise<NextResponse> {
const attestation = await createAttestation(txnId);
console.log("attestation", attestation);
}
// const state = JSON.parse(decodeURIComponent(body.untrustedData?.state as string));
// let stateUpdate
// if (state) {
// console.log("state", state);
// // Creating Analytics for the frame asynchronously
// storeAnalytics(body, state).catch(err => console.error("Error Saving Analytics", err));
// // Adding State for Button Press and Inputted Text on last frame
// state.frame_id = frameId;
// stateUpdate = {
// ...state,
// [`${frameId}ButtonPressed`]: body.untrustedData.buttonIndex,
// [`${frameId}InputtedText`]: body.untrustedData.inputText,
// };
// }
const state = JSON.parse(decodeURIComponent(body.untrustedData?.state as string));
let stateUpdate;
if (state) {
// Creating Analytics for the frame asynchronously
storeAnalytics(body, state).catch(err => console.error("Error Saving Analytics", err));
// Adding State for Button Press and Inputted Text on last frame
state.frame_id = frameId;
stateUpdate = {
...state,
[`${frameId}ButtonPressed`]: body.untrustedData.buttonIndex,
[`${frameId}InputtedText`]: body.untrustedData.inputText,
};
}

const dbFrame = await getFrameAtServer(frameId);
if (!dbFrame) {
return new NextResponse(JSON.stringify({ message: "Frame not found" }), { status: 404 });
}
const nextFrame = dbFrame.frameJson;
// if (state) {
// nextFrame.state = {
// ...stateUpdate
// };
// }
if (state) {
nextFrame.state = {
...stateUpdate,
};
}

return new NextResponse(getFrameHtmlResponse(nextFrame));
}
Expand Down
1 change: 1 addition & 0 deletions packages/nextjs/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const txFrame = {
input: {
text: "Enter No of items",
},
state: {},
} as FrameMetadataType;

export const emailFrame = {
Expand Down
29 changes: 28 additions & 1 deletion packages/nextjs/services/frames.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EAS, SchemaEncoder } from "@ethereum-attestation-service/eas-sdk";
import { ethers } from "ethers";
import { initJourneyWithFrames } from "./frames/initScript";
import { APP_URL } from "~~/constants";
import { Frame, Journey } from "~~/types/commontypes";

Expand Down Expand Up @@ -35,6 +36,7 @@ export const removeUrl = (url: string) => {
if (!url) return "";
return url.replace(`${APP_URL}/api/orchestrator/`, "");
};

export const createFrame = async (frame: Omit<Frame, "_id">) => {
try {
const response = await fetch(`/api/frame`, {
Expand Down Expand Up @@ -65,7 +67,14 @@ export const createJourney = async (journey: Partial<Journey>) => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json();
const data = await response.json();
const newJourney = await initJourneyWithFrames(
data._id as string,
journey.price as string,
journey.desc as string,
journey.image as string,
);
return newJourney;
} catch (error: any) {
console.error(error);
throw new Error(error.message);
Expand Down Expand Up @@ -122,3 +131,21 @@ export async function createAttestation(txnId: string) {
console.log("attestation", attestation);
return attestation;
}
export const saveJourney = async (journey: Partial<Journey>) => {
try {
const response = await fetch(`/api/journey/${journey._id}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(journey),
});
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json();
} catch (error: any) {
console.error(error);
throw new Error(error.message);
}
};
146 changes: 138 additions & 8 deletions packages/nextjs/services/frames/frameGetters.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,138 @@
import { FrameMetadataType } from "@coinbase/onchainkit";
import { DEFAULT_FRAME } from "~~/constants";

export const GetDefaultFrame = async (journey_id: string) => {
let frame: FrameMetadataType = DEFAULT_FRAME;
frame.state.journey_id = journey_id;
return frame;
};
import { FrameMetadataType } from "@coinbase/onchainkit";
import { APP_URL, DEFAULT_FRAME, txFrame } from "~~/constants";

export const GetDefaultFrame = async (journey_id: string) => {
const frame: FrameMetadataType = DEFAULT_FRAME;
if (frame.state) {
frame.state.journey_id = journey_id;
}
return frame;
};

export const GetProductFrame = async (
journey_id: string,
frame_id: string,
frame_id_2: string,
frame_id_3: string,
productImage: any,
) => {
const PRODUCT_FRAME: FrameMetadataType = {
buttons: [
{
action: "post",
target: APP_URL + "/api/orchestrator/" + frame_id_2,
label: "Get Details",
},
{
action: "post",
target: APP_URL + "/api/orchestrator/" + frame_id_3,
label: "Buy Now",
},
],
image: {
src: productImage,
},
state: {
journey_id: journey_id,
frame_id: frame_id,
},
};
return PRODUCT_FRAME;
};

export const GetDescriptionFrame = async (
journey_id: string,
frame_id: string,
next_frame_id: string,
productDesc: string,
) => {
const DESCRIPTION_FRAME: FrameMetadataType = {
buttons: [
{
action: "post",
target: APP_URL + "/api/orchestrator/" + next_frame_id,
label: "Buy Now",
},
],
image: {
src: "https://via.placeholder.com/150",
},
state: {
journey_id: journey_id,
frame_id: frame_id,
},
};
return DESCRIPTION_FRAME;
};

export const GetEmailFrame = async (
journey_id: string,
frame_id: string,
next_frame_id: string,
productImage: string,
) => {
const EMAIL_FRAME: FrameMetadataType = {
buttons: [
{
action: "post",
target: APP_URL + "/api/orchestrator/" + next_frame_id,
label: "Next",
},
],
image: {
src: productImage,
},
state: {
journey_id: journey_id,
frame_id: frame_id,
},
input: {
text: "Enter your email address",
},
};
return EMAIL_FRAME;
};

export const GetBuyFrame = async (journey_id: string, frame_id: string, next_frame_id: string) => {
const newTxFrame = {
buttons: [
{
action: "tx",
label: "Buy",
postUrl: `${APP_URL}/api/orchestrator/` + next_frame_id,
target: `${APP_URL}/api/orchestrator/tx`,
},
],
image: {
src: `https://amber-causal-cougar-937.mypinata.cloud/ipfs/QmafH4oZDZWFynGyK9gHVvPRFTTFFrbwYwGQNps4FDLky2`,
},
input: {
text: "Enter No of items",
},
state: {
journey_id: journey_id,
frame_id: frame_id,
},
} as FrameMetadataType;
return newTxFrame;
};

export const GetSuccessFrame = async (journey_id: string, frame_id: string) => {
const SUCCESS_FRAME: FrameMetadataType = {
image: {
src: "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTW_BNf9b9fUN735sATwS1OfxlfV-LD9RhVMA&s",
},
state: {
journey_id: journey_id,
frame_id: frame_id,
},
buttons: [
{
action: "link",
target: APP_URL,
label: "Finish",
},
],
};
return SUCCESS_FRAME;
};
76 changes: 76 additions & 0 deletions packages/nextjs/services/frames/initScript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { createFrame, saveFrame, saveJourney } from "../frames";
import {
GetBuyFrame,
GetDescriptionFrame,
GetEmailFrame,
GetProductFrame,
GetQuantityFrame,
GetSuccessFrame,
} from "./frameGetters";
import { FrameMetadataType } from "@coinbase/onchainkit";
import { emailFrame } from "~~/constants";

export const initJourneyWithFrames = async (
journeyId: string,
productPrice: string,
productDesc: string,
productImage: string,
) => {
const Frames = [];
for (let i = 0; i <= 4; i++) {
const frameBody: FrameMetadataType = {
image: "https://via.placeholder.com/150",
};
const Frame = await createFrame({
name: "Frame",
frameJson: frameBody,
});
Frames.push(Frame._id);
}
// First Frame
const ProductFrameJson = await GetProductFrame(journeyId, Frames[0], Frames[1], Frames[2], productImage);
const status = await saveFrame({
_id: Frames[0],
name: "Product Frame",
frameJson: ProductFrameJson,
});
console.log("Product Frame Status", status);
// Second Frame
const DescriptionFrameJson = await GetDescriptionFrame(journeyId, Frames[1], Frames[2], productDesc);
const desStatus = await saveFrame({
_id: Frames[1],
name: "Description Frame",
frameJson: DescriptionFrameJson,
});
console.log("Description Frame Status", desStatus);
// Third Frame
const emailFrameJson = await GetEmailFrame(journeyId, Frames[2], Frames[3], productImage);
const emailFrameStatus = await saveFrame({
_id: Frames[2],
name: "Email Frame",
frameJson: emailFrameJson,
});
console.log("Email Frame Status", emailFrameStatus);
// Fourth Frame
const BuyFrameJson = await GetBuyFrame(journeyId, Frames[3], Frames[4]);
const buyStatus = await saveFrame({
_id: Frames[3],
name: "Buy Frame",
frameJson: BuyFrameJson,
});
console.log("Buy Frame Status", buyStatus);
//Fifth Frame
const SuccessFrameJson = await GetSuccessFrame(journeyId, Frames[4]);
const sucStatus = await saveFrame({
_id: Frames[4],
name: "Success Frame",
frameJson: SuccessFrameJson,
});
console.log("Success Frame Status", sucStatus);
// Saving to journey
const journey = await saveJourney({
_id: journeyId,
frames: Frames,
});
return journey;
};

0 comments on commit fee40d0

Please sign in to comment.