Merge branch 'dev' into feat/um-76-turn-off-ui

This commit is contained in:
Brian Cao 2022-10-25 16:24:47 -07:00
commit 1d808f1b1f
8 changed files with 33 additions and 25 deletions

View File

@ -1,6 +1,5 @@
import { useState } from 'react';
import { defineMessages, useIntl } from 'react-intl';
import { useRouter } from 'next/router';
import Page from 'components/layout/Page';
import PageHeader from 'components/layout/PageHeader';
import WebsiteList from 'components/pages/WebsiteList';
@ -16,10 +15,7 @@ const messages = defineMessages({
more: { id: 'label.more', defaultMessage: 'More' },
});
export default function Dashboard() {
const router = useRouter();
const { id } = router.query;
const userId = id?.[0];
export default function Dashboard({ userId }) {
const dashboard = useDashboard();
const { showCharts, limit, editing } = dashboard;
const [max, setMax] = useState(limit);

View File

@ -29,13 +29,15 @@ export default function AccountSettings() {
const Checkmark = ({ isAdmin }) => (isAdmin ? <Icon icon={<Check />} size="medium" /> : null);
const DashboardLink = row => (
<Link href={`/dashboard/${row.userId}/${row.username}`}>
const DashboardLink = row => {
return (
<Link href={`/dashboard/${row.accountUuid}/${row.username}`}>
<a>
<Icon icon={<LinkIcon />} />
</a>
</Link>
);
};
const Buttons = row => (
<ButtonLayout align="right">

View File

@ -21,9 +21,9 @@ export default async (req, res) => {
if (req.method === 'POST') {
const { username, password, account_uuid } = req.body;
const accountByUsername = await getAccount({ username });
const account = await getAccount({ username });
if (accountByUsername) {
if (account) {
return badRequest(res, 'Account already exists');
}

View File

@ -10,7 +10,7 @@ export default async (req, res) => {
if (req.method === 'GET') {
const { userId } = req.auth;
const websites = await getUserWebsites(userId);
const websites = await getUserWebsites({ userId });
const ids = websites.map(({ websiteUuid }) => websiteUuid);
const token = createToken({ websites: ids }, secret());
const data = await getRealtimeData(ids, subMinutes(new Date(), 30));

View File

@ -6,15 +6,16 @@ import { uuid } from 'lib/crypto';
export default async (req, res) => {
await useAuth(req, res);
const { userId: currentUserId, isAdmin, accountUuid } = req.auth;
const { user_id, include_all } = req.query;
const { userId: currentUserId, isAdmin } = req.auth;
const accountUuid = user_id || req.auth.accountUuid;
let account;
if (accountUuid) {
account = await getAccount({ accountUuid: accountUuid });
account = await getAccount({ accountUuid });
}
const userId = account ? account.id : +user_id;
const userId = account ? account.id : user_id;
if (req.method === 'GET') {
if (userId && userId !== currentUserId && !isAdmin) {
@ -24,7 +25,7 @@ export default async (req, res) => {
const websites =
isAdmin && include_all
? await getAllWebsites()
: await getUserWebsites(userId || currentUserId);
: await getUserWebsites({ userId: account.id });
return ok(res, websites);
}

View File

@ -2,17 +2,27 @@ import React from 'react';
import Layout from 'components/layout/Layout';
import Dashboard from 'components/pages/Dashboard';
import useRequireLogin from 'hooks/useRequireLogin';
import { useRouter } from 'next/router';
import useUser from 'hooks/useUser';
export default function DashboardPage() {
const {
query: { id },
isReady,
asPath,
} = useRouter();
const { loading } = useRequireLogin();
const user = useUser();
if (loading) {
if (!user || !isReady || loading) {
return null;
}
const userId = id?.[0];
return (
<Layout>
<Dashboard />
<Dashboard key={asPath} userId={user.id || userId} />
</Layout>
);
}

View File

@ -14,6 +14,7 @@ export async function getAccounts() {
isAdmin: true,
createdAt: true,
updatedAt: true,
accountUuid: true,
},
});
}

View File

@ -1,10 +1,8 @@
import prisma from 'lib/prisma';
export async function getUserWebsites(userId) {
export async function getUserWebsites(where) {
return prisma.client.website.findMany({
where: {
userId,
},
where,
orderBy: {
name: 'asc',
},