Updated save method for websites.

This commit is contained in:
Mike Cao 2022-10-07 17:13:03 -07:00
parent dcf16e1411
commit 2428314f58
3 changed files with 26 additions and 20 deletions

View File

@ -14,6 +14,7 @@ import useApi from 'hooks/useApi';
import useFetch from 'hooks/useFetch'; import useFetch from 'hooks/useFetch';
import useUser from 'hooks/useUser'; import useUser from 'hooks/useUser';
import styles from './WebsiteEditForm.module.css'; import styles from './WebsiteEditForm.module.css';
import { getRandomChars } from 'next-basics';
const initialValues = { const initialValues = {
name: '', name: '',
@ -78,7 +79,10 @@ export default function WebsiteEditForm({ values, onSave, onClose }) {
const [message, setMessage] = useState(); const [message, setMessage] = useState();
const handleSubmit = async values => { const handleSubmit = async values => {
const { website_id } = values; const { website_id, enable_share_url, share_id } = values;
if (enable_share_url) {
values.share_id = share_id || getRandomChars(8);
}
const { ok, data } = await post(website_id ? `/websites/${website_id}` : '/websites', values); const { ok, data } = await post(website_id ? `/websites/${website_id}` : '/websites', values);
if (ok) { if (ok) {
@ -137,7 +141,6 @@ export default function WebsiteEditForm({ values, onSave, onClose }) {
defaultMessage="Enable share URL" defaultMessage="Enable share URL"
/> />
} }
value={null}
/> />
)} )}
</Field> </Field>

View File

@ -1,5 +1,5 @@
import { getRandomChars, methodNotAllowed, ok, unauthorized } from 'next-basics'; import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { deleteWebsite, getWebsite, getWebsiteById, updateWebsite } from 'queries'; import { deleteWebsite, getAccount, getWebsite, updateWebsite } from 'queries';
import { allowQuery } from 'lib/auth'; import { allowQuery } from 'lib/auth';
import { useAuth, useCors } from 'lib/middleware'; import { useAuth, useCors } from 'lib/middleware';
import { validate } from 'uuid'; import { validate } from 'uuid';
@ -25,24 +25,29 @@ export default async (req, res) => {
if (req.method === 'POST') { if (req.method === 'POST') {
await useAuth(req, res); await useAuth(req, res);
const { is_admin: currentUserIsAdmin, user_id: currentUserId } = req.auth; const { is_admin: currentUserIsAdmin, user_id: currentUserId, account_uuid } = req.auth;
const { name, domain, owner, enable_share_url } = req.body; const { name, domain, owner, share_id } = req.body;
let account;
const website = await getWebsiteById(websiteId); if (account_uuid) {
account = await getAccount({ account_uuid });
}
const website = await getWebsite(where);
if (website.user_id !== currentUserId && !currentUserIsAdmin) { if (website.user_id !== currentUserId && !currentUserIsAdmin) {
return unauthorized(res); return unauthorized(res);
} }
let { share_id } = website; await updateWebsite(
{
if (enable_share_url) { name,
share_id = share_id ? share_id : getRandomChars(8); domain,
} else { share_id: share_id || null,
share_id = null; user_id: account ? account.id : +owner,
} },
where,
await updateWebsite(websiteId, { name, domain, share_id, user_id: +owner }); );
return ok(res); return ok(res);
} }

View File

@ -1,10 +1,8 @@
import prisma from 'lib/prisma'; import prisma from 'lib/prisma';
export async function updateWebsite(website_id, data) { export async function updateWebsite(data, where) {
return prisma.client.website.update({ return prisma.client.website.update({
where: { where,
website_id,
},
data, data,
}); });
} }