diff --git a/components/OAuth2Page/OAuth2Page.module.scss b/components/OAuth2Page/OAuth2Page.module.scss index 6101d50..40fcc82 100644 --- a/components/OAuth2Page/OAuth2Page.module.scss +++ b/components/OAuth2Page/OAuth2Page.module.scss @@ -27,6 +27,23 @@ font-size: 0.875rem; } } + + .urls { + display: flex; + flex-direction: column; + align-items: flex-start; + } + } +} + +.header { + display: flex; + justify-content: space-between; + align-items: center; + margin-bottom: 1.5rem; + + h1 { + margin-bottom: 0; } } diff --git a/components/OAuth2Page/OAuth2Page.tsx b/components/OAuth2Page/OAuth2Page.tsx index 17d3221..b25831b 100644 --- a/components/OAuth2Page/OAuth2Page.tsx +++ b/components/OAuth2Page/OAuth2Page.tsx @@ -1,4 +1,4 @@ -import useSWR from 'swr'; +import useSWR, { mutate } from 'swr'; import Image from 'next/image'; import { OAuth2ClientListItem, @@ -24,6 +24,9 @@ import { ModalProps } from '../../lib/types/modal.interface'; import { useForm } from '../../lib/hooks/useForm'; import { FormWrapper } from '../common/Form/FormWrapper/FormWrapper'; import { FormControl } from '../common/Form/FormControl/FormControl'; +import toast from 'react-hot-toast'; +import { Button } from '../common/Button/Button'; +import userHasPrivileges from '../../lib/utils/has-privileges'; const LINK_NAMES = { redirect_uri: 'Redirect URI', @@ -59,7 +62,10 @@ const LinkEdit = ({ onChange={(e) => { if (!formUrl.type) { formUrl.type = linkType; - (formData.urls as Partial[])?.push(formUrl); + (formData.urls as Partial[]) = [ + ...(formData.urls || []), + formUrl, + ]; } formUrl.url = e.target.value; handleInputChange(e, formData.urls, 'urls'); @@ -73,31 +79,69 @@ const EditClientModal = ({ close, modalRef, client, -}: ModalProps<{ client?: OAuth2ClientListItem }>) => { + isAdmin, + update, +}: ModalProps<{ + client?: OAuth2ClientListItem; + update: () => void; + isAdmin: boolean; +}>) => { const formRef = useRef(null); const { formData, handleInputChange, handleSubmit } = useForm< Partial >(client || {}, () => { - console.log(formData); + toast + .promise( + fetch( + client + ? `/api/admin/oauth2/clients/${client.id}` + : '/api/admin/oauth2/clients', + { + method: client ? 'PATCH' : 'POST', + headers: { + 'content-type': 'application/json', + }, + body: JSON.stringify({ activated: true, ...formData }), + } + ) + .then((res) => res.json()) + .then(async (data) => { + if (data.error) { + throw data; + } + return data; + }), + { + loading: 'Saving client...', + success: 'Client saved!', + error: (err) => `Saving the client failed: ${err.message}`, + } + ) + .then((data) => { + if (data) { + close(true); + update(); + } + }); return false; }); return ( -

Edit OAuth2 Client

+

{client ? 'Edit OAuth2 Client' : 'New OAuth2 Client'}

-
+ @@ -106,7 +150,7 @@ const EditClientModal = ({