Skip to content

Commit

Permalink
B 2764 add mutation to cancel application (#639)
Browse files Browse the repository at this point in the history
  • Loading branch information
haydencleary authored Nov 14, 2024
1 parent 8705e71 commit 57a16d3
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./use-accept-application";
export * from "./use-get-application-by-id";
export * from "./use-patch-application";
export * from "./use-delete-application";
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { useMutation } from "@tanstack/react-query";

import {
UseMutationFacadeParams,
useMutationAdapter,
} from "@/core/application/react-query-adapter/helpers/use-mutation-adapter";
import { bootstrap } from "@/core/bootstrap";
import { DeleteApplicationBody } from "@/core/domain/application/application-contract.types";
import { ApplicationFacadePort } from "@/core/domain/application/input/application-facade-port";

export function useDeleteApplication({
pathParams,
options,
}: UseMutationFacadeParams<ApplicationFacadePort["deleteApplication"], undefined, never, DeleteApplicationBody>) {
const applicationStoragePort = bootstrap.getApplicationStoragePortForClient();

return useMutation(
useMutationAdapter({
...applicationStoragePort.deleteApplication({ pathParams }),
options: {
...options,
onSuccess: async (data, variables, context) => {
// TODO invalidate application list query

options?.onSuccess?.(data, variables, context);
},
},
})
);
}
10 changes: 10 additions & 0 deletions core/domain/application/application-contract.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,13 @@ export type GetApplicationByIdPortParams = HttpClientParameters<{ PathParams: Ge
export type GetApplicationByIdResponse = components["schemas"]["ProjectApplicationResponse"];

export type GetApplicationByIdPortResponse = HttpStorageResponse<GetApplicationByIdResponse>;

/* ----------------------------- Delete application ---------------------------- */

export type DeleteApplicationBody = components["schemas"]["ProjectApplicationDeleteRequest"];

export type DeleteApplicationPathParams = operations["deleteProjectApplication"]["parameters"]["path"];

export type DeleteApplicationPortParams = HttpClientParameters<{ PathParams: DeleteApplicationPathParams }>;

export type DeleteApplicationPortResponse = HttpStorageResponse;
3 changes: 3 additions & 0 deletions core/domain/application/input/application-facade-port.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
AcceptApplicationPortParams,
AcceptApplicationPortResponse,
DeleteApplicationPortParams,
DeleteApplicationPortResponse,
GetApplicationByIdPortParams,
GetApplicationByIdPortResponse,
PatchApplicationPortParams,
Expand All @@ -11,4 +13,5 @@ export interface ApplicationFacadePort {
patchApplication(p: PatchApplicationPortParams): PatchApplicationPortResponse;
acceptApplication(p: AcceptApplicationPortParams): AcceptApplicationPortResponse;
getApplicationById(p: GetApplicationByIdPortParams): GetApplicationByIdPortResponse;
deleteApplication(p: DeleteApplicationPortParams): DeleteApplicationPortResponse;
}
3 changes: 3 additions & 0 deletions core/domain/application/outputs/application-storage-port.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
AcceptApplicationPortParams,
AcceptApplicationPortResponse,
DeleteApplicationPortParams,
DeleteApplicationPortResponse,
GetApplicationByIdPortParams,
GetApplicationByIdPortResponse,
PatchApplicationPortParams,
Expand All @@ -12,4 +14,5 @@ export interface ApplicationStoragePort {
patchApplication(p: PatchApplicationPortParams): PatchApplicationPortResponse;
acceptApplication(p: AcceptApplicationPortParams): AcceptApplicationPortResponse;
getApplicationById(p: GetApplicationByIdPortParams): GetApplicationByIdPortResponse;
deleteApplication(p: DeleteApplicationPortParams): DeleteApplicationPortResponse;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { GetApplicationByIdResponse, PatchApplicationBody } from "@/core/domain/application/application-contract.types";
import {
DeleteApplicationBody,
GetApplicationByIdResponse,
PatchApplicationBody,
} from "@/core/domain/application/application-contract.types";
import { Application } from "@/core/domain/application/model/application-model";
import { ApplicationStoragePort } from "@/core/domain/application/outputs/application-storage-port";
import { HttpClient } from "@/core/infrastructure/marketplace-api-client-adapter/http/http-client/http-client";
Expand All @@ -11,6 +15,7 @@ export class ApplicationClientAdapter implements ApplicationStoragePort {
patchApplication: "applications/:applicationId",
acceptApplication: "applications/:applicationId/accept",
getApplicationById: "applications/:applicationId",
deleteApplication: "applications/:applicationId",
} as const;

patchApplication = ({ pathParams }: FirstParameter<ApplicationStoragePort["patchApplication"]>) => {
Expand Down Expand Up @@ -72,4 +77,24 @@ export class ApplicationClientAdapter implements ApplicationStoragePort {
tag,
};
};

deleteApplication = ({ pathParams }: FirstParameter<ApplicationStoragePort["deleteApplication"]>) => {
const path = this.routes["deleteApplication"];
const method = "DELETE";
const tag = HttpClient.buildTag({ path, pathParams });

const request = async (body: DeleteApplicationBody) =>
this.client.request<never>({
path,
method,
tag,
pathParams,
body: JSON.stringify(body),
});

return {
request,
tag,
};
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ export class ApplicationClientAdapterMock implements ApplicationStoragePort {
acceptApplication = mockHttpStorageResponse<ApplicationStoragePort["acceptApplication"]>;

getApplicationById = mockHttpStorageResponse<ApplicationStoragePort["getApplicationById"]>;

deleteApplication = mockHttpStorageResponse<ApplicationStoragePort["deleteApplication"]>;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { useState } from "react";

import { ApplicationReactQueryAdapter } from "@/core/application/react-query-adapter/application";
import { ContributionAs } from "@/core/domain/contribution/models/contribution.types";

import { Button } from "@/design-system/atoms/button/variants/button-default";
import { Tooltip } from "@/design-system/atoms/tooltip";
import { CheckboxButton } from "@/design-system/molecules/checkbox-button";
import { toast } from "@/design-system/molecules/toaster";

import { BaseLink } from "@/shared/components/base-link/base-link";
import { useGithubPermissionsContext } from "@/shared/features/github-permissions/github-permissions.context";
import { useSidePanelsContext } from "@/shared/features/side-panels/side-panels.context";
import { useAuthUser } from "@/shared/hooks/auth/use-auth-user";
import { useContributionActions } from "@/shared/hooks/contributions/use-contribution-actions";
import { UseContributionPanelFooter } from "@/shared/panels/contribution-sidepanel/_features/footer/footer.types";
import { Translate } from "@/shared/translation/components/translate/translate";
Expand Down Expand Up @@ -87,13 +91,33 @@ export const useContributionPanelFooterAsMaintainer = ({

export const useContributionPanelFooterAsContributor = ({ contribution }: UseContributionPanelFooter) => {
const [shouldDeleteComment, setShouldDeleteComment] = useState(false);
const { close } = useSidePanelsContext();

const { githubUserId } = useAuthUser();

const applicationId =
contribution.applicants.find(applicant => applicant.githubUserId === githubUserId)?.applicationId ?? "";

// TODO handle Github permissions
const { mutate, isPending } = ApplicationReactQueryAdapter.client.useDeleteApplication({
pathParams: { applicationId },
options: {
onSuccess: () => {
close();
toast.success(<Translate token={"panels:contribution.footer.tooltip.cancelApplication.success"} />);
},
onError: () => {
toast.error(<Translate token={"panels:contribution.footer.tooltip.cancelApplication.error"} />);
},
},
});

if (contribution.isArchived()) {
return <div />;
}

function onCancelApplication() {
console.log(`Should cancel : true & should delete comment : ${shouldDeleteComment}`);
mutate({ deleteGithubComment: shouldDeleteComment });
}

if (contribution?.isNotAssigned()) {
Expand All @@ -111,6 +135,7 @@ export const useContributionPanelFooterAsContributor = ({ contribution }: UseCon
size={"md"}
variant={"secondary"}
onClick={onCancelApplication}
isLoading={isPending}
translate={{ token: "panels:contribution.footer.actions.asContributor.cancelApplication" }}
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
"cancelApplication": "Cancel application",
"seePrOnGithub": "See PR on GitHub"
}
},
"tooltip": {
"cancelApplication": {
"success": "Application canceled successfully.",
"error": "An error occurred while canceling the application."
}
}
},
"helper": {
Expand Down

0 comments on commit 57a16d3

Please sign in to comment.