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

fix: optimistic update #71

Merged
merged 1 commit into from
Nov 3, 2023
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
3 changes: 2 additions & 1 deletion src/actions/delete-invitation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import db from '@/lib/db';
import * as Sentry from '@sentry/nextjs';
import { checkAuthAction, getErrorMessage } from './utils';
import { revalidateTag } from 'next/cache';
import { revalidatePath } from 'next/cache';

interface DeleteInvitationResult {
error?: {
Expand Down Expand Up @@ -32,6 +32,7 @@ export default async function deleteInvitation(
},
});

revalidatePath('/teams/[teamSlug]/settings');
return { data: { invitationId } };
} catch (err: any) {
Sentry.captureException(err);
Expand Down
1 change: 0 additions & 1 deletion src/actions/generate-api-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import db from '@/lib/db';
import * as Sentry from '@sentry/nextjs';
import jwt from 'jsonwebtoken';
import { revalidateTag } from 'next/cache';

interface APIKeyGenerationResult {
error?: {
Expand Down
1 change: 0 additions & 1 deletion src/actions/update-team-info.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use server';
import db from '@/lib/db';
import * as Sentry from '@sentry/nextjs';
import { revalidateTag } from 'next/cache';
import { checkAuthAction, checkTeamAction, getErrorMessage } from './utils';
import { Team } from '@prisma/client';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';

import useCustomToast from '@/hooks/useCustomToast';
import { useTransition, useOptimistic } from 'react';
import { useTransition } from 'react';
import { TeamInvitation } from '@/lib/queries';
import message from '@/messages/en';
import InvitationItem from './InvitationItem';
Expand All @@ -14,15 +14,9 @@ type Props = {
const InvitationList = ({ invitations }: Props) => {
const { successToast, errorToast } = useCustomToast();
const [isPending, startTransition] = useTransition();
const [optiInvitations, removeOptiInvitation] = useOptimistic(
invitations || [],
(state, deleteId: string) =>
[...state].filter((invitation) => invitation?.id !== deleteId)
);

const handleDeleteInvitation = async (invitation: TeamInvitation) => {
startTransition(async () => {
removeOptiInvitation(invitation?.id);
const { error } = await deleteInvitation(invitation.id);
if (error) {
errorToast(error.message);
Expand All @@ -35,7 +29,7 @@ const InvitationList = ({ invitations }: Props) => {

return (
<div className="flex flex-col gap-2 pt-4">
{optiInvitations.map((invitation: TeamInvitation) => (
{invitations.map((invitation: TeamInvitation) => (
<InvitationItem
key={invitation.id}
invitation={invitation}
Expand Down
Loading