Skip to content

Commit

Permalink
Fix permissions and imports for actions
Browse files Browse the repository at this point in the history
Also add documentation for video readyState and format prisma schema
  • Loading branch information
archessmn committed Oct 3, 2024
1 parent 2a4a1a8 commit eb7bdf0
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 51 deletions.
40 changes: 6 additions & 34 deletions app/(authenticated)/webcam/WebcamGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,13 @@
import { WebcamView } from "@/components/WebcamView";
import { WebcamFeed } from "@prisma/client";
import { use } from "react";
import {
addWebcamSchema,
editWebcamSchema,
removeWebcamSchema,
} from "./schema";
import { z } from "zod";
import { FormResponse } from "@/components/Form";
import { WebcamCreateForm, WebcamEditForm, WebcamRemoveForm } from "./form";
import { addWebcam } from "./actions";
import { addWebcam, editWebcam, removeWebcam } from "./actions";
import { Card, Grid, Group, Stack, Text } from "@mantine/core";
import { useElementSize } from "@mantine/hooks";
import { PermissionGate } from "@/components/UserContext";

export function WebcamGrid(props: {
webcams: Promise<WebcamFeed[]>;
addWebcam: (data: z.infer<typeof addWebcamSchema>) => Promise<FormResponse>;
editWebcam: (data: z.infer<typeof editWebcamSchema>) => Promise<FormResponse>;
removeWebcam: (
data: z.infer<typeof removeWebcamSchema>,
) => Promise<FormResponse>;
}) {
export function WebcamGrid(props: { webcams: Promise<WebcamFeed[]> }) {
const webcamFeeds = use(props.webcams);

return (
Expand All @@ -34,12 +20,7 @@ export function WebcamGrid(props: {
{webcamFeeds.length > 0 ? (
<Grid w={"100%"}>
{webcamFeeds.map((feed) => (
<WebcamGridCol
feed={feed}
key={feed.webcam_id}
editWebcam={props.editWebcam}
removeWebcam={props.removeWebcam}
/>
<WebcamGridCol feed={feed} key={feed.webcam_id} />
))}
</Grid>
) : (
Expand All @@ -49,13 +30,7 @@ export function WebcamGrid(props: {
);
}

function WebcamGridCol(props: {
feed: WebcamFeed;
editWebcam: (data: z.infer<typeof editWebcamSchema>) => Promise<FormResponse>;
removeWebcam: (
data: z.infer<typeof removeWebcamSchema>,
) => Promise<FormResponse>;
}) {
function WebcamGridCol(props: { feed: WebcamFeed }) {
const { ref, height, width } = useElementSize();

return (
Expand All @@ -74,11 +49,8 @@ function WebcamGridCol(props: {
</Card.Section>
<Group>
<PermissionGate required={"Webcams.Manage"}>
<WebcamEditForm webcam={props.feed} edit={props.editWebcam} />
<WebcamRemoveForm
webcam={props.feed}
remove={props.removeWebcam}
/>
<WebcamEditForm webcam={props.feed} edit={editWebcam} />
<WebcamRemoveForm webcam={props.feed} remove={removeWebcam} />
</PermissionGate>
</Group>
</Stack>
Expand Down
7 changes: 0 additions & 7 deletions app/(authenticated)/webcam/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ import {
removeWebcamFeed,
} from "@/features/webcams";
import { revalidatePath } from "next/cache";
import { requirePermission } from "@/lib/auth/server";

export async function addWebcam(unsafeData: unknown): Promise<FormResponse> {
await requirePermission("Webcams.Manage");

const parsedData = await addWebcamSchema.safeParseAsync(unsafeData);

if (!parsedData.success) {
Expand All @@ -34,8 +31,6 @@ export async function addWebcam(unsafeData: unknown): Promise<FormResponse> {
}

export async function editWebcam(unsafeData: unknown): Promise<FormResponse> {
await requirePermission("Webcams.Manage");

const parsedData = await editWebcamSchema.safeParseAsync(unsafeData);

if (!parsedData.success) {
Expand All @@ -52,8 +47,6 @@ export async function editWebcam(unsafeData: unknown): Promise<FormResponse> {
}

export async function removeWebcam(unsafeData: unknown): Promise<FormResponse> {
await requirePermission("Webcams.Manage");

const parsedData = await removeWebcamSchema.safeParseAsync(unsafeData);

if (!parsedData.success) {
Expand Down
7 changes: 1 addition & 6 deletions app/(authenticated)/webcam/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ export default function WebcamPage() {
<Center>
<Stack w={"100%"}>
<Suspense fallback={<Loader />}>
<WebcamGrid
webcams={webcamFeeds}
addWebcam={addWebcam}
editWebcam={editWebcam}
removeWebcam={removeWebcam}
/>
<WebcamGrid webcams={webcamFeeds} />
</Suspense>
</Stack>
</Center>
Expand Down
2 changes: 2 additions & 0 deletions components/WebcamView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export function WebcamView(props: {
useDisclosure(false);

useEffect(() => {
// The four (4) here is the value of readyState when a video element is ready to play
// See: https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/readyState
if (ref.current?.readyState === 4) {
setReady();
} else {
Expand Down
5 changes: 5 additions & 0 deletions features/webcams/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@ import {
editWebcamSchema,
removeWebcamSchema,
} from "@/app/(authenticated)/webcam/schema";
import { requirePermission } from "@/lib/auth/server";
import { prisma } from "@/lib/db";
import { z } from "zod";

export async function fetchWebcamFeeds() {
await requirePermission("Webcams.View");
return await prisma.webcamFeed.findMany();
}

export async function addWebcamFeed(data: z.infer<typeof addWebcamSchema>) {
await requirePermission("Webcams.Manage");
return await prisma.webcamFeed.create({ data });
}

export async function editWebcamFeed(data: z.infer<typeof editWebcamSchema>) {
await requirePermission("Webcams.Manage");
return await prisma.webcamFeed.update({
where: { webcam_id: data.webcam_id },
data: {
Expand All @@ -28,6 +32,7 @@ export async function editWebcamFeed(data: z.infer<typeof editWebcamSchema>) {
export async function removeWebcamFeed(
data: z.infer<typeof removeWebcamSchema>,
) {
await requirePermission("Webcams.Manage");
return await prisma.webcamFeed.delete({
where: {
webcam_id: data.webcam_id,
Expand Down
8 changes: 4 additions & 4 deletions lib/db/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,10 @@ model EquipmentListTemplate {
}

model WebcamFeed {
webcam_id Int @id @default(autoincrement())
full_name String
identifier String @unique
stream_url String
webcam_id Int @id @default(autoincrement())
full_name String
identifier String @unique
stream_url String
@@map("webcam_feeds")
}

0 comments on commit eb7bdf0

Please sign in to comment.