Skip to content

Commit

Permalink
feat: add validation to the form
Browse files Browse the repository at this point in the history
  • Loading branch information
gosiexon-zen committed Nov 22, 2024
1 parent fefec33 commit 2df4d53
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 60 deletions.
88 changes: 59 additions & 29 deletions assets/service-catalog-bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 30 additions & 9 deletions src/modules/service-catalog/ServiceCatalogItemPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ItemRequestForm } from "./components/service-catalog-item/ItemRequestFo
import type { Organization } from "../ticket-fields";
import { useServiceCatalogItem } from "./useServiceCatalogItem";
import { useServiceFormSubmit } from "./useServiceFormSubmit";
import type { RequestError } from "./data-types/RequestError";

const Container = styled.div`
display: flex;
Expand Down Expand Up @@ -37,21 +38,41 @@ export function ServiceCatalogItemPage({
brandId,
}: ServiceCatalogItemPageProps) {
const serviceCatalogItem = useServiceCatalogItem(serviceCatalogItemId);
const { requestFields, handleChange } = useItemFormFields(
serviceCatalogItem,
baseLocale
);
const {
requestFields,
associatedLookupField,
setRequestFields,
handleChange,
} = useItemFormFields(serviceCatalogItem, baseLocale);
const { submitServiceItemRequest } = useServiceFormSubmit(
serviceCatalogItem,
requestFields
requestFields,
associatedLookupField,
baseLocale
);

const handleRequestSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
try {
await submitServiceItemRequest();
} catch (error) {
console.error("Error submitting service item request:", error);

const response = await submitServiceItemRequest();
if (response && !response.ok) {
const errorData = await response.json();
if (errorData.error === "RecordInvalid") {
const invalidFieldErros = errorData.details.base;
const updatedFields = requestFields.map((field) => {
const errorField = invalidFieldErros.find(
(errorField: RequestError) => errorField.field_key === field.id
);
return errorField
? { ...field, error: errorField.description }
: field;
});
setRequestFields(updatedFields);
}
} else if (response && response.ok) {
const data = await response?.json();
const redirectUrl = "/hc/requests/" + data.request.id;
window.location.href = redirectUrl;
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export function ItemRequestForm({
}: ItemRequestFormProps) {
const { t } = useTranslation();
return (
<Form onSubmit={onSubmit}>
<Form onSubmit={onSubmit} noValidate>
<LeftColumn>
<CollapsibleDescription
title={serviceCatalogItem.name}
Expand Down
32 changes: 24 additions & 8 deletions src/modules/service-catalog/components/useItemFormFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,23 @@ const formatField = (field: TicketField): Field => {
};
};

const isAssociatedLookupField = (field: TicketField) => {
const isAssociatedLookupField = (
field: TicketField,
setAssociatedLookupField: (field: Field | null) => void
) => {
const customObjectKey = getCustomObjectKey(
field.relationship_target_type as string
);
return customObjectKey === "service_catalog_item";
if (customObjectKey === "service_catalog_item") {
setAssociatedLookupField(formatField(field));
return true;
} else return false;
};

const fetchTicketFields = async (
form_id: string,
baseLocale: string
baseLocale: string,
setAssociatedLookupField: (field: Field | null) => void
): Promise<Field[]> => {
try {
const [formResponse, fieldsResponse] = await Promise.all([
Expand Down Expand Up @@ -66,7 +73,7 @@ const fetchTicketFields = async (
ticketField &&
!(
ticketField.type === "lookup" &&
isAssociatedLookupField(ticketField)
isAssociatedLookupField(ticketField, setAssociatedLookupField)
) &&
ticketField.editable_in_portal
) {
Expand All @@ -87,14 +94,18 @@ export function useItemFormFields(
baseLocale: string
) {
const [requestFields, setRequestFields] = useState<Field[]>([]);
const [associatedLookupField, setAssociatedLookupField] =
useState<Field | null>();

useEffect(() => {
const fetchAndSetFields = async () => {
if (serviceCatalogItem && serviceCatalogItem.form_id) {
try {
await fetchTicketFields(serviceCatalogItem.form_id, baseLocale).then(
(ticketFields) => setRequestFields(ticketFields)
);
await fetchTicketFields(
serviceCatalogItem.form_id,
baseLocale,
setAssociatedLookupField
).then((ticketFields) => setRequestFields(ticketFields));
} catch (error) {
console.error("Error fetching ticket fields:", error);
}
Expand All @@ -117,5 +128,10 @@ export function useItemFormFields(
[requestFields]
);

return { requestFields, handleChange };
return {
requestFields,
associatedLookupField,
setRequestFields,
handleChange,
};
}
5 changes: 5 additions & 0 deletions src/modules/service-catalog/data-types/RequestError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface RequestError {
description: string;
error: string;
field_key: number;
}
1 change: 1 addition & 0 deletions src/modules/service-catalog/data-types/TicketField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface TicketField {
title: string;
value?: string | string[] | boolean;
required: boolean;
error: string | null;
description: string;
type: string;
custom_field_options: FieldOption[];
Expand Down
34 changes: 21 additions & 13 deletions src/modules/service-catalog/useServiceFormSubmit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@ import type { ServiceCatalogItem } from "./data-types/ServiceCatalogItem";

export function useServiceFormSubmit(
serviceCatalogItem: ServiceCatalogItem | undefined,
requestFields: Field[]
requestFields: Field[],
associatedLookupField: Field | null | undefined,
baseLocale: string
) {
const submitServiceItemRequest = async () => {
if (!serviceCatalogItem) {
return;
}
console.log("requestFields", requestFields);
const currentUserRequest = await fetch("/api/v2/users/me.json");
const currentUser = await currentUserRequest.json();
const customFields = requestFields.map((field) => {
if (field.type !== "subject" && field.type !== "description") {
return {
id: field.id,
value: field.value,
};
} else return;
});
const response = await fetch("/api/v2/requests", {
method: "POST",
headers: {
Expand All @@ -25,20 +34,19 @@ export function useServiceFormSubmit(
body: serviceCatalogItem.description,
},
ticket_form_id: serviceCatalogItem.form_id,
custom_fields: requestFields.map((field) => {
if (field.type !== "subject" && field.type !== "description") {
return {
id: field.id,
value: field.value,
};
} else return;
}),
custom_fields: [
...customFields,
{ id: associatedLookupField?.id, value: serviceCatalogItem.id },
],
via: {
channel: "web form",
source: 50,
},
locale: baseLocale,
},
}),
});
const data = await response.json();
const redirectUrl = "/hc/requests/" + data.request.id;
window.location.href = redirectUrl;
return response;
};

return { submitServiceItemRequest };
Expand Down

0 comments on commit 2df4d53

Please sign in to comment.