Skip to content

Commit

Permalink
Add back move to tutorial action
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiodxa committed Jun 16, 2024
1 parent cb450a6 commit 60507c1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
17 changes: 14 additions & 3 deletions app/routes/_.cms.articles/article-list.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { SerializeFrom } from "@remix-run/cloudflare";
import type { UUID } from "~/utils/uuid";
import type { loader } from "./route";
import type { action, loader } from "./route";

import { useFetcher, useLoaderData } from "@remix-run/react";
import { useId } from "react";
import { Trans } from "react-i18next";

import { useT } from "~/helpers/use-i18n.hook";
Expand All @@ -28,7 +27,7 @@ type ItemProps = SerializeFrom<typeof loader>["articles"][number];

function Item(props: ItemProps) {
let t = useT("cms.articles.list.item");
let id = useId();
let fetcher = useFetcher<typeof action>();

return (
<li className="flex items-center justify-between gap-3 gap-x-6 py-5">
Expand Down Expand Up @@ -57,6 +56,18 @@ function Item(props: ItemProps) {
</Button>
</Form>

<fetcher.Form method="post">
<input type="hidden" name="id" value={props.id} />
<Button
type="submit"
name="intent"
value={INTENT.moveToTutorial}
variant="secondary"
>
{t("moveToTutorial")}
</Button>
</fetcher.Form>

<DeleteButton id={props.id} />
</div>
</li>
Expand Down
12 changes: 10 additions & 2 deletions app/routes/_.cms.articles/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { assertUUID } from "~/utils/uuid";

import { Article } from "~/models/article.server";
import { ArticlesList } from "./article-list";
import { deleteArticle } from "./queries";
import { deleteArticle, moveToTutorial } from "./queries";
import { INTENT } from "./types";

export async function loader({ request, context }: LoaderFunctionArgs) {
Expand Down Expand Up @@ -54,7 +54,9 @@ export async function action({ request, context }: ActionFunctionArgs) {
if (user.role !== "admin") throw redirect("/");

let formData = await request.formData();
let intent = z.enum([INTENT.delete]).parse(formData.get("intent"));
let intent = z
.enum([INTENT.delete, INTENT.moveToTutorial])
.parse(formData.get("intent"));

try {
if (intent === INTENT.delete) {
Expand All @@ -63,6 +65,12 @@ export async function action({ request, context }: ActionFunctionArgs) {
await deleteArticle(context, id);
}

if (intent === INTENT.moveToTutorial) {
let id = formData.get("id");
assertUUID(id);
await moveToTutorial(context, id);
}

throw redirect("/cms/articles");
} catch (exception) {
if (exception instanceof Response) throw exception;
Expand Down
1 change: 1 addition & 0 deletions app/routes/_.cms.articles/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const INTENT = {
delete: "DELETE_ARTICLE" as const,
moveToTutorial: "MOVE_TO_TUTORIAL" as const,
};

0 comments on commit 60507c1

Please sign in to comment.