1+ /**
2+ * MIT License
3+ *
4+ * Copyright (c) 2025 Ronan LE MEILLAT
5+ *
6+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7+ * of this software and associated documentation files (the "Software"), to deal
8+ * in the Software without restriction, including without limitation the rights
9+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+ * copies of the Software, and to permit persons to whom the Software is
11+ * furnished to do so, subject to the following conditions:
12+ *
13+ * The above copyright notice and this permission notice shall be included in all
14+ * copies or substantial portions of the Software.
15+ *
16+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+ * SOFTWARE.
23+ */
24+
125import { Button } from "@heroui/button" ;
226import { useTranslation } from "react-i18next" ;
327import ButtonAddFeedbackOrReturn from "@/components/button-add-feedback-or-return" ;
428import { PurchaseStatus } from "@/types/db" ;
529
30+ /**
31+ * Props interface for the ActionCell component
32+ * Defines the structure of data and callback functions needed
33+ */
634interface ActionCellProps {
35+ /** The purchase item data containing status information */
736 item : PurchaseStatus ;
37+ /** Whether the current user has write permissions (null while loading) */
838 hasWritePermission : boolean | null ;
39+ /** Callback function to create feedback for a purchase */
940 onCreateFeedback : ( purchaseId : string , amount : number ) => void ;
41+ /** Callback function to return/return a purchase */
1042 onReturnItem : ( purchaseId : string , amount : number ) => void ;
43+ /** Callback function to publish feedback for a purchase */
1144 onPublishFeedback : ( purchaseId : string , amount : number ) => void ;
45+ /** Callback function to refund a purchase */
1246 onRefundPurchase : ( purchaseId : string , amount : number ) => void ;
1347}
1448
1549/**
16- * Component for rendering the action column in the purchase table
17- * Shows different actions based on purchase status and user permissions
50+ * ActionCell Component
51+ *
52+ * This component renders the action buttons for each row in the purchase table.
53+ * The actions available depend on the purchase status and user permissions:
54+ *
55+ * - If the purchase is refunded: Shows "Refunded" status text
56+ * - If no feedback exists and user has write permission: Shows buttons to create feedback or return item
57+ * - If feedback exists but not published and user has write permission: Shows "Publish Feedback" button
58+ * - If feedback is published and user has write permission: Shows "Refund" button
59+ * - If user lacks write permission: Shows no actions
60+ *
61+ * The component uses conditional rendering to display appropriate actions based on:
62+ * 1. Purchase status (refunded, hasFeedback, hasPublication)
63+ * 2. User permissions (hasWritePermission)
64+ * 3. Loading state (when permissions are still being checked)
65+ *
66+ * @param props - The component props containing item data and callback functions
67+ * @returns JSX element representing the action cell content
1868 */
1969export const ActionCell = ( {
2070 item,
@@ -24,27 +74,35 @@ export const ActionCell = ({
2474 onPublishFeedback,
2575 onRefundPurchase,
2676} : ActionCellProps ) => {
77+ // Hook to access translation functions for internationalization
2778 const { t } = useTranslation ( ) ;
2879
29- // Early return if permissions are still loading
80+ // Show loading text while permissions are being checked
81+ // This prevents showing incorrect action buttons during the loading phase
3082 if ( hasWritePermission === null ) {
3183 return < span className = "text-gray-400" > { t ( "loading" ) } </ span > ;
3284 }
3385
34- // If refunded, show refunded status
86+ // If the purchase has been refunded, show the refunded status
87+ // No further actions are possible for refunded purchases
3588 if ( item . refunded ) {
3689 return < span className = "text-green-500" > { t ( "refunded" ) } </ span > ;
3790 }
3891
39- // If no feedback and user has write permission, show create feedback/return options
92+ // If no feedback exists and user has write permission,
93+ // show the combined button for creating feedback or returning the item
4094 if ( ! item . hasFeedback && hasWritePermission ) {
4195 return (
4296 < div className = "flex gap-2" >
97+ { /* Custom button component that handles both feedback creation and item return */ }
4398 < ButtonAddFeedbackOrReturn
4499 onAction = { ( key ) => {
100+ // Handle the action based on the key returned by the button
45101 if ( key === "feedback" ) {
102+ // Call the callback to create feedback for this purchase
46103 onCreateFeedback ( item . purchase , item . amount ) ;
47104 } else if ( key === "return" ) {
105+ // Call the callback to return this purchase
48106 onReturnItem ( item . purchase , item . amount ) ;
49107 }
50108 } }
0 commit comments