@@ -7,7 +7,7 @@ import { useCallback, useRef, useState } from "react";
77import { useApiClient } from "@/hooks/useApiClient" ;
88import Portal from "./baseTable/Portal" ;
99import toast from "react-hot-toast" ;
10- import { CheckCircle , DotsThree } from "@phosphor-icons/react" ;
10+ import { CheckCircle , DotsThree , XCircle } from "@phosphor-icons/react" ;
1111import DistributionsGeneralItemChipGroup from "./chips/DistributionsGeneralItemChipGroup" ;
1212import { TableDistribution } from "@/types/api/distribution.types" ;
1313import { useUser } from "@/components/context/UserContext" ;
@@ -168,10 +168,35 @@ function OptionsButton({
168168 error : "Failed to approve distribution." ,
169169 } ) ;
170170
171- await promise ;
171+ try {
172+ await promise ;
173+ fetchTableData ( ) ;
174+ } finally {
175+ setIsDropdownOpen ( false ) ;
176+ }
172177
173- setIsDropdownOpen ( false ) ;
174- fetchTableData ( ) ;
178+ }
179+
180+ async function unapproveDistribution ( ) {
181+ const formData = new FormData ( ) ;
182+ formData . append ( "pending" , "true" ) ;
183+
184+ const promise = apiClient . patch ( `/api/distributions/${ distribution . id } ` , {
185+ body : formData ,
186+ } ) ;
187+
188+ toast . promise ( promise , {
189+ loading : "Unapproving distribution..." ,
190+ success : "Distribution unapproved!" ,
191+ error : "Failed to unapprove distribution." ,
192+ } ) ;
193+
194+ try {
195+ await promise ;
196+ fetchTableData ( ) ;
197+ } finally {
198+ setIsDropdownOpen ( false ) ;
199+ }
175200 }
176201
177202 return (
@@ -190,13 +215,24 @@ function OptionsButton({
190215 position = "bottom-right"
191216 className = "w-48 rounded-md bg-white shadow-lg ring-1 ring-black/5 py-1"
192217 >
193- < button
194- onClick = { approveDistribution }
195- className = "flex items-center w-full px-4 py-2 text-sm text-left transition-colors duration-150 hover:bg-gray-50 text-gray-900 cursor-pointer"
196- >
197- < CheckCircle size = { 16 } className = "mr-3 flex-shrink-0" />
198- < p > Approve</ p >
199- </ button >
218+ { distribution . pending ? (
219+ < button
220+ onClick = { approveDistribution }
221+ className = "flex items-center w-full px-4 py-2 text-sm text-left transition-colors duration-150 hover:bg-gray-50 text-gray-900 cursor-pointer"
222+ >
223+ < CheckCircle size = { 16 } className = "mr-3 flex-shrink-0" />
224+ < p > Approve</ p >
225+ </ button >
226+ ) : (
227+ < button
228+ onClick = { unapproveDistribution }
229+ className = "flex items-center w-full px-4 py-2 text-sm text-left transition-colors duration-150 hover:bg-gray-50 text-gray-900 cursor-pointer"
230+ >
231+ < XCircle size = { 16 } className = "mr-3 flex-shrink-0" />
232+ < p > Unapprove</ p >
233+ </ button >
234+
235+ ) }
200236 </ Portal >
201237 </ div >
202238 ) ;
0 commit comments