Skip to content

Commit

Permalink
Role page uses store logic. #10.
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanFrantz committed Jan 6, 2024
1 parent 20f79fc commit 6c8c64c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ const getRoleArchive = async (userId: string, roleId: number) => {
const kv = await Deno.openKv();
const isArchived = await kv.get([userId, "roleArchive", roleId]);
kv.close();
return isArchived;
return isArchived.value;
};

// Returns an HTTP status code and an object with helpful context.
Expand Down
28 changes: 14 additions & 14 deletions routes/api/role/[id]/archive.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { Handlers } from "$fresh/server.ts";

// Temp state, for testing.
const store = {archived: true};
import { getRoleArchive, setRoleArchive } from "../../../../lib/store.ts";

// Generate a partial document describing the archive status of a role.
const checkbox = ({url = new URL(), toggle = false} = {}) => {
if (toggle) {
store.archived = !store.archived;
}
const checkbox = async ({userId = "", url = new URL(), toggle = false} = {}) => {
// /api/role/4/archive
// ^
const roleId = url.pathname.split('/')[3];
// TODO: use roleId to look up role status.
const checked = store.archived ? "checked": "";
let isArchived = await getRoleArchive(userId, roleId);
if (toggle) {
isArchived = !isArchived;
await setRoleArchive(userId, roleId, isArchived);
}
const checked = isArchived ? "checked": "";
const archiveCheckbox = `
<div
id="archiveStatus"
Expand All @@ -30,20 +29,21 @@ const checkbox = ({url = new URL(), toggle = false} = {}) => {

export const handler: Handlers = {
// Return a resource describing the current state of the archive value.
async GET(req) {
async GET(req, ctx) {
const { userId } = ctx.state;
const url = new URL(req.url);
const role = {id: 1};

return new Response(checkbox({url: url}), {
return new Response(await checkbox({userId: userId, url: url}), {
status: 200,
});
},
// Toggle the archive state and return the updated resource.
async PUT(req) {
async PUT(req, ctx) {
const { userId } = ctx.state;
// Return the resource with its checked value toggled to the opposite of
// what it was previously.
const url = new URL(req.url);
return new Response(checkbox({url: url, toggle: true}), {
return new Response(await checkbox({userId: userId, url: url, toggle: true}), {
status: 200,
});
},
Expand Down

0 comments on commit 6c8c64c

Please sign in to comment.