Skip to content

Commit

Permalink
Applications form: Fix contributors field handling (and other stuff)
Browse files Browse the repository at this point in the history
The `contributors` field on the `Application` payload needs to be
a pure `Ref` object or it will be rejected by the REST API call.
Adopt the same set of data transforms used in the archetype-form to
handle getting the correct set of data.

Related changes:
  - REST `createApplication()` function updated to have the proper
    return type (response data is not unwrapped)

  - Query `useCreateApplicationMutation()` updated to properly pass the
    newly created `Application` to the `onSuccess()` handler

  - `onCreateApplicationSuccess()` in the application form updated to
    use the correct onSuccess() response data

Signed-off-by: Scott J Dickerson <sdickers@redhat.com>
  • Loading branch information
sjd78 committed Sep 27, 2023
1 parent e269b74 commit 43a14f4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
4 changes: 2 additions & 2 deletions client/src/app/api/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ export const deleteIdentity = (identity: Identity): AxiosPromise => {

// Axios direct

export const createApplication = (obj: Application): Promise<Application> =>
axios.post(`${APPLICATIONS}`, obj);
export const createApplication = (data: Application) =>
axios.post<Application>(`${APPLICATIONS}`, data);

export const deleteApplication = (id: number): Promise<Application> =>
axios.delete(`${APPLICATIONS}/${id}`);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { AxiosError, AxiosResponse } from "axios";
import { AxiosError } from "axios";
import { object, string } from "yup";
import {
ActionGroup,
Expand All @@ -16,7 +16,7 @@ import { yupResolver } from "@hookform/resolvers/yup";

import { SimpleSelect, OptionWithValue } from "@app/components/SimpleSelect";
import { DEFAULT_SELECT_MAX_HEIGHT } from "@app/Constants";
import { Application, TagRef } from "@app/api/models";
import { Application, Ref, TagRef } from "@app/api/models";
import {
customURLValidation,
duplicateNameCheck,
Expand Down Expand Up @@ -104,7 +104,7 @@ export const ApplicationForm: React.FC<ApplicationFormProps> = ({
if (tagCategories) {
setTags(tagCategories.flatMap((f) => f.tags || []));
}
}, []);
}, [tagCategories]);

const tagOptions =
tags?.map((tag) => {
Expand Down Expand Up @@ -272,11 +272,11 @@ export const ApplicationForm: React.FC<ApplicationFormProps> = ({
}
};

const onCreateApplicationSuccess = (response: AxiosResponse<Application>) => {
const onCreateApplicationSuccess = (data: Application) => {
pushNotification({
title: t("toastr.success.createWhat", {
type: t("terms.application"),
what: response.data.name,
what: data.name,
}),
variant: "success",
});
Expand Down Expand Up @@ -320,9 +320,15 @@ export const ApplicationForm: React.FC<ApplicationFormProps> = ({
(stakeholder) => formValues?.owner === stakeholder.name
);

const matchingContributors = stakeholders?.filter((stakeholder) =>
formValues.contributors.includes(stakeholder.name)
);
const contributors =
formValues.contributors === undefined
? undefined
: (formValues.contributors
.map((name) => stakeholders.find((s) => s.name === name))
.map<Ref | undefined>((sh) =>
!sh ? undefined : { id: sh.id, name: sh.name }
)
.filter(Boolean) as Ref[]);

const payload: Application = {
name: formValues.name.trim(),
Expand All @@ -338,7 +344,7 @@ export const ApplicationForm: React.FC<ApplicationFormProps> = ({
owner: matchingOwner
? { id: matchingOwner.id, name: matchingOwner.name }
: undefined,
contributors: matchingContributors,
contributors,
...(formValues.sourceRepository
? {
repository: {
Expand Down Expand Up @@ -445,7 +451,7 @@ export const ApplicationForm: React.FC<ApplicationFormProps> = ({
name="tags"
label={t("terms.tags")}
fieldId="tags"
renderInput={({ field: { value, name, onChange } }) => (
renderInput={({ field: { value, onChange } }) => (
<Autocomplete
noResultsMessage={t("message.noResultsFoundTitle")}
onChange={(selections) => {
Expand Down
8 changes: 4 additions & 4 deletions client/src/app/queries/applications.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { AxiosError } from "axios";

import { MimeType } from "@app/api/models";
import { Application, MimeType } from "@app/api/models";
import {
createApplication,
deleteApplication,
Expand Down Expand Up @@ -89,14 +89,14 @@ export const useUpdateAllApplicationsMutation = (
};

export const useCreateApplicationMutation = (
onSuccess: (res: any) => void,
onSuccess: (data: Application) => void,
onError: (err: AxiosError) => void
) => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: createApplication,
onSuccess: (res) => {
onSuccess(res);
onSuccess: ({ data }) => {
onSuccess(data);
queryClient.invalidateQueries([ApplicationsQueryKey]);
},
onError: onError,
Expand Down

0 comments on commit 43a14f4

Please sign in to comment.