From 7d3415c57a9df0c6f249977ea4cef2f29ed53f3d Mon Sep 17 00:00:00 2001 From: DIYgod Date: Thu, 30 Jun 2022 20:11:35 +0100 Subject: [PATCH] feat: crossbell renovation - new site page --- src/components/dashboard/DashboardLayout.tsx | 4 +- src/components/site/SitePage.tsx | 4 +- src/models/site.model.ts | 59 +++++--------------- src/pages/dashboard/new-site.tsx | 43 ++++++++------ 4 files changed, 46 insertions(+), 64 deletions(-) diff --git a/src/components/dashboard/DashboardLayout.tsx b/src/components/dashboard/DashboardLayout.tsx index 936e9883..c831a99c 100644 --- a/src/components/dashboard/DashboardLayout.tsx +++ b/src/components/dashboard/DashboardLayout.tsx @@ -33,8 +33,10 @@ export function DashboardLayout({ getUserSites(viewer.address).then((sites) => { setSubscriptions(sites || []) }) + } else { + router.push("/") } - }, [viewer?.address]) + }, [viewer?.address, router]) const links: { href: string diff --git a/src/components/site/SitePage.tsx b/src/components/site/SitePage.tsx index 34eb4c30..d1debd6b 100644 --- a/src/components/site/SitePage.tsx +++ b/src/components/site/SitePage.tsx @@ -43,8 +43,8 @@ export const SitePage: React.FC<{ contentHTML={page.body?.content || ""} className="my-10" /> -
-

🔔 This post has been permanently stored on-chain and signed by its creator.

+
+

🔔 This {page.tags?.includes("post") ? "post" : "page"} has been permanently stored on-chain and signed by its creator.

  • Crossbell Transaction
    diff --git a/src/models/site.model.ts b/src/models/site.model.ts index 04dfb6c3..9d007cb8 100644 --- a/src/models/site.model.ts +++ b/src/models/site.model.ts @@ -114,54 +114,25 @@ export async function updateSite( } export async function createSite( - gate: Gate, + address: string, payload: { name: string; subdomain: string }, ) { - const user = gate.getUser(true) - await checkSubdomain({ subdomain: payload.subdomain }) - - const navigation: SiteNavigationItem[] = [ - { - id: nanoid(), - label: "About", - url: "/about", - }, - { - id: nanoid(), - label: "Archives", - url: "/archives", - }, - ] - const site = await prismaPrimary.site.create({ - data: { - name: payload.name, - subdomain: payload.subdomain, - navigation, - memberships: { - create: { - role: MembershipRole.OWNER, - user: { - connect: { - id: user.id, - }, - }, - }, + return await unidata.profiles.set({ + source: 'Crossbell Profile', + identity: address, + platform: 'Ethereum', + action: 'add', + }, { + username: payload.subdomain, + name: payload.name, + tags: ['navigation:' + JSON.stringify([ + { + id: nanoid(), + label: "Archives", + url: "/archives", }, - pages: { - create: { - title: "About", - slug: "about", - excerpt: "", - content: `My name is ${payload.name} and I'm a new site.`, - published: true, - publishedAt: new Date(), - type: PageType.PAGE, - }, - }, - }, + ])], }) - - return { site } } export async function subscribeToSite( diff --git a/src/pages/dashboard/new-site.tsx b/src/pages/dashboard/new-site.tsx index 9fd9b35a..f740919e 100644 --- a/src/pages/dashboard/new-site.tsx +++ b/src/pages/dashboard/new-site.tsx @@ -1,6 +1,6 @@ import Link from "next/link" import { useRouter } from "next/router" -import { useEffect } from "react" +import { useEffect, useState } from "react" import { useForm } from "react-hook-form" import toast from "react-hot-toast" import { SEOHead } from "~/components/common/SEOHead" @@ -8,11 +8,22 @@ import { Button } from "~/components/ui/Button" import { Input } from "~/components/ui/Input" import { APP_NAME, OUR_DOMAIN } from "~/lib/env" import { trpc } from "~/lib/trpc" +import { useAccount } from "wagmi" +import { createSite } from "~/models/site.model" export default function NewSitePage() { const router = useRouter() - const viewer = trpc.useQuery(["auth.viewer"]) - const createSite = trpc.useMutation("site.create") + + const [address, setAddress] = useState('') + const { data: wagmiData } = useAccount() + + useEffect(() => { + if (wagmiData?.address) { + setAddress(wagmiData?.address || '') + } else { + router.push("/") + } + }, [wagmiData, router]) const form = useForm({ defaultValues: { @@ -21,19 +32,17 @@ export default function NewSitePage() { }, }) - const handleSubmit = form.handleSubmit((values) => { - createSite.mutate(values) - }) - - useEffect(() => { - if (createSite.isError) { - createSite.reset() - toast.error(createSite.error.message) - } else if (createSite.isSuccess) { - createSite.reset() - router.push(`/dashboard/${createSite.data.subdomain}`) + let [loading, setLoading] = useState(false) + const handleSubmit = form.handleSubmit(async (values) => { + setLoading(true) + const result = await createSite(wagmiData!.address!, values) + setLoading(false) + if (result.code === 0) { + router.push(`/dashboard/${values.subdomain}`) + } else { + toast.error("Failed to create site" + ": " + result.message) } - }, [createSite, router]) + }) return ( <> @@ -59,7 +68,7 @@ export default function NewSitePage() {
    Logged in as:
    -
    {viewer.data?.email}
    +
    {address}
    @@ -88,7 +97,7 @@ export default function NewSitePage() { />
    -