diff --git a/components/settings/WebsiteSettings.js b/components/settings/WebsiteSettings.js index ee23e549..998df40e 100644 --- a/components/settings/WebsiteSettings.js +++ b/components/settings/WebsiteSettings.js @@ -1,5 +1,6 @@ import React, { useState } from 'react'; import { FormattedMessage } from 'react-intl'; +import { useSelector } from 'react-redux'; import classNames from 'classnames'; import Link from 'components/common/Link'; import Table from 'components/common/Table'; @@ -25,6 +26,7 @@ import useFetch from 'hooks/useFetch'; import styles from './WebsiteSettings.module.css'; export default function WebsiteSettings() { + const user = useSelector(state => state.user); const [editWebsite, setEditWebsite] = useState(); const [resetWebsite, setResetWebsite] = useState(); const [deleteWebsite, setDeleteWebsite] = useState(); @@ -33,7 +35,9 @@ export default function WebsiteSettings() { const [showUrl, setShowUrl] = useState(); const [saved, setSaved] = useState(0); const [message, setMessage] = useState(); - const { data } = useFetch(`/api/websites`, {}, [saved]); + const { data } = useFetch(`/api/websites` + (user.is_admin ? '?include_all=true' : ''), {}, [ + saved, + ]); const Buttons = row => ( @@ -55,15 +59,27 @@ export default function WebsiteSettings() { tooltipId={`button-code-${row.website_id}`} onClick={() => setShowCode(row)} /> - - - + - +
{editWebsite && ( }> diff --git a/lib/queries.js b/lib/queries.js index 13f4181d..f54b9267 100644 --- a/lib/queries.js +++ b/lib/queries.js @@ -115,6 +115,29 @@ export async function getUserWebsites(user_id) { ); } +export async function getAllWebsites() { + let data = await runQuery( + prisma.website.findMany({ + orderBy: [ + { + user_id: 'asc', + }, + { + name: 'asc', + }, + ], + include: { + account: { + select: { + username: true, + }, + }, + }, + }), + ); + return data.map(i => ({ ...i, account: i.account.username })); +} + export async function createWebsite(user_id, data) { return runQuery( prisma.website.create({ diff --git a/pages/api/websites.js b/pages/api/websites.js index 6f51754f..589b7315 100644 --- a/pages/api/websites.js +++ b/pages/api/websites.js @@ -1,4 +1,4 @@ -import { getUserWebsites } from 'lib/queries'; +import { getAllWebsites, getUserWebsites } from 'lib/queries'; import { useAuth } from 'lib/middleware'; import { ok, methodNotAllowed, unauthorized } from 'lib/response'; @@ -6,7 +6,7 @@ export default async (req, res) => { await useAuth(req, res); const { user_id: current_user_id, is_admin } = req.auth; - const { user_id } = req.query; + const { user_id, include_all } = req.query; const userId = +user_id; if (req.method === 'GET') { @@ -14,7 +14,10 @@ export default async (req, res) => { return unauthorized(res); } - const websites = await getUserWebsites(userId || current_user_id); + const websites = + is_admin && include_all + ? await getAllWebsites() + : await getUserWebsites(userId || current_user_id); return ok(res, websites); }