Skip to content

Commit

Permalink
fix: move fetchMessage function into page component
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteAtATime committed Dec 20, 2024
1 parent adf5b47 commit ec80b95
Showing 1 changed file with 41 additions and 41 deletions.
82 changes: 41 additions & 41 deletions packages/nextjs/app/view/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,47 @@ const ViewSignature: NextPage<{ params: { id: string } }> = ({ params }: { param
},
});

const fetchMessage = async () => {
try {
setIsLoading(true);
setError(null);

const res = await fetch(`/api/signatures/${id}`);

if (!res.ok) {
throw new Error(`HTTP error! status: ${res.status}`);
}

const { message, signatures } = (await res.json()) as {
message: typeof messagesTable.$inferSelect;
signatures: (typeof signaturesTable.$inferSelect)[];
};

if (!message) {
throw new Error("Message not found");
}

if (message.type === "text") {
setMessage(message.message);
} else if (message.type === "typed_data") {
try {
setTypedData(JSON.parse(message.message));
} catch (e) {
throw new Error("Invalid typed data format");
}
} else {
throw new Error("Invalid message type");
}

setSignatures(signatures?.map(s => s.signature) ?? []);
setAddresses(signatures?.map(s => s.signer) ?? []);
} catch (err) {
setError(err instanceof Error ? err.message : "An unknown error occurred");
} finally {
setIsLoading(false);
}
};

const submitSignature = async (signature: string) => {
try {
setIsSubmitting(true);
Expand Down Expand Up @@ -86,47 +127,6 @@ const ViewSignature: NextPage<{ params: { id: string } }> = ({ params }: { param
};

useEffect(() => {
const fetchMessage = async () => {
try {
setIsLoading(true);
setError(null);

const res = await fetch(`/api/signatures/${id}`);

if (!res.ok) {
throw new Error(`HTTP error! status: ${res.status}`);
}

const { message, signatures } = (await res.json()) as {
message: typeof messagesTable.$inferSelect;
signatures: (typeof signaturesTable.$inferSelect)[];
};

if (!message) {
throw new Error("Message not found");
}

if (message.type === "text") {
setMessage(message.message);
} else if (message.type === "typed_data") {
try {
setTypedData(JSON.parse(message.message));
} catch (e) {
throw new Error("Invalid typed data format");
}
} else {
throw new Error("Invalid message type");
}

setSignatures(signatures?.map(s => s.signature) ?? []);
setAddresses(signatures?.map(s => s.signer) ?? []);
} catch (err) {
setError(err instanceof Error ? err.message : "An unknown error occurred");
} finally {
setIsLoading(false);
}
};

fetchMessage();
}, [id]);

Expand Down

0 comments on commit ec80b95

Please sign in to comment.