| 1 | /** |
| 2 | * Starring a repository, or taking the star back (`intent` star or |
| 3 | * unstar), from the Star button in the project's header |
| 4 | * (components/star-button.tsx). |
| 5 | */ |
| 6 | import { data } from "react-router"; |
| 7 | |
| 8 | import type { Route } from "./+types/star"; |
| 9 | import { repos } from "../../lib/services.server"; |
| 10 | import { assertSameOrigin, requireUser } from "../../lib/session.server"; |
| 11 | |
| 12 | export async function action({ request, params, context }: Route.ActionArgs) { |
| 13 | assertSameOrigin(request); |
| 14 | const user = requireUser(context, request); |
| 15 | const form = await request.formData(); |
| 16 | const intent = form.get("intent"); |
| 17 | if (intent !== "star" && intent !== "unstar") return data({ error: "Nothing to do.", stars: null }, { status: 400 }); |
| 18 | try { |
| 19 | const found = await repos.star(user, { namespace: params.owner, name: params.repo }, intent === "star"); |
| 20 | if (!found.ok) return data({ error: found.error.message, stars: null }, { status: found.error.code === "not_found" ? 404 : 403 }); |
| 21 | return { error: null, stars: found.value }; |
| 22 | } catch { |
| 23 | return data({ error: "That did not save. Try again in a moment.", stars: null }, { status: 503 }); |
| 24 | } |
| 25 | } |