mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-09 15:05:35 +00:00
feat: add delete link form and controller
This commit is contained in:
@@ -2,6 +2,7 @@ import CollectionsController from '#controllers/collections_controller';
|
|||||||
import Link from '#models/link';
|
import Link from '#models/link';
|
||||||
import {
|
import {
|
||||||
createLinkValidator,
|
createLinkValidator,
|
||||||
|
deleteLinkValidator,
|
||||||
updateLinkFavoriteStatusValidator,
|
updateLinkFavoriteStatusValidator,
|
||||||
updateLinkValidator,
|
updateLinkValidator,
|
||||||
} from '#validators/link';
|
} from '#validators/link';
|
||||||
@@ -88,6 +89,28 @@ export default class LinksController {
|
|||||||
return response.json({ status: 'ok' });
|
return response.json({ status: 'ok' });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async showDeletePage({ auth, inertia, request, response }: HttpContext) {
|
||||||
|
const linkId = request.qs()?.linkId;
|
||||||
|
if (!linkId) {
|
||||||
|
return response.redirectToNamedRoute('dashboard');
|
||||||
|
}
|
||||||
|
|
||||||
|
const link = await this.getLinkById(linkId, auth.user!.id);
|
||||||
|
await link.load('collection');
|
||||||
|
return inertia.render('links/delete', { link });
|
||||||
|
}
|
||||||
|
|
||||||
|
async delete({ request, auth, response }: HttpContext) {
|
||||||
|
const { params } = await request.validateUsing(deleteLinkValidator);
|
||||||
|
|
||||||
|
const link = await this.getLinkById(params.id, auth.user!.id);
|
||||||
|
await link.delete();
|
||||||
|
|
||||||
|
return response.redirectToNamedRoute('dashboard', {
|
||||||
|
qs: { collectionId: link.id },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get link by id.
|
* Get link by id.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
import vine from '@vinejs/vine';
|
import vine from '@vinejs/vine';
|
||||||
|
|
||||||
|
const params = vine.object({
|
||||||
|
id: vine.string().trim(),
|
||||||
|
});
|
||||||
|
|
||||||
export const createLinkValidator = vine.compile(
|
export const createLinkValidator = vine.compile(
|
||||||
vine.object({
|
vine.object({
|
||||||
name: vine.string().trim().minLength(1).maxLength(254),
|
name: vine.string().trim().minLength(1).maxLength(254),
|
||||||
@@ -18,9 +22,13 @@ export const updateLinkValidator = vine.compile(
|
|||||||
favorite: vine.boolean(),
|
favorite: vine.boolean(),
|
||||||
collectionId: vine.string().trim(),
|
collectionId: vine.string().trim(),
|
||||||
|
|
||||||
params: vine.object({
|
params,
|
||||||
id: vine.string().trim(),
|
})
|
||||||
}),
|
);
|
||||||
|
|
||||||
|
export const deleteLinkValidator = vine.compile(
|
||||||
|
vine.object({
|
||||||
|
params,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ const Input = styled.input(({ theme }) => ({
|
|||||||
},
|
},
|
||||||
|
|
||||||
'&:disabled': {
|
'&:disabled': {
|
||||||
opacity: 0.5,
|
opacity: 0.85,
|
||||||
},
|
},
|
||||||
|
|
||||||
'&::placeholder': {
|
'&::placeholder': {
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ interface FormLinkProps {
|
|||||||
data: FormLinkData;
|
data: FormLinkData;
|
||||||
errors?: Record<string, Array<string>>;
|
errors?: Record<string, Array<string>>;
|
||||||
collections: Collection[];
|
collections: Collection[];
|
||||||
|
disableInputs?: boolean;
|
||||||
|
submitBtnDanger?: boolean;
|
||||||
|
|
||||||
setData: (name: string, value: any) => void;
|
setData: (name: string, value: any) => void;
|
||||||
handleSubmit: () => void;
|
handleSubmit: () => void;
|
||||||
@@ -34,12 +36,14 @@ export default function FormLink({
|
|||||||
data,
|
data,
|
||||||
errors,
|
errors,
|
||||||
collections,
|
collections,
|
||||||
|
disableInputs = false,
|
||||||
|
submitBtnDanger = false,
|
||||||
|
|
||||||
setData,
|
setData,
|
||||||
handleSubmit,
|
handleSubmit,
|
||||||
}: FormLinkProps) {
|
}: FormLinkProps) {
|
||||||
const { t } = useTranslation('common');
|
const { t } = useTranslation('common');
|
||||||
const collectionId = useSearchParam('collectionId') ?? collections[0].id;
|
const collectionId = useSearchParam('collectionId') ?? collections?.[0].id;
|
||||||
|
|
||||||
const onSubmit = (event: FormEvent<HTMLFormElement>) => {
|
const onSubmit = (event: FormEvent<HTMLFormElement>) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
@@ -53,6 +57,7 @@ export default function FormLink({
|
|||||||
canSubmit={canSubmit}
|
canSubmit={canSubmit}
|
||||||
disableHomeLink={disableHomeLink}
|
disableHomeLink={disableHomeLink}
|
||||||
collectionId={collectionId}
|
collectionId={collectionId}
|
||||||
|
submitBtnDanger={submitBtnDanger}
|
||||||
>
|
>
|
||||||
<BackToDashboard>
|
<BackToDashboard>
|
||||||
<TextBox
|
<TextBox
|
||||||
@@ -62,6 +67,7 @@ export default function FormLink({
|
|||||||
onChange={setData}
|
onChange={setData}
|
||||||
value={data.name}
|
value={data.name}
|
||||||
errors={errors?.name}
|
errors={errors?.name}
|
||||||
|
disabled={disableInputs}
|
||||||
required
|
required
|
||||||
autoFocus
|
autoFocus
|
||||||
/>
|
/>
|
||||||
@@ -72,6 +78,7 @@ export default function FormLink({
|
|||||||
onChange={setData}
|
onChange={setData}
|
||||||
value={data.url}
|
value={data.url}
|
||||||
errors={errors?.url}
|
errors={errors?.url}
|
||||||
|
disabled={disableInputs}
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
<TextBox
|
<TextBox
|
||||||
@@ -81,12 +88,14 @@ export default function FormLink({
|
|||||||
onChange={setData}
|
onChange={setData}
|
||||||
value={data.description ?? undefined}
|
value={data.description ?? undefined}
|
||||||
errors={errors?.description}
|
errors={errors?.description}
|
||||||
|
disabled={disableInputs}
|
||||||
/>
|
/>
|
||||||
<select
|
<select
|
||||||
onChange={({ target }) => setData('collectionId', target.value)}
|
onChange={({ target }) => setData('collectionId', target.value)}
|
||||||
defaultValue={data.collectionId}
|
defaultValue={data.collectionId}
|
||||||
|
disabled={disableInputs}
|
||||||
>
|
>
|
||||||
{collections.map((collection) => (
|
{collections?.map((collection) => (
|
||||||
<option key={collection.id} value={collection.id}>
|
<option key={collection.id} value={collection.id}>
|
||||||
{collection.name}
|
{collection.name}
|
||||||
</option>
|
</option>
|
||||||
@@ -98,6 +107,7 @@ export default function FormLink({
|
|||||||
onChange={setData}
|
onChange={setData}
|
||||||
checked={data.favorite}
|
checked={data.favorite}
|
||||||
errors={errors?.favorite}
|
errors={errors?.favorite}
|
||||||
|
disabled={disableInputs}
|
||||||
/>
|
/>
|
||||||
</BackToDashboard>
|
</BackToDashboard>
|
||||||
</FormLayout>
|
</FormLayout>
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ export default function DeleteCollectionPage({
|
|||||||
setData={setData}
|
setData={setData}
|
||||||
handleSubmit={handleSubmit}
|
handleSubmit={handleSubmit}
|
||||||
errors={errors as any}
|
errors={errors as any}
|
||||||
disableInputs={true}
|
disableInputs
|
||||||
submitBtnDanger
|
submitBtnDanger
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|||||||
35
inertia/pages/links/delete.tsx
Normal file
35
inertia/pages/links/delete.tsx
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
import type Link from '#models/link';
|
||||||
|
import { useForm } from '@inertiajs/react';
|
||||||
|
import { route } from '@izzyjs/route/client';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
|
import FormLink from '~/components/form/form_link';
|
||||||
|
|
||||||
|
export default function DeleteLinkPage({ link }: { link: Link }) {
|
||||||
|
const { t } = useTranslation('common');
|
||||||
|
const { data, setData, submit, processing } = useForm({
|
||||||
|
name: link.name,
|
||||||
|
description: link.description,
|
||||||
|
url: link.url,
|
||||||
|
favorite: link.favorite,
|
||||||
|
collectionId: link.collectionId,
|
||||||
|
});
|
||||||
|
|
||||||
|
const handleSubmit = () => {
|
||||||
|
const { method, url } = route('link.delete', { params: { id: link.id } });
|
||||||
|
submit(method, url);
|
||||||
|
};
|
||||||
|
console.log(link);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<FormLink
|
||||||
|
title={t('link.delete')}
|
||||||
|
canSubmit={!processing}
|
||||||
|
data={data}
|
||||||
|
setData={setData}
|
||||||
|
handleSubmit={handleSubmit}
|
||||||
|
collections={[link.collection]}
|
||||||
|
disableInputs
|
||||||
|
submitBtnDanger
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -19,7 +19,10 @@ router
|
|||||||
.put('/:id/favorite', [LinksController, 'toggleFavorite'])
|
.put('/:id/favorite', [LinksController, 'toggleFavorite'])
|
||||||
.as('link.toggle-favorite');
|
.as('link.toggle-favorite');
|
||||||
|
|
||||||
router.get('/delete', () => 'delete').as('link.delete-form');
|
router
|
||||||
|
.get('/delete', [LinksController, 'showDeletePage'])
|
||||||
|
.as('link.delete-form');
|
||||||
|
router.delete('/:id', [LinksController, 'delete']).as('link.delete');
|
||||||
})
|
})
|
||||||
.middleware([middleware.auth()])
|
.middleware([middleware.auth()])
|
||||||
.prefix('/links');
|
.prefix('/links');
|
||||||
|
|||||||
Reference in New Issue
Block a user