|
| 1 | +import graphqlClient from "@/shared/api/graphql/graphqlClientApollo"; |
| 2 | +import { Button, ButtonProps } from "@/shared/components/buttons/button-primitive"; |
| 3 | +import { ALERT_TYPES, Alert } from "@/shared/components/ui/alert"; |
| 4 | +import { Combobox, ComboboxContent } from "@/shared/components/ui/combobox"; |
| 5 | +import { PopoverTrigger } from "@/shared/components/ui/popover"; |
| 6 | +import { inputStyle } from "@/shared/components/ui/style"; |
| 7 | +import { classNames } from "@/shared/utils/common"; |
| 8 | +import { capitalizeFirstLetter } from "@/shared/utils/string"; |
| 9 | +import { Icon } from "@iconify-icon/react"; |
| 10 | +import { useState } from "react"; |
| 11 | +import { toast } from "react-toastify"; |
| 12 | +import { useUpdateReview } from "../../domain/update-review.mutation"; |
| 13 | +import { ActionComboboxList } from "./actions-combobox-list"; |
| 14 | + |
| 15 | +interface PcActionButtonProps extends ButtonProps { |
| 16 | + proposedChangeId: string; |
| 17 | + approvers: Array<any>; |
| 18 | + state: "closed" | "open" | "merged"; |
| 19 | +} |
| 20 | + |
| 21 | +export const PcActionButton = ({ proposedChangeId }: PcActionButtonProps) => { |
| 22 | + const [open, setOpen] = useState(false); |
| 23 | + const [action, setAction] = useState("approve"); |
| 24 | + |
| 25 | + const { mutateAsync: updateObjectMutateAsync, isPending: isObjectUpdatePending } = |
| 26 | + useUpdateReview({ |
| 27 | + onSuccess: async () => { |
| 28 | + await graphqlClient.reFetchObservableQueries(); |
| 29 | + toast(<Alert type={ALERT_TYPES.SUCCESS} message="Proposed change updated!" />); |
| 30 | + }, |
| 31 | + onError: async () => { |
| 32 | + await graphqlClient.reFetchObservableQueries(); |
| 33 | + toast( |
| 34 | + <Alert |
| 35 | + type={ALERT_TYPES.SUCCESS} |
| 36 | + message="An error occured while updating the proposed changes" |
| 37 | + /> |
| 38 | + ); |
| 39 | + }, |
| 40 | + }); |
| 41 | + |
| 42 | + const { mutateAsync: updateApprovalMutateAsync, isPending: isApprovalUpdatePending } = |
| 43 | + useUpdateReview({ |
| 44 | + onSuccess: async () => { |
| 45 | + await graphqlClient.reFetchObservableQueries(); |
| 46 | + toast(<Alert type={ALERT_TYPES.SUCCESS} message="Proposed change approved!" />); |
| 47 | + }, |
| 48 | + onError: async () => { |
| 49 | + await graphqlClient.reFetchObservableQueries(); |
| 50 | + toast( |
| 51 | + <Alert |
| 52 | + type={ALERT_TYPES.SUCCESS} |
| 53 | + message="An error occured while approving the proposed changes" |
| 54 | + /> |
| 55 | + ); |
| 56 | + }, |
| 57 | + }); |
| 58 | + |
| 59 | + const handleAction = () => { |
| 60 | + switch (action) { |
| 61 | + case "cancel-reject": |
| 62 | + case "reject": |
| 63 | + case "cancel-approve": |
| 64 | + case "approve": { |
| 65 | + return updateApprovalMutateAsync({ |
| 66 | + proposedChangeId, |
| 67 | + decision: action, |
| 68 | + }); |
| 69 | + } |
| 70 | + case "merge": |
| 71 | + case "close": { |
| 72 | + return updateObjectMutateAsync({ |
| 73 | + proposedChangeId, |
| 74 | + decision: action, |
| 75 | + }); |
| 76 | + } |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + const isLoading = isApprovalUpdatePending || isObjectUpdatePending; |
| 81 | + |
| 82 | + return ( |
| 83 | + <Combobox open={open} onOpenChange={setOpen}> |
| 84 | + <PopoverTrigger asChild> |
| 85 | + <div className={classNames(inputStyle, "flex p-0 border-0 ")}> |
| 86 | + <Button |
| 87 | + className="grow flex flex-wrap gap-2 h-full rounded-r-none border-r-white" |
| 88 | + onClick={handleAction} |
| 89 | + variant={"primary"} |
| 90 | + isLoading={isLoading} |
| 91 | + disabled={isLoading} |
| 92 | + > |
| 93 | + {capitalizeFirstLetter(action)} |
| 94 | + </Button> |
| 95 | + |
| 96 | + <Button |
| 97 | + className="h-full rounded-l-none border-l-0" |
| 98 | + variant={"primary"} |
| 99 | + size={"sm"} |
| 100 | + onClick={() => { |
| 101 | + setOpen(true); |
| 102 | + }} |
| 103 | + disabled={isLoading} |
| 104 | + > |
| 105 | + <Icon icon="mdi:unfold-more-horizontal" /> |
| 106 | + </Button> |
| 107 | + </div> |
| 108 | + </PopoverTrigger> |
| 109 | + <ComboboxContent fitTriggerWidth={false}> |
| 110 | + <ActionComboboxList |
| 111 | + value={action} |
| 112 | + onSelect={(action) => { |
| 113 | + setOpen(false); |
| 114 | + setAction(action); |
| 115 | + }} |
| 116 | + /> |
| 117 | + </ComboboxContent> |
| 118 | + </Combobox> |
| 119 | + ); |
| 120 | +}; |
0 commit comments