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: when delete prod you also delete all connected layouts #40

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions src/api/manager/multiviews.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ObjectId, WithId } from 'mongodb';
import { MultiviewPreset } from '../../interfaces/preset';
import { getDatabase } from '../mongoClient/dbClient';
import { Log } from '../logger';

export async function getMultiviewLayouts(): Promise<MultiviewPreset[]> {
const db = await getDatabase();
Expand Down Expand Up @@ -36,3 +37,10 @@ export async function putMultiviewLayout(
await collection.insertOne({ ...newMultiviewLayout, _id: new ObjectId() });
}
}

export async function deleteLayouts(id: string): Promise<void> {
const db = await getDatabase();

await db.collection('multiviews').deleteMany({ productionId: id });
Log().info('Deleted layouts for production', id);
}
24 changes: 24 additions & 0 deletions src/app/api/manager/multiviews/route.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextRequest, NextResponse } from 'next/server';
import { isAuthenticated } from '../../../../api/manager/auth';
import {
deleteLayouts,
getMultiviewLayouts,
putMultiviewLayout
} from '../../../../api/manager/multiviews';
Expand Down Expand Up @@ -43,3 +44,26 @@ export async function PUT(request: NextRequest): Promise<NextResponse> {
});
}
}

export async function DELETE(request: NextRequest): Promise<NextResponse> {
if (!(await isAuthenticated())) {
return new NextResponse(`Not Authorized!`, {
status: 403
});
}
try {
const productionId = await request.json();
await deleteLayouts(productionId);
return new NextResponse(null, {
status: 200
});
} catch (error) {
console.log(error);
return new NextResponse(
`Error occurred while posting to DB! Error: ${error}`,
{
status: 500
}
);
}
}
7 changes: 5 additions & 2 deletions src/components/productionsList/DeleteProductionButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useCallback, useState } from 'react';
import { Loader } from '../loader/Loader';
import { useRouter } from 'next/navigation';
import { DeleteModal } from '../modal/DeleteModal';
import { useDeleteMultiviewLayouts } from '../../hooks/multiviewLayout';

type DeleteProductionButtonProps = {
id: string;
Expand All @@ -22,6 +23,7 @@ export function DeleteProductionButton({
}: DeleteProductionButtonProps) {
const router = useRouter();
const deleteProduction = useDeleteProduction();
const deleteLayouts = useDeleteMultiviewLayouts();

const [loading, setLoading] = useState(false);
const [modalOpen, setModalOpen] = useState(false);
Expand All @@ -30,10 +32,11 @@ export function DeleteProductionButton({
const onConfirm = useCallback(async () => {
setModalOpen(false);
setLoading(true);
deleteProduction(id)
await deleteProduction(id);
deleteLayouts(id)
.then(() => router.refresh())
.finally(() => setLoading(false));
}, [router, deleteProduction, id]);
}, [deleteProduction, id, deleteLayouts, router]);

return (
<>
Expand Down
14 changes: 14 additions & 0 deletions src/hooks/multiviewLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,17 @@ export function usePutMultiviewLayout() {
throw await response.text();
};
}

export function useDeleteMultiviewLayouts() {
return async (id: string): Promise<void> => {
const response = await fetch(`/api/manager/multiviews`, {
method: 'DELETE',
headers: [['x-api-key', `Bearer ${API_SECRET_KEY}`]],
body: JSON.stringify(id)
});
if (response.ok) {
return;
}
throw await response.text();
};
}
Loading