feat: crossbell renovation - new site page

This commit is contained in:
DIYgod 2022-06-30 20:11:35 +01:00
parent d7d8a80fad
commit 7d3415c57a
No known key found for this signature in database
GPG Key ID: D159328F47A80DCA
4 changed files with 46 additions and 64 deletions

View File

@ -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

View File

@ -43,8 +43,8 @@ export const SitePage: React.FC<{
contentHTML={page.body?.content || ""}
className="my-10"
/>
<div className="bg-gray-100 rounded-lg py-8 px-5 break-all">
<p className="text-gray-700 mb-4">🔔 This post has been permanently stored on-chain and signed by its creator.</p>
<div className="bg-gray-50 rounded-lg py-8 px-5 break-all">
<p className="text-gray-700 mb-4">🔔 This {page.tags?.includes("post") ? "post" : "page"} has been permanently stored on-chain and signed by its creator.</p>
<ul className="text-gray-600 text-sm leading-6">
<li className="mt-2">
<div className="font-bold">Crossbell Transaction</div>

View File

@ -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(

View File

@ -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<string>('')
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<boolean>(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() {
</Link>
<div>
<div className="text-zinc-400">Logged in as:</div>
<div>{viewer.data?.email}</div>
<div>{address}</div>
</div>
</header>
<div className="max-w-sm mx-auto mt-20">
@ -88,7 +97,7 @@ export default function NewSitePage() {
/>
</div>
<div>
<Button type="submit" isBlock isLoading={createSite.isLoading}>
<Button type="submit" isBlock isLoading={loading}>
Create
</Button>
</div>