Attribute errors with field property to the correct property

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2022-03-24 15:25:10 +01:00
parent 3c294b33a8
commit 45ce7d52b0
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
2 changed files with 36 additions and 7 deletions

View file

@ -0,0 +1,16 @@
import { Operation, NextLink } from "@apollo/client/core";
import { NetworkError } from "@apollo/client/errors";
import { ExecutionResult, GraphQLError } from "graphql";
export declare class AbsintheGraphQLError extends GraphQLError {
field?: string;
}
export declare type AbsintheGraphQLErrors = ReadonlyArray<AbsintheGraphQLError>;
export interface ErrorResponse {
graphQLErrors?: AbsintheGraphQLErrors;
networkError?: NetworkError;
response?: ExecutionResult;
operation: Operation;
forward: NextLink;
}

View file

@ -60,7 +60,12 @@
/>
</div>
<b-field :label="$t('Description')" label-for="group-summary">
<b-field
:label="$t('Description')"
label-for="group-summary"
:message="fieldErrors.summary"
:type="fieldErrors.summary ? 'is-danger' : undefined"
>
<b-input v-model="group.summary" type="textarea" id="group-summary" />
</b-field>
@ -102,7 +107,7 @@ import { convertToUsername } from "../../utils/username";
import PictureUpload from "../../components/PictureUpload.vue";
import { CONFIG } from "@/graphql/config";
import { IConfig } from "@/types/config.model";
import { ErrorResponse } from "@apollo/client/link/error";
import { ErrorResponse } from "@/types/errors.model";
import { ServerParseError } from "@apollo/client/link/http";
import { ApolloCache, FetchResult, InMemoryCache } from "@apollo/client/core";
@ -135,6 +140,8 @@ export default class CreateGroup extends mixins(IdentityEditionMixin) {
errors: string[] = [];
fieldErrors: Record<string, string> = {};
usernameWithDomain = usernameWithDomain;
async createGroup(): Promise<void> {
@ -244,11 +251,17 @@ export default class CreateGroup extends mixins(IdentityEditionMixin) {
);
}
}
this.errors.push(
...(err.graphQLErrors || []).map(
({ message }: { message: string }) => message
)
);
err.graphQLErrors?.forEach((error) => {
if (error.field) {
if (Array.isArray(error.message)) {
this.fieldErrors[error.field] = error.message[0];
} else {
this.fieldErrors[error.field] = error.message;
}
} else {
this.errors.push(error.message);
}
});
}
}
</script>