From a3f73826738bf639da8a23c8ba698234249da50b Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Sat, 10 Feb 2024 23:47:26 -0800 Subject: [PATCH] Added additional logic for handling website transfers. --- .../websites/[websiteId]/WebsiteData.tsx | 63 ++++++++++--------- .../websites/[websiteId]/WebsiteSettings.tsx | 11 +++- src/components/messages.ts | 6 +- src/lib/auth.ts | 8 --- 4 files changed, 43 insertions(+), 45 deletions(-) diff --git a/src/app/(main)/settings/websites/[websiteId]/WebsiteData.tsx b/src/app/(main)/settings/websites/[websiteId]/WebsiteData.tsx index 410f1783..c43b29bf 100644 --- a/src/app/(main)/settings/websites/[websiteId]/WebsiteData.tsx +++ b/src/app/(main)/settings/websites/[websiteId]/WebsiteData.tsx @@ -1,53 +1,54 @@ -import { Button, Modal, ModalTrigger, ActionForm, useToasts } from 'react-basics'; +import { Button, Modal, ModalTrigger, ActionForm } from 'react-basics'; import { useRouter } from 'next/navigation'; -import { useMessages, useModified, useTeamUrl } from 'components/hooks'; +import { useLogin, useMessages, useModified, useTeamUrl } from 'components/hooks'; import WebsiteDeleteForm from './WebsiteDeleteForm'; import WebsiteResetForm from './WebsiteResetForm'; import WebsiteTransferForm from './WebsiteTransferForm'; +import { ROLES } from 'lib/constants'; export function WebsiteData({ websiteId, onSave }: { websiteId: string; onSave?: () => void }) { const { formatMessage, labels, messages } = useMessages(); - const router = useRouter(); - const { showToast } = useToasts(); + const { user } = useLogin(); const { touch } = useModified(); const { teamId, renderTeamUrl } = useTeamUrl(); + const router = useRouter(); + const hasTeams = user?.teams?.length > 0; + const isTeamOwner = + (!teamId && hasTeams) || + (hasTeams && + user?.teams + ?.find(({ id }) => id === teamId) + ?.teamUser.find(({ role, userId }) => role === ROLES.teamOwner && userId === user.id)); - const handleTransfer = () => { + const handleSave = () => { touch('websites'); - + onSave?.(); router.push(renderTeamUrl(`/settings/websites`)); }; const handleReset = async () => { - showToast({ message: formatMessage(messages.saved), variant: 'success' }); onSave?.(); }; - const handleDelete = async () => { - touch('websites'); - - if (teamId) { - router.push(renderTeamUrl('/settings/websites')); - } else { - router.push('/settings/websites'); - } - }; - return ( <> - - - - - {(close: () => void) => ( - - )} - - - + {process.env.cloudMode && ( + + + + + {(close: () => void) => ( + + )} + + + + )} {formatMessage(labels.delete)} {(close: () => void) => ( - + )} diff --git a/src/app/(main)/settings/websites/[websiteId]/WebsiteSettings.tsx b/src/app/(main)/settings/websites/[websiteId]/WebsiteSettings.tsx index 24bf3d02..af7723ae 100644 --- a/src/app/(main)/settings/websites/[websiteId]/WebsiteSettings.tsx +++ b/src/app/(main)/settings/websites/[websiteId]/WebsiteSettings.tsx @@ -1,5 +1,5 @@ import { useState, Key, useContext } from 'react'; -import { Item, Tabs, Button, Text, Icon } from 'react-basics'; +import { Item, Tabs, Button, Text, Icon, useToasts } from 'react-basics'; import Link from 'next/link'; import Icons from 'components/icons'; import PageHeader from 'components/layout/PageHeader'; @@ -12,8 +12,13 @@ import { WebsiteContext } from 'app/(main)/websites/[websiteId]/WebsiteProvider' export function WebsiteSettings({ websiteId, openExternal = false }) { const website = useContext(WebsiteContext); - const { formatMessage, labels } = useMessages(); + const { formatMessage, labels, messages } = useMessages(); const [tab, setTab] = useState('details'); + const { showToast } = useToasts(); + + const handleSave = () => { + showToast({ message: formatMessage(messages.saved), variant: 'success' }); + }; return ( <> @@ -36,7 +41,7 @@ export function WebsiteSettings({ websiteId, openExternal = false }) { {tab === 'details' && } {tab === 'tracking' && } {tab === 'share' && } - {tab === 'data' && } + {tab === 'data' && } ); } diff --git a/src/components/messages.ts b/src/components/messages.ts index f9d518ed..2710e99d 100644 --- a/src/components/messages.ts +++ b/src/components/messages.ts @@ -330,14 +330,14 @@ export const messages = defineMessages({ }, transferWebsite: { id: 'message.transfer-website', - defaultMessage: 'Transfer website ownership to another user or team.', + defaultMessage: 'Transfer website ownership to your account or another team.', }, transferTeamWebsiteToUser: { id: 'message.transfer-team-website-to-user', - defaultMessage: 'Do you want to transfer this website to your account?', + defaultMessage: 'Transfer this website to your account?', }, transferUserWebsiteToTeam: { id: 'message.transfer-user-website-to-team', - defaultMessage: 'Which team do you want to transfer this website to?', + defaultMessage: 'Select the team to transfer this website to.', }, }); diff --git a/src/lib/auth.ts b/src/lib/auth.ts index ee3defea..92ec23bb 100644 --- a/src/lib/auth.ts +++ b/src/lib/auth.ts @@ -102,10 +102,6 @@ export async function canUpdateWebsite({ user }: Auth, websiteId: string) { } export async function canTransferWebsiteToUser({ user }: Auth, websiteId: string, userId: string) { - if (user.isAdmin) { - return true; - } - const website = await loadWebsite(websiteId); if (website.teamId && user.id === userId) { @@ -118,10 +114,6 @@ export async function canTransferWebsiteToUser({ user }: Auth, websiteId: string } export async function canTransferWebsiteToTeam({ user }: Auth, websiteId: string, teamId: string) { - if (user.isAdmin) { - return true; - } - const website = await loadWebsite(websiteId); if (website.userId === user.id) {