Skip to content
This repository has been archived by the owner on Feb 13, 2024. It is now read-only.

Commit

Permalink
feat: publish nostr event
Browse files Browse the repository at this point in the history
  • Loading branch information
abhiShandy committed Mar 23, 2023
1 parent e540683 commit 35f2d0e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 17 deletions.
4 changes: 2 additions & 2 deletions client/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import ReactDOM from "react-dom/client";
import { Home, CreateProduct, Product, Discover } from "./routes";
import { Home, Sell, Product, Discover } from "./routes";
import "./index.css";
import { createBrowserRouter, RouterProvider } from "react-router-dom";
import { QueryClient, QueryClientProvider } from "react-query";
Expand All @@ -16,7 +16,7 @@ const router = createBrowserRouter([
},
{
path: "/sell",
element: <CreateProduct />,
element: <Sell />,
},
{
path: "/products/:productId",
Expand Down
34 changes: 25 additions & 9 deletions client/src/routes/CreateProduct.tsx → client/src/routes/Sell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@ import { useNavigate } from "react-router-dom";
import { createProduct } from "./api/products";
import { getAssetUploadURL, uploadAsset } from "./api/assets";
import { useState } from "react";
import { nsecToNpub } from "./utils/nostr";
import { publishEvent } from "./utils/nostr";

export const CreateProduct = () => {
const Sell = () => {
const navigate = useNavigate();

const [isLoading, setIsLoading] = useState(false);

const mutateProduct = useMutation(createProduct, {
onSuccess: () => {
navigate("/discover");
},
onError: () => {
alert("Error creating product");
},
Expand Down Expand Up @@ -52,6 +49,26 @@ export const CreateProduct = () => {
});
};
thumbnailReader.readAsDataURL(event.image[0]);

const unsignedEvent = {
created_at: Math.trunc(Date.now() / 1000),
kind: 1,
tags: [],
content:
"Checkout my new product on GoodStr:" +
mutateProduct.data?.data.product.id,
};
console.log("unsignedEvent", unsignedEvent);
try {
// @ts-ignore
const signedEvent = await window.nostr.signEvent(unsignedEvent);
console.log("signedEvent", signedEvent);
await publishEvent(signedEvent);
console.log("event published");
navigate("/discover");
} catch (e) {
console.error("sign-error", e);
}
} catch (e) {
console.error("post-error", e);
alert("Error creating product");
Expand All @@ -64,11 +81,10 @@ export const CreateProduct = () => {
<>
<Navbar currentPage="sell" />
<div className="max-w-lg mx-auto mt-8 p-4">
<AddProductForm
onSubmit={onSubmit}
isLoading={mutateProduct.isLoading || isLoading}
/>
<AddProductForm onSubmit={onSubmit} isLoading={isLoading} />
</div>
</>
);
};

export default Sell;
3 changes: 2 additions & 1 deletion client/src/routes/api/products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ type CreateProductInput = {

export const createProduct = ({ product, images }: CreateProductInput) => {
const PRODUCTS_URL = import.meta.env.VITE_BASE_URL + "/products";
return axios.post(PRODUCTS_URL, {

return axios.post<{ product: { id: string } }>(PRODUCTS_URL, {
title: product.title,
description: product.description,
images: [
Expand Down
4 changes: 2 additions & 2 deletions client/src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Home } from "./Home";
import Product from "./Product";
import { CreateProduct } from "./CreateProduct";
import Sell from "./Sell";
import Discover from "./Discover";

export { Home, Product, CreateProduct, Discover };
export { Home, Product, Sell, Discover };
19 changes: 18 additions & 1 deletion client/src/routes/utils/nostr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const nsecToNpub = (nsec: string) => {
return encodeBytes("npub", pubkey);
};

const decode = (nip19: string) => {
export const decode = (nip19: string) => {
const { prefix, words } = bech32.decode(nip19);
let data = new Uint8Array(bech32.fromWords(words));
switch (prefix) {
Expand All @@ -102,3 +102,20 @@ export const encodeBytes = (prefix: string, hex: string): string => {
let words = bech32.toWords(data);
return bech32.encode(prefix, words, 5000);
};

export const publishEvent = async (event: any) => {
const ws = new WebSocket("wss://nostr.thegoodstr.com");
ws.onopen = () => {
ws.send(JSON.stringify(["EVENT", event]));
console.log("NOSTR event sent!");
};
ws.onmessage = (event) => {
console.log(event.data);
};
ws.onclose = () => {
console.log("closed");
};
ws.onerror = () => {
console.log("error");
};
};
8 changes: 6 additions & 2 deletions server/functions/createProduct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,15 @@ export const handler: APIGatewayProxyHandler = async (event) => {
await newProduct.create();

return {
statusCode: 204,
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
},
body: "Success!",
body: JSON.stringify({
product: {
id: newProduct.id,
},
}),
};
} catch (err) {
console.error(err);
Expand Down

0 comments on commit 35f2d0e

Please sign in to comment.