({ request, context })
| 14 | }; |
| 15 | |
| 16 | export let action: ActionFunction = async ({ request, context }) => { |
| 17 | const formData = await request.formData(); |
| 18 | const toastCookie = await getSession(request.headers.get("cookie")); |
| 19 | const jsonUrl = formData.get("jsonUrl"); |
| 20 | const title = formData.get("title") as string; |
| 21 | |
| 22 | const errors: CreateFromUrlError = {}; |
| 23 | if (!jsonUrl) errors.jsonUrl = true; |
| 24 | |
| 25 | if (Object.keys(errors).length) { |
| 26 | return errors; |
| 27 | } |
| 28 | |
| 29 | invariant(typeof jsonUrl === "string", "jsonUrl must be a string"); |
| 30 | |
| 31 | try { |
| 32 | const doc = await createFromUrlOrRawJson(jsonUrl, title); |
| 33 | |
| 34 | if (!doc) { |
| 35 | setErrorMessage( |
| 36 | toastCookie, |
| 37 | "Unknown error", |
| 38 | "Could not create document. Please try again." |
| 39 | ); |
| 40 | |
| 41 | return redirect("/", { |
| 42 | headers: { "Set-Cookie": await commitSession(toastCookie) }, |
| 43 | }); |
| 44 | } |
| 45 | |
| 46 | const requestUrl = new URL(request.url); |
| 47 | |
| 48 | context.waitUntil( |
| 49 | sendEvent({ |
| 50 | type: "create", |
| 51 | from: "urlOrJson", |
| 52 | id: doc.id, |
| 53 | source: |
| 54 | requestUrl.searchParams.get("utm_source") ?? requestUrl.hostname, |
| 55 | }) |
| 56 | ); |
| 57 | |
| 58 | return redirect(`/j/${doc.id}`); |
| 59 | } catch (e) { |
| 60 | if (e instanceof Error) { |
| 61 | setErrorMessage(toastCookie, e.message, "Something went wrong"); |
| 62 | } else { |
| 63 | setErrorMessage(toastCookie, "Unknown error", "Something went wrong"); |
| 64 | } |
| 65 | |
| 66 | return redirect("/", { |
| 67 | headers: { "Set-Cookie": await commitSession(toastCookie) }, |
| 68 | }); |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | export let loader: LoaderFunction = async ({ request, context }) => { |
| 73 | const url = new URL(request.url); |
nothing calls this directly
no test coverage detected