11import { useEffect , useState } from "react" ;
2+ import { useParams } from "react-router-dom" ;
23import { Card , CardContent , CardHeader , CardTitle } from "@/components/ui/card" ;
34import { Input } from "@/components/ui/input" ;
45import {
@@ -28,6 +29,7 @@ export default function BillingDetailCard({
2829 const [ details , setDetails ] = useState <
2930 Array < { id : number ; paidFor : number ; amount : number | "" } >
3031 > ( [ { id : - 1 , paidFor : - 1 , amount : "" } ] ) ;
32+ const { eventId } = useParams ( ) ;
3133
3234 // payerに関連するpaymentsをdetailsに反映
3335 useEffect ( ( ) => {
@@ -45,7 +47,7 @@ export default function BillingDetailCard({
4547 setDetails ( newDetails ) ;
4648 } , [ payments , paidBy . id ] ) ;
4749
48- const handleReceiverChange = ( index : number , value : string ) => {
50+ const handleReceiverChange = async ( index : number , value : string ) => {
4951 const newValue = value === "none" ? - 1 : Number ( value ) ;
5052 setDetails ( ( prev ) => {
5153 const updated = [ ...prev ] ;
@@ -67,6 +69,19 @@ export default function BillingDetailCard({
6769 } ;
6870 setPayments ( ( prev ) => [ ...prev , newPayment ] ) ;
6971
72+ await fetch ( "http://127.0.0.1:8000/api/v1/payments/" , {
73+ method : "POST" ,
74+ headers : { "Content-Type" : "application/json" } ,
75+ body : JSON . stringify ( {
76+ event_id : eventId ,
77+ payment_id : newPayment . id ,
78+ paid_by : newPayment . paidBy ,
79+ paid_for : newPayment . paidFor ,
80+ amount : 0 ,
81+ note : "" ,
82+ } ) ,
83+ } ) ;
84+
7085 // detailsのidも更新
7186 setDetails ( ( prev ) => {
7287 const updated = [ ...prev ] ;
@@ -86,17 +101,6 @@ export default function BillingDetailCard({
86101 const updated = [ ...prev ] ;
87102 updated [ index ] . amount = v ;
88103
89- const detail = updated [ index ] ;
90-
91- // 支払い情報が既存なら即時反映
92- if ( detail . id !== - 1 && v !== "" ) {
93- setPayments ( ( prevPayments ) =>
94- prevPayments . map ( ( p ) =>
95- p . id === detail . id ? { ...p , amount : Number ( v ) } : p ,
96- ) ,
97- ) ;
98- }
99-
100104 const last = updated [ updated . length - 1 ] ;
101105 if ( last . amount !== "" && last . paidFor !== - 1 ) {
102106 updated . push ( { id : - 1 , paidFor : - 1 , amount : "" } ) ;
@@ -106,28 +110,10 @@ export default function BillingDetailCard({
106110 } ) ;
107111 } ;
108112
109- const handleBlur = ( index : number ) => {
113+ const handleBlur = async ( index : number ) => {
110114 const detail = details [ index ] ;
111115
112- if ( detail . id === - 1 && detail . paidFor !== - 1 && detail . amount !== "" ) {
113- // 新規作成
114- const newId =
115- payments . length > 0 ? Math . max ( ...payments . map ( ( p ) => p . id ) ) + 1 : 0 ;
116- const newPayment : Payment = {
117- id : newId ,
118- paidBy : paidBy . id ,
119- paidFor : detail . paidFor ,
120- amount : Number ( detail . amount ) ,
121- } ;
122- setPayments ( ( prev ) => [ ...prev , newPayment ] ) ;
123-
124- // detailsのidも更新
125- setDetails ( ( prev ) => {
126- const updated = [ ...prev ] ;
127- updated [ index ] . id = newId ;
128- return updated ;
129- } ) ;
130- } else if ( detail . id !== - 1 ) {
116+ if ( detail . id !== - 1 ) {
131117 // 既存を更新
132118 setPayments ( ( prev ) =>
133119 prev . map ( ( p ) =>
@@ -136,6 +122,17 @@ export default function BillingDetailCard({
136122 ) ;
137123 }
138124
125+ await fetch ( `http://127.0.0.1:8000/api/v1/payments/patch_by_key/` , {
126+ method : "PATCH" ,
127+ headers : { "Content-Type" : "application/json" } ,
128+ body : JSON . stringify ( {
129+ event_id : eventId ,
130+ payment_id : detail . id ,
131+ amount : detail . amount ,
132+ note : "" ,
133+ } ) ,
134+ } ) ;
135+
139136 if (
140137 index === details . length - 1 &&
141138 ( detail . paidFor !== - 1 || detail . amount !== "" )
@@ -144,13 +141,20 @@ export default function BillingDetailCard({
144141 }
145142 } ;
146143
147- const handleDeleteBilling = ( index : number ) => {
144+ const handleDeleteBilling = async ( index : number ) => {
148145 const target = details [ index ] ;
149146
150147 if ( ! confirm ( "本当に削除しますか?" ) ) return ;
151148
152149 if ( target . id !== - 1 ) {
153150 setPayments ( ( prev ) => prev . filter ( ( p ) => p . id !== target . id ) ) ;
151+
152+ await fetch (
153+ `http://127.0.0.1:8000/api/v1/payments/delete_by_key/?event_id=${ eventId } &payment_id=${ target . id } ` ,
154+ {
155+ method : "DELETE" ,
156+ } ,
157+ ) ;
154158 }
155159
156160 setDetails ( ( prev ) => prev . filter ( ( _ , i ) => i !== index ) ) ;
0 commit comments