Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add link endpoint creation #93

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 47 additions & 27 deletions src/app/shared-links/Components/AddLinkDialog.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -38,6 +37,8 @@ export const AddLinkDialog: FC<AddLinkDialogProps> = ({
callback,
}) => {
const { session } = useAuth();
const [hasError, setHasError] = useState(false);
const [disableButton, setDisableButton] = useState(false);

const {
reset,
Expand All @@ -48,17 +49,30 @@ export const AddLinkDialog: FC<AddLinkDialogProps> = ({
} = useForm<TCreateSHLinkDto>();

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(() => {
Expand All @@ -68,10 +82,17 @@ export const AddLinkDialog: FC<AddLinkDialogProps> = ({

return (
<Dialog open={!!open} fullWidth maxWidth="xs" onClose={() => onClose?.()}>
<StyledDialogTitle>Create a new Link</StyledDialogTitle>
<DialogContent style={{ padding: '5px 8px' }}>
<form onSubmit={handleSubmit(onSubmitForm)}>
<form onSubmit={handleSubmit(onSubmitForm)}>
<StyledDialogTitle>Create a new Link</StyledDialogTitle>
<DialogContent style={{ padding: '5px 8px' }}>
<StyledDialogContent>
{hasError && (
<Grid pb={2}>
<Alert severity="error">
The creation of the endpoint is failed
</Alert>
</Grid>
)}
<TextField
label="User id"
error={!!errors.userId}
Expand All @@ -81,9 +102,8 @@ export const AddLinkDialog: FC<AddLinkDialogProps> = ({
{...register('userId', {})}
/>
<TextField
label="Name"
label="Name *"
error={!!errors.name}
required
helperText={errors.name ? errors.name.message : null}
placeholder="Name"
{...register('name', { required: 'Required field' })}
Expand All @@ -104,21 +124,21 @@ export const AddLinkDialog: FC<AddLinkDialogProps> = ({
})}
/>
</StyledDialogContent>
</form>
</DialogContent>
<StyledDialogActions>
<Button color="inherit" variant="contained" onClick={onClose}>
Cancel
</Button>
<Button
type="submit"
color="success"
variant="contained"
onClick={handleSubmit(onSubmitForm)}
>
Create
</Button>
</StyledDialogActions>
</DialogContent>
<StyledDialogActions>
<Button color="inherit" variant="contained" onClick={onClose}>
Cancel
</Button>
<Button
type="submit"
color="success"
variant="contained"
disabled={disableButton}
>
Create
</Button>
</StyledDialogActions>
</form>
</Dialog>
);
};
9 changes: 9 additions & 0 deletions src/app/utils/api.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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` });
}
Expand Down
Loading