Skip to content

Commit

Permalink
feat(occurrence): add resolve button to occurrence page and table to …
Browse files Browse the repository at this point in the history
…allow users to mark an occurrence as resolved or reinstate it, refs #33

feat(occurrence): add resolved_at field to occurrence model to track when an occurrence was resolved
fix(processError): auto reinstate an occurrence when it is processed again
  • Loading branch information
masterkain committed Jun 8, 2023
1 parent b1b27c7 commit b6ce009
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/occurrences/[occurrence_id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import BookmarkButton from '@/components/occurrence/BookmarkButton';
import Context from '@/components/occurrence/Context';
import Environment from '@/components/occurrence/Environment';
import Params from '@/components/occurrence/Params';
import ResolveButton from '@/components/occurrence/ResolveButton';
import Session from '@/components/occurrence/Session';
import Toolbox from '@/components/occurrence/Toolbox';
import ProjectActionsMenu from '@/components/project/ActionsMenu';
Expand Down Expand Up @@ -164,6 +165,7 @@ export default async function Occurrence({ params, searchParams }: ComponentProp
<p>Last seen: {occurrence.updated_at.toUTCString()}</p>
</div>
<BookmarkButton isBookmarked={isBookmarked} occurrenceId={occurrence.id} />
<ResolveButton occurrenceId={occurrence.id} resolvedAt={occurrence.resolved_at} />
<EnvironmentLabel env={occurrence.notice.env} className="self-start sm:self-auto" />
</div>
</div>
Expand Down
6 changes: 6 additions & 0 deletions components/OccurrencesTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ export default async function OccurrencesTable({ noticeId, searchParams }: Occur
</div>
<EnvironmentLabel env={occurrence.notice.env} />

{occurrence.resolved_at && (
<div className="flex-none rounded-md bg-green-900 px-2 py-1 text-xs font-medium text-white ring-1 ring-inset ring-green-700">
resolved
</div>
)}

<p className="truncate">
First seen: <CustomTimeAgo datetime={occurrence.created_at} locale="en_US" />
</p>
Expand Down
26 changes: 26 additions & 0 deletions components/occurrence/ResolveButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use client';

import { reinstateOccurrence, resolveOccurrence } from '@/app/_actions';
import { useTransition } from 'react';

export default function ResolveButton({ occurrenceId, resolvedAt }: { occurrenceId: string; resolvedAt: Date | null }) {
let [isPending, startTransition] = useTransition();

return resolvedAt ? (
<button
onClick={() => startTransition(() => reinstateOccurrence(occurrenceId))}
type="button"
className="flex-none rounded-md bg-rose-400/10 px-2 py-1 text-xs font-medium text-rose-400 ring-1 ring-inset ring-rose-400/30 hover:bg-rose-900 focus:z-10"
>
Reinstate
</button>
) : (
<button
onClick={() => startTransition(() => resolveOccurrence(occurrenceId))}
type="button"
className="flex-none rounded-md bg-green-400/10 px-2 py-1 text-xs font-medium text-green-400 ring-1 ring-inset ring-green-400/30 hover:bg-green-900 focus:z-10"
>
Resolve
</button>
);
}
18 changes: 18 additions & 0 deletions lib/actions/occurrenceActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,21 @@ export async function removeOccurrenceBookmark(occurrenceId: string) {
revalidatePath(`/occurrences/${occurrenceId}`)
revalidatePath('/bookmarks')
}

export async function resolveOccurrence(occurrenceId: string) {
await prisma.occurrence.update({
where: { id: occurrenceId },
data: { resolved_at: new Date() },
});

revalidatePath(`/occurrences/${occurrenceId}`);
}

export async function reinstateOccurrence(occurrenceId: string) {
await prisma.occurrence.update({
where: { id: occurrenceId },
data: { resolved_at: null },
});

revalidatePath(`/occurrences/${occurrenceId}`);
}
1 change: 1 addition & 0 deletions lib/processError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export async function processError(
},
},
update: {
resolved_at: null, // auto reinstate
// we've seen updated_at work here
seen_count: {
increment: 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "occurrences" ADD COLUMN "resolved_at" TIMESTAMP(6);
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ model Occurrence {
params Json @default("{}")
created_at DateTime @default(now()) @db.Timestamp(6)
updated_at DateTime @updatedAt @db.Timestamp(6)
resolved_at DateTime? @db.Timestamp(6)
notice Notice @relation(fields: [notice_id], references: [id], onDelete: Cascade)
hourly_occurrences HourlyOccurrence[]
bookmarks OccurrenceBookmark[]
Expand Down

0 comments on commit b6ce009

Please sign in to comment.