diff --git a/src/app/shared-links/Components/AddLinkDialog.tsx b/src/app/shared-links/Components/AddLinkDialog.tsx index 4724b93..cb14438 100644 --- a/src/app/shared-links/Components/AddLinkDialog.tsx +++ b/src/app/shared-links/Components/AddLinkDialog.tsx @@ -1,14 +1,13 @@ 'use client'; import { Dialog, - DialogTitle, DialogContent, TextField, - DialogActions, Button, - styled, + Alert, + Grid, } from '@mui/material'; -import { FC, useEffect } from 'react'; +import { FC, useEffect, useState } from 'react'; import { useForm } from 'react-hook-form'; import { StyledDialogActions } from '@/app/components/StyledDialogActions'; @@ -38,6 +37,8 @@ export const AddLinkDialog: FC = ({ callback, }) => { const { session } = useAuth(); + const [hasError, setHasError] = useState(false); + const [disableButton, setDisableButton] = useState(false); const { reset, @@ -48,17 +49,30 @@ export const AddLinkDialog: FC = ({ } = useForm(); const onSubmitForm = async (data: TCreateSHLinkDto) => { + setDisableButton(true); try { const transformedData = removeUndefinedValues(data); - await apiSharedLink.createLink(transformedData); - callback?.(); + const { data: createdLink } = + await apiSharedLink.createLink(transformedData); + await apiSharedLink + .createEndpoint(createdLink['id']) + .then(() => { + callback?.(); + }) + .catch(() => { + setHasError(true); + }); } catch (error) { console.error('Failed to create link:', error); } }; useEffect(() => { - if (open) reset(); + if (open) { + reset(); + setHasError(false); + setDisableButton(false); + } }, [open]); useEffect(() => { @@ -68,10 +82,17 @@ export const AddLinkDialog: FC = ({ return ( onClose?.()}> - Create a new Link - -
+ + Create a new Link + + {hasError && ( + + + The creation of the endpoint is failed + + + )} = ({ {...register('userId', {})} /> = ({ })} /> - -
- - - - +
+ + + + +
); }; diff --git a/src/app/utils/api.class.ts b/src/app/utils/api.class.ts index e35428d..7d78afe 100644 --- a/src/app/utils/api.class.ts +++ b/src/app/utils/api.class.ts @@ -43,6 +43,15 @@ export class ApiSHLink extends BaseApi { return await this.create({ url: `/${EEndpoint.shareLinks}`, data }); } + async createEndpoint(linkId: string) { + return await this.create({ + url: `/${EEndpoint.shareLinks}/${linkId}/endpoints`, + data: { + urlPath: '/$summary', + }, + }); + } + async deactivateLink(id: string) { return await this.delete({ url: `/share-links/${id}/deactivate` }); }