Skip to content

Commit

Permalink
docs(swr): add example incrementing a value
Browse files Browse the repository at this point in the history
  • Loading branch information
vvo committed Jun 14, 2024
1 parent baf6e0f commit 7dbca00
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export async function POST(request: NextRequest) {
export async function GET(request: NextRequest) {
const session = await getIronSession<SessionData>(cookies(), sessionOptions);

console.log(new URL(request.url).searchParams);
const action = new URL(request.url).searchParams.get("action");
// /app-router-client-component-redirect-route-handler-fetch/session?action=logout
if (action === "logout") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import useSession from "./use-session";
import { defaultSession } from "./lib";

export function Form() {
const { session, isLoading } = useSession();
const { session, isLoading, increment } = useSession();

if (isLoading) {
return <p className="text-lg">Loading...</p>;
Expand All @@ -16,6 +16,21 @@ export function Form() {
<>
<p className="text-lg">
Logged in user: <strong>{session.username}</strong>
&nbsp;
<button
className={css.button}
onClick={() => {
increment(null, {
optimisticData: {
...session,
counter: session.counter + 1,
},
revalidate: false,
});
}}
>
{session.counter}
</button>
</p>
<LogoutButton />
</>
Expand All @@ -38,6 +53,7 @@ function LoginForm() {
optimisticData: {
isLoggedIn: true,
username,
counter: 0,
},
});
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { SessionOptions } from "iron-session";
export interface SessionData {
username: string;
isLoggedIn: boolean;
counter: number;
}

export const defaultSession: SessionData = {
username: "",
isLoggedIn: false,
counter: 0,
};

export const sessionOptions: SessionOptions = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export async function POST(request: NextRequest) {

session.isLoggedIn = true;
session.username = username;
session.counter = 0;
await session.save();

// simulate looking up the user in db
Expand All @@ -22,6 +23,23 @@ export async function POST(request: NextRequest) {
return Response.json(session);
}

export async function PATCH() {
const session = await getIronSession<SessionData>(cookies(), sessionOptions);

session.counter++;
session.updateConfig({
...sessionOptions,
cookieOptions: {
...sessionOptions.cookieOptions,
expires: new Date("2024-12-27T00:00:00.000Z"),
maxAge: undefined,
},
});
await session.save();

return Response.json(session);
}

// read session
export async function GET() {
const session = await getIronSession<SessionData>(cookies(), sessionOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ function doLogout(url: string) {
});
}

function doIncrement(url: string) {
return fetchJson<SessionData>(url, {
method: "PATCH",
});
}

export default function useSession() {
const { data: session, isLoading } = useSWR(
sessionApiRoute,
Expand All @@ -45,6 +51,7 @@ export default function useSession() {
revalidate: false,
});
const { trigger: logout } = useSWRMutation(sessionApiRoute, doLogout);
const { trigger: increment } = useSWRMutation(sessionApiRoute, doIncrement);

return { session, logout, login, isLoading };
return { session, logout, login, increment, isLoading };
}

0 comments on commit 7dbca00

Please sign in to comment.