From 9f576f112c0ecf4ed869355e7d728cc9a1d60bc8 Mon Sep 17 00:00:00 2001 From: Timofey Boyko Date: Thu, 2 Nov 2023 13:06:54 +0300 Subject: [PATCH] Client:PortalSettings: fix save client form --- .../ClientForm/ClientForm.types.ts | 8 ++- .../OAuth/sub-components/ClientForm/index.tsx | 53 +++++++------------ packages/client/src/store/OAuthStore.ts | 16 +++--- packages/common/api/oauth/index.ts | 7 ++- packages/common/utils/oauth/index.ts | 8 +-- packages/common/utils/oauth/interfaces.ts | 6 +-- 6 files changed, 43 insertions(+), 55 deletions(-) diff --git a/packages/client/src/pages/PortalSettings/categories/developer-tools/OAuth/sub-components/ClientForm/ClientForm.types.ts b/packages/client/src/pages/PortalSettings/categories/developer-tools/OAuth/sub-components/ClientForm/ClientForm.types.ts index da856da23d..02414bb7a4 100644 --- a/packages/client/src/pages/PortalSettings/categories/developer-tools/OAuth/sub-components/ClientForm/ClientForm.types.ts +++ b/packages/client/src/pages/PortalSettings/categories/developer-tools/OAuth/sub-components/ClientForm/ClientForm.types.ts @@ -1,6 +1,10 @@ //@ts-ignore import { OAuthStoreProps } from "SRC_DIR/store/OAuthStore"; -import { IClientProps, IScope } from "@docspace/common/utils/oauth/interfaces"; +import { + IClientProps, + IClientReqDTO, + IScope, +} from "@docspace/common/utils/oauth/interfaces"; export interface InputProps { value: string; @@ -40,7 +44,7 @@ export interface ClientFormProps { fetchClient?: (clientId: string) => Promise; fetchScopes?: () => Promise; - saveClient?: (client: IClientProps) => Promise; + saveClient?: (client: IClientReqDTO) => Promise; updateClient?: ( clientId: string, client: IClientProps diff --git a/packages/client/src/pages/PortalSettings/categories/developer-tools/OAuth/sub-components/ClientForm/index.tsx b/packages/client/src/pages/PortalSettings/categories/developer-tools/OAuth/sub-components/ClientForm/index.tsx index 79a89507c0..e68ba1be41 100644 --- a/packages/client/src/pages/PortalSettings/categories/developer-tools/OAuth/sub-components/ClientForm/index.tsx +++ b/packages/client/src/pages/PortalSettings/categories/developer-tools/OAuth/sub-components/ClientForm/index.tsx @@ -54,12 +54,12 @@ const ClientForm = ({ redirect_uris: [], allowed_origins: [], - logout_redirect_uris: [], + logout_redirect_uri: "", terms_url: "", policy_url: "", - authentication_method: "", + authentication_method: "zxc", scopes: [], }); @@ -95,40 +95,25 @@ const ClientForm = ({ // [checkedScopes] // ); - // const onSaveClick = async () => { - // const newClient: ClientProps = client ? { ...client } : ({} as ClientProps); + const onSaveClick = async () => { + if (!id) { + if (!saveClient) return; - // newClient.name = form.appName; - // newClient.logoUrl = form.appIcon; - // newClient.description = form.description; - // newClient.redirectUri = form.redirectUrl; - // newClient.logoutRedirectUri = form.logoutRedirectUrl; - // newClient.policyUrl = form.privacyURL; - // newClient.clientId = clientId; - // newClient.secret = secret; - // newClient.scopes = [...checkedScopes]; + await saveClient(form); - // if (!id) { - // if (!saveClient) return; + onCancelClick(); + } + // } else { + // if (!updateClient) return; + // await updateClient(clientId, newClient); + // } - // if (tenant === -1 && fetchTenant) { - // const t = await fetchTenant(); + // onCancelClick(); + }; - // newClient.tenant = t; - // } - - // await saveClient(newClient); - // } else { - // if (!updateClient) return; - // await updateClient(clientId, newClient); - // } - - // onCancelClick(); - // }; - - // const onCancelClick = () => { - // navigate("/portal-settings/developer-tools/oauth"); - // }; + const onCancelClick = () => { + navigate("/portal-settings/developer-tools/oauth"); + }; // const onResetClick = React.useCallback(async () => { // if (!regenerateSecret) return; @@ -312,8 +297,8 @@ const ClientForm = ({ {}} - onCancelClick={() => {}} + onSaveClick={onSaveClick} + onCancelClick={onCancelClick} isRequestRunning={isRequestRunning} saveButtonDisabled={!isValid} cancelButtonDisabled={isRequestRunning} diff --git a/packages/client/src/store/OAuthStore.ts b/packages/client/src/store/OAuthStore.ts index d3afef0060..c045fe503f 100644 --- a/packages/client/src/store/OAuthStore.ts +++ b/packages/client/src/store/OAuthStore.ts @@ -207,7 +207,10 @@ class OAuthStore implements OAuthStoreProps { this.currentPage = page + 1; }); - const clientList: IClientListProps = await getClientList(page, PAGE_LIMIT); + const clientList: IClientListProps = await getClientList( + this.nextPage || page, + PAGE_LIMIT + ); runInAction(() => { this.currentPage = clientList.page; @@ -220,17 +223,12 @@ class OAuthStore implements OAuthStoreProps { this.setClientsIsLoading(false); }; - //TODO: OAuth, add tenant and other params - saveClient = async (client: ClientProps) => { + saveClient = async (client: IClientReqDTO) => { try { - client.tenant = 1; - client.authenticationMethod = "zxc"; - client.termsUrl = "zxc"; - const newClient = await addClient(client); runInAction(() => { - this.clients.push(newClient); + this.clients.unshift(newClient); }); } catch (e) { console.log(e); @@ -287,7 +285,6 @@ class OAuthStore implements OAuthStoreProps { } }; - // COMPLETE fetchScope = async (name: string) => { try { const scope = await getScope(name); @@ -300,7 +297,6 @@ class OAuthStore implements OAuthStoreProps { } }; - // COMPLETE fetchScopes = async () => { try { const scopes = await getScopeList(); diff --git a/packages/common/api/oauth/index.ts b/packages/common/api/oauth/index.ts index 542624262c..880a8b3e6f 100644 --- a/packages/common/api/oauth/index.ts +++ b/packages/common/api/oauth/index.ts @@ -13,6 +13,7 @@ import { IClientListDTO, IScope, INoAuthClientProps, + IClientReqDTO, } from "../../utils/oauth/interfaces"; export const getClient = async ( @@ -59,11 +60,13 @@ export const getClientList = async ( return clients; }; -export const addClient = async (data: IClientProps): Promise => { +export const addClient = async (data: IClientReqDTO): Promise => { + data.logout_redirect_uri = data.website_url; + const client: IClientResDTO = await request({ method: "post", url: `/clients`, - data: transformToClientReqDTO(data), + data, }); return transformToClientProps(client); diff --git a/packages/common/utils/oauth/index.ts b/packages/common/utils/oauth/index.ts index 29aaf153ff..e265fa25e3 100644 --- a/packages/common/utils/oauth/index.ts +++ b/packages/common/utils/oauth/index.ts @@ -19,7 +19,7 @@ export const transformToClientProps = ( logo, authentication_method, redirect_uris, - logout_redirect_uris, + logout_redirect_uri, scopes, tenant, invalidated, @@ -44,7 +44,7 @@ export const transformToClientProps = ( logo, authenticationMethod: authentication_method, redirectUris: redirect_uris, - logoutRedirectUris: logout_redirect_uris, + logoutRedirectUri: logout_redirect_uri, scopes, tenant, invalidated, @@ -74,7 +74,7 @@ export const transformToClientReqDTO = ( logo, authenticationMethod, redirectUris: redirect_uris, - logoutRedirectUris: logout_redirect_uris, + logoutRedirectUri: logout_redirect_uri, scopes, websiteUrl, allowedOrigins, @@ -85,7 +85,7 @@ export const transformToClientReqDTO = ( description, logo, redirect_uris, - logout_redirect_uris, + logout_redirect_uri, terms_url, policy_url, diff --git a/packages/common/utils/oauth/interfaces.ts b/packages/common/utils/oauth/interfaces.ts index a472f59bd3..f28059afa6 100644 --- a/packages/common/utils/oauth/interfaces.ts +++ b/packages/common/utils/oauth/interfaces.ts @@ -36,7 +36,7 @@ export interface IClientProps { authenticationMethod: string; tenant: number; redirectUris: string[]; - logoutRedirectUris: string[]; + logoutRedirectUri: string; enabled: boolean; invalidated: boolean; scopes: string[]; @@ -59,7 +59,7 @@ export interface IClientReqDTO { terms_url: string; policy_url: string; redirect_uris: string[]; - logout_redirect_uris: string[]; + logout_redirect_uri: string; scopes: string[]; website_url: string; allowed_origins: string[]; @@ -77,7 +77,7 @@ export interface IClientResDTO { redirect_uris: string[]; terms_url: string; policy_url: string; - logout_redirect_uris: string[]; + logout_redirect_uri: string; authentication_method: string;