frontend server utility improvement

This commit is contained in:
Oliver Bryan
2025-12-31 17:57:55 +00:00
parent c7d261048b
commit 70ef02f790
19 changed files with 381 additions and 182 deletions

View File

@@ -10,7 +10,8 @@ import {
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { cn, getAuthHeaders, getServerURL } from "@/lib/utils";
import { issue } from "@/lib/server";
import { cn } from "@/lib/utils";
export function CreateIssue({
projectId,
@@ -88,36 +89,24 @@ export function CreateIssue({
setSubmitting(true);
try {
const url = new URL(`${getServerURL()}/issue/create`);
url.searchParams.set("projectId", `${projectId}`);
url.searchParams.set("title", title.trim());
url.searchParams.set("description", description.trim());
const res = await fetch(url.toString(), {
headers: getAuthHeaders(),
await issue.create({
projectId,
title,
description,
onSuccess: async (data) => {
setOpen(false);
reset();
try {
await completeAction?.(data.id);
} catch (actionErr) {
console.error(actionErr);
}
},
onError: (message) => {
setError(message);
setSubmitting(false);
},
});
if (!res.ok) {
const message = await res.text();
setError(message || `failed to create issue (${res.status})`);
setSubmitting(false);
return;
}
const issue = (await res.json()) as { id?: number };
if (!issue.id) {
setError("failed to create issue");
setSubmitting(false);
return;
}
setOpen(false);
reset();
try {
await completeAction?.(issue.id);
} catch (actionErr) {
console.error(actionErr);
}
} catch (err) {
console.error(err);
setError("failed to create issue");

View File

@@ -10,7 +10,8 @@ import {
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { cn, getAuthHeaders, getServerURL } from "@/lib/utils";
import { organisation } from "@/lib/server";
import { cn } from "@/lib/utils";
const slugify = (value: string) =>
value
@@ -88,39 +89,25 @@ export function CreateOrganisation({
setSubmitting(true);
try {
const url = new URL(`${getServerURL()}/organisation/create`);
url.searchParams.set("name", name.trim());
url.searchParams.set("slug", slug.trim());
url.searchParams.set("userId", `${userId}`);
if (description.trim() !== "") {
url.searchParams.set("description", description.trim());
}
const res = await fetch(url.toString(), {
headers: getAuthHeaders(),
await organisation.create({
name,
slug,
description,
userId,
onSuccess: async (data) => {
setOpen(false);
reset();
try {
await completeAction?.(data.id);
} catch (actionErr) {
console.error(actionErr);
}
},
onError: (err) => {
setError(err || "failed to create organisation");
setSubmitting(false);
},
});
if (!res.ok) {
const message = await res.text();
setError(message || `failed to create organisation (${res.status})`);
setSubmitting(false);
return;
}
const organisation = (await res.json()) as { id?: number };
if (!organisation.id) {
setError("failed to create organisation");
setSubmitting(false);
return;
}
setOpen(false);
reset();
try {
await completeAction?.(organisation.id);
} catch (actionErr) {
console.error(actionErr);
}
} catch (err) {
console.error(err);
setError("failed to create organisation");

View File

@@ -1,3 +1,4 @@
import type { ProjectRecord } from "@issue/shared";
import { type FormEvent, useMemo, useState } from "react";
import { Button } from "@/components/ui/button";
import {
@@ -10,7 +11,8 @@ import {
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { cn, getAuthHeaders, getServerURL } from "@/lib/utils";
import { project } from "@/lib/server";
import { cn } from "@/lib/utils";
const keyify = (value: string) =>
value
@@ -94,37 +96,27 @@ export function CreateProject({
setSubmitting(true);
try {
const url = new URL(`${getServerURL()}/project/create`);
url.searchParams.set("key", key);
url.searchParams.set("name", name.trim());
url.searchParams.set("creatorId", `${userId}`);
url.searchParams.set("organisationId", `${organisationId}`);
await project.create({
key,
name,
creatorId: userId,
organisationId,
onSuccess: async (data) => {
const project = data as ProjectRecord;
const res = await fetch(url.toString(), {
headers: getAuthHeaders(),
setOpen(false);
reset();
try {
await completeAction?.(project.id);
} catch (actionErr) {
console.error(actionErr);
}
},
onError: (message) => {
setError(message);
setSubmitting(false);
},
});
if (!res.ok) {
const message = await res.text();
setError(message || `failed to create project (${res.status})`);
setSubmitting(false);
return;
}
const project = (await res.json()) as { id?: number };
if (!project.id) {
setError("failed to create project");
setSubmitting(false);
return;
}
setOpen(false);
reset();
try {
await completeAction?.(project.id);
} catch (actionErr) {
console.error(actionErr);
}
} catch (err) {
console.error(err);
setError("failed to create project");