Skip to content

Commit

Permalink
Merge pull request #917 from thunderstore-io/continuing-908
Browse files Browse the repository at this point in the history
Fix CreateTeam forms UI problems and add field error parsing to Cyberstorm forms
  • Loading branch information
Oksamies authored Nov 24, 2023
2 parents 46dc662 + 362c3a9 commit 1c15173
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 48 deletions.
2 changes: 1 addition & 1 deletion packages/cyberstorm-forms/src/components/FormTextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function FormTextInput<
{...field}
ref={field.ref}
placeholder={placeholder}
color={isDirty ? (invalid ? "red" : "green") : undefined}
color={isDirty || invalid ? (invalid ? "red" : "green") : undefined}
disabled={isSubmitting || disabled}
/>
{error && <span className={styles.errorMessage}>{error.message}</span>}
Expand Down
7 changes: 4 additions & 3 deletions packages/cyberstorm-forms/src/forms/CreateTeamForm.module.css
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
.root {
display: flex;
flex-direction: column;
gap: 2rem;
}

.dialog {
display: flex;
flex-direction: column;
gap: 1.5rem;
padding: var(--space--32);
border-top: var(--border-width--px) solid var(--color-surface--5);
}

.dialogText {
Expand All @@ -22,6 +23,6 @@
flex-direction: row-reverse;
gap: 1rem;
justify-content: space-between;
padding-top: var(--space--16);
border-top: var(--border);
padding: var(--space--16) var(--space--24);
border-top: var(--border-width--px) solid var(--color-surface--5);
}
18 changes: 0 additions & 18 deletions packages/cyberstorm/src/components/Dialog/Dialog.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,6 @@
overflow: auto;
}

.bodyPadding > * {
padding-right: var(--space--32);
padding-left: var(--space--32);
}

.bodyPadding > :first-child {
padding-top: var(--space--16);
}

.bodyPadding > :last-child {
padding: var(--space--16) var(--space--24);
border-top: var(--border);
}

.bodyPadding > :nth-last-child(2) {
padding-bottom: var(--space--32);
}

.header {
display: flex;
flex-direction: row-reverse;
Expand Down
11 changes: 1 addition & 10 deletions packages/cyberstorm/src/components/Dialog/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ interface DialogProps extends PropsWithChildren {
defaultOpen?: boolean;
trigger?: ReactNode;
title?: string;
disableDialogContentStyles?: boolean;
showHeaderBorder?: boolean;
}

Expand All @@ -25,7 +24,6 @@ export function Dialog(props: DialogProps) {
defaultOpen = false,
trigger,
title = undefined,
disableDialogContentStyles = false,
showHeaderBorder = false,
} = props;

Expand Down Expand Up @@ -72,14 +70,7 @@ export function Dialog(props: DialogProps) {
{title ? <div className={styles.title}>{title}</div> : null}
</div>

<div
className={classnames(
styles.body,
disableDialogContentStyles ? null : styles.bodyPadding
)}
>
{children}
</div>
<div className={styles.body}>{children}</div>
</RadixDialog.Content>
</RadixDialog.Overlay>
</RadixDialog.Portal>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ export function PackageDependencyList(props: PackageDependencyListProps) {
headerIcon={<FontAwesomeIcon icon={faBoxOpen} />}
headerRightContent={
<Dialog.Root
disableDialogContentStyles
showHeaderBorder
title="Required mods"
trigger={
Expand Down
8 changes: 3 additions & 5 deletions packages/thunderstore-api/src/apiFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,22 @@ const BASE_HEADERS = {
export type apiFetchArgs = {
config: RequestConfig;
path: string;
method: RequestInit["method"];
query?: string;
body?: RequestInit["body"];
request?: Omit<RequestInit, "headers">;
};
export async function apiFetch2(args: apiFetchArgs) {
const url = getUrl(args.config, args.path, args.query);

const response = await fetch(url, {
...(args.request ?? {}),
headers: {
...BASE_HEADERS,
...getAuthHeaders(args.config),
},
body: args.body,
});

if (!response.ok) {
throw ApiError.createFromResponse(response);
throw await ApiError.createFromResponse(response);
}

return response.json();
Expand All @@ -38,7 +37,6 @@ export function apiFetch(config: RequestConfig, path: string, query?: string) {
config,
path,
query,
method: "GET",
});
}

Expand Down
58 changes: 50 additions & 8 deletions packages/thunderstore-api/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,61 @@
type JSONValue =
| string
| number
| boolean
| { [x: string]: JSONValue }
| JSONValue[];

export function isApiError(e: Error | ApiError | unknown): e is ApiError {
return e instanceof ApiError;
}

export class ApiError extends Error {
constructor(message?: string) {
super(message);
response: Response;
responseJson?: JSONValue;

constructor(args: {
message: string;
response: Response;
responseJson?: JSONValue;
}) {
super(args.message);
this.responseJson = args.responseJson;
this.response = args.response;
}

static createFromResponse(response: Response): ApiError {
// TODO: Implement response parsing for known error scenarios
return new ApiError(`${response.status}: ${response.statusText}`);
static async createFromResponse(response: Response): Promise<ApiError> {
let responseJson: JSONValue | undefined;
try {
responseJson = await response.json();
} catch (e) {
responseJson = undefined;
}

return new ApiError({
message: `${response.status}: ${response.statusText}`,
response: response,
responseJson: responseJson,
});
}

getFieldErrors(): { [key: string]: string[] } {
// TODO: Implement
return {};
getFieldErrors(): { [key: string | "root"]: string[] } {
if (typeof this.responseJson !== "object")
return { root: ["Unknown error occurred"] };
if (Array.isArray(this.responseJson)) {
return {
root: this.responseJson.map((x) => x.toString()),
};
} else {
return Object.fromEntries(
Object.entries(this.responseJson).map(([key, val]) => {
return [
key,
Array.isArray(val)
? val.map((x) => x.toString())
: [val.toString()],
];
})
);
}
}
}
6 changes: 4 additions & 2 deletions packages/thunderstore-api/src/fetch/teamCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export function createTeam(config: RequestConfig, data: CreateTeamApiArgs) {
return apiFetch2({
config,
path,
method: "POST",
body: JSON.stringify(data),
request: {
method: "POST",
body: JSON.stringify(data),
},
});
}

0 comments on commit 1c15173

Please sign in to comment.