From ff53629b8e58293def41a3a41d9555ff4469bc81 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Tue, 28 Jun 2022 17:31:11 +0100 Subject: [PATCH] feat: crossbell renovation - settings general page --- src/models/page.model.ts | 4 +- src/models/site.model.ts | 168 ++++++++---------- src/pages/dashboard/[subdomain]/editor.tsx | 2 +- .../[subdomain]/settings/general.tsx | 50 +++--- 4 files changed, 106 insertions(+), 118 deletions(-) diff --git a/src/models/page.model.ts b/src/models/page.model.ts index 9e8bd762..4bf8b970 100644 --- a/src/models/page.model.ts +++ b/src/models/page.model.ts @@ -71,7 +71,7 @@ export async function createOrUpdatePage( source: 'Crossbell Note', identity: input.siteId, platform: 'Crossbell', - action: input.pageId ? 'add' : 'update', + action: input.pageId ? 'update' : 'add', }, { ...(input.pageId && { id: input.pageId }), ...(input.title && { title: input.title }), @@ -155,7 +155,6 @@ export async function getPagesBySite( platform: 'Crossbell', limit: input.take || 100, }); - console.log(pages?.list.length, visibility) if (pages?.list) { switch (visibility) { @@ -169,6 +168,7 @@ export async function getPagesBySite( pages.list = pages.list.filter(page => +new Date(page.date_published) > +new Date() && page.date_published !== new Date('9999-01-01').toISOString()) break } + pages.list = pages.list.filter(page => page.tags?.includes(input.type)) pages.total = pages.list.length pages.list = await Promise.all(pages?.list.map(async (page) => { diff --git a/src/models/site.model.ts b/src/models/site.model.ts index 3af41697..a9ebecf5 100644 --- a/src/models/site.model.ts +++ b/src/models/site.model.ts @@ -58,24 +58,20 @@ export const getUserSites = async (address: string) => { } export const getSite = async (input: string) => { - const site = isUUID(input) - ? await prismaRead.site.findUnique({ - where: { - id: input, - }, - }) - : await prismaRead.site.findUnique({ - where: { - subdomain: input, - }, - }) + if (unidata) { + const profiles = await unidata.profiles.get({ + source: 'Crossbell Profile', + identity: input, + platform: 'Crossbell', + }); - if (!site || site.deletedAt) { - throw new Error(`Site not found`) - } + const site = profiles.list?.sort((a, b) => +new Date(b.date_updated || 0) - +new Date(a.date_updated || 0))?.[0] - return site as Omit & { - navigation: SiteNavigationItem[] | null + if (!site) return null + + return site + } else { + return null } } @@ -98,45 +94,31 @@ export const getSubscription = async (data: { } export async function updateSite( - gate: Gate, payload: { site: string name?: string description?: string icon?: string | null subdomain?: string - navigation?: SiteNavigationItem[] + navigation?: SiteNavigationItem[] // TODO }, ) { - const site = await getSite(payload.site) - - if (!gate.allows({ type: "can-update-site", site })) { - throw gate.permissionError() - } - - if (payload.subdomain) { - await checkSubdomain({ - subdomain: payload.subdomain, - updatingSiteId: site.id, + if (unidata) { + return await unidata.profiles.set({ + source: 'Crossbell Profile', + identity: payload.site, + platform: 'Crossbell', + }, { + ...(payload.name && { name: payload.name }), + ...(payload.description && { bio: payload.description }), + ...(payload.icon && { avatars: [payload.icon] }), + ...(payload.subdomain && { username: payload.subdomain }), }) - } - - const updated = await prismaPrimary.site.update({ - where: { - id: site.id, - }, - data: { - name: payload.name, - subdomain: payload.subdomain, - description: payload.description, - icon: payload.icon, - navigation: payload.navigation, - }, - }) - - return { - site: updated, - subdomainUpdated: updated.subdomain !== site.subdomain, + } else { + return { + code: 1, + message: 'Unidata is not found', + } } } @@ -202,55 +184,55 @@ export async function subscribeToSite( } }, ) { - const { newUser } = input - if (newUser) { - // Create the login token instead - sendLoginEmail({ - email: newUser.email, - url: newUser.url, - toSubscribeSiteId: input.siteId, - }) - return - } + // const { newUser } = input + // if (newUser) { + // // Create the login token instead + // sendLoginEmail({ + // email: newUser.email, + // url: newUser.url, + // toSubscribeSiteId: input.siteId, + // }) + // return + // } - const user = gate.getUser(true) + // const user = gate.getUser(true) - const site = await getSite(input.siteId) - const subscription = await getSubscription({ - userId: user.id, - siteId: site.id, - }) - if (!subscription) { - await prismaPrimary.membership.create({ - data: { - role: MembershipRole.SUBSCRIBER, - user: { - connect: { - id: user.id, - }, - }, - site: { - connect: { - id: site.id, - }, - }, - config: { - email: input.email, - }, - }, - }) - } else { - await prismaPrimary.membership.update({ - where: { - id: subscription.id, - }, - data: { - config: { - email: input.email, - }, - }, - }) - } + // const site = await getSite(input.siteId) + // const subscription = await getSubscription({ + // userId: user.id, + // siteId: site.id, + // }) + // if (!subscription) { + // await prismaPrimary.membership.create({ + // data: { + // role: MembershipRole.SUBSCRIBER, + // user: { + // connect: { + // id: user.id, + // }, + // }, + // site: { + // connect: { + // id: site.id, + // }, + // }, + // config: { + // email: input.email, + // }, + // }, + // }) + // } else { + // await prismaPrimary.membership.update({ + // where: { + // id: subscription.id, + // }, + // data: { + // config: { + // email: input.email, + // }, + // }, + // }) + // } } export async function unsubscribeFromSite( diff --git a/src/pages/dashboard/[subdomain]/editor.tsx b/src/pages/dashboard/[subdomain]/editor.tsx index ed1a1b1a..c342f6aa 100644 --- a/src/pages/dashboard/[subdomain]/editor.tsx +++ b/src/pages/dashboard/[subdomain]/editor.tsx @@ -149,7 +149,7 @@ export default function SubdomainEditor() { setValues({ title: page.title || '', - publishedAt: page.date_published, + publishedAt: page.date_published === new Date('9999-01-01').toISOString() ? new Date().toISOString() : page.date_published, published: page.date_published !== new Date('9999-01-01').toISOString(), excerpt: page.summary?.content || "", }) diff --git a/src/pages/dashboard/[subdomain]/settings/general.tsx b/src/pages/dashboard/[subdomain]/settings/general.tsx index f95b2dc4..0d7d71c9 100644 --- a/src/pages/dashboard/[subdomain]/settings/general.tsx +++ b/src/pages/dashboard/[subdomain]/settings/general.tsx @@ -1,26 +1,30 @@ import { Button } from "~/components/ui/Button" import { Input } from "~/components/ui/Input" import { AvatarForm } from "~/components/dashboard/AvatarForm" -import { useEffect } from "react" +import { useEffect, useState } from "react" import toast from "react-hot-toast" import { SettingsLayout } from "~/components/dashboard/SettingsLayout" import { useRouter } from "next/router" import { DashboardLayout } from "~/components/dashboard/DashboardLayout" import { trpc } from "~/lib/trpc" import { useForm } from "react-hook-form" +import { getSite, updateSite } from "~/models/site.model" +import { Profile } from "unidata.js" export default function SiteSettingsGeneralPage() { const router = useRouter() const subdomain = router.query.subdomain as string - const siteResult = trpc.useQuery(["site", { site: subdomain }], { - enabled: !!subdomain, - refetchOnWindowFocus: false, - refetchOnReconnect: false, - }) - const site = siteResult.data - const updateSite = trpc.useMutation("site.updateSite") + let [site, setSite] = useState(null) + + useEffect(() => { + if (subdomain) { + getSite(subdomain).then((site) => { + setSite(site) + }) + } + }, [subdomain]) const form = useForm({ defaultValues: { @@ -29,28 +33,30 @@ export default function SiteSettingsGeneralPage() { }, }) + let [loading, setLoading] = useState(false) const handleSubmit = form.handleSubmit((values) => { - updateSite.mutate({ + setLoading(true) + updateSite({ site: subdomain, name: values.name, description: values.description, + }).then((result) => { + if (result.code === 0) { + toast.success("Site updated") + } else { + toast.error("Failed to update site" + ": " + result.message) + } + setLoading(false) }) }) useEffect(() => { if (site) { - form.setValue("name", site.name) - form.setValue("description", site.description || "") + form.setValue("name", site.name || "") + form.setValue("description", site.bio || "") } }, [site, form]) - useEffect(() => { - if (updateSite.isSuccess && updateSite.data) { - toast.success("Site updated") - updateSite.reset() - } - }, [updateSite]) - return ( @@ -58,9 +64,9 @@ export default function SiteSettingsGeneralPage() {
)} @@ -80,7 +86,7 @@ export default function SiteSettingsGeneralPage() { />
-