11import type { NextPage } from 'next'
2- import { useLanguage , type Languages } from '@helpwave/common/hooks/useLanguage'
2+ import { type Languages } from '@helpwave/common/hooks/useLanguage'
33import { useTranslation , type PropsForTranslation } from '@helpwave/common/hooks/useTranslation'
44import { Page } from '@/components/layout/Page'
55import titleWrapper from '@/utils/titleWrapper'
@@ -16,6 +16,7 @@ import { withAuth } from '@/hooks/useAuth'
1616import { withOrganization } from '@/hooks/useOrganization'
1717import { useMyInvoicesQuery } from '@/api/mutations/invoice_mutations'
1818import EmbeddedCheckoutButton from '@/components/forms/StripeCheckOutForm'
19+ import { defaultLocaleFormatters } from '@/utils/locale'
1920
2021type InvoicesTranslation = {
2122 invoices : string ,
@@ -103,39 +104,9 @@ const PaymentDisplay = ({ amount }: PaymentDisplayProps) => {
103104
104105const Invoices : NextPage < PropsForTranslation < InvoicesTranslation , InvoicesServerSideProps > > = ( { overwriteTranslation } ) => {
105106 const translation = useTranslation ( defaultInvoicesTranslations , overwriteTranslation )
106- const { isError, isLoading, data } = useMyInvoicesQuery ( )
107- const language = useLanguage ( )
108-
109- function formatDate ( date ?: Date ) {
110- if ( date == null || date == undefined ) {
111- return null
112- }
113-
114- const languageToLocaleMapping = {
115- de : 'de-DE' ,
116- en : 'en-US' ,
117- }
118-
119- const locale = languageToLocaleMapping [ language . language ] || 'en-US'
120-
121-
122- const options : Intl . DateTimeFormatOptions = {
123- year : 'numeric' ,
124- month : '2-digit' ,
125- day : '2-digit' ,
126- hour : '2-digit' ,
127- minute : '2-digit' ,
128- hour12 : locale === 'en-US'
129- }
130-
131- let formatted = date . toLocaleString ( locale , options )
132-
133- if ( locale === 'de-DE' ) {
134- formatted = formatted . replace ( / ( \d { 2 } : \d { 2 } ) $ / , '$1 Uhr' )
135- }
107+ const localeTranslation = useTranslation ( defaultLocaleFormatters )
136108
137- return formatted
138- }
109+ const { isError, isLoading, data } = useMyInvoicesQuery ( )
139110
140111 return (
141112 < Page pageTitle = { titleWrapper ( translation . invoices ) } >
@@ -144,78 +115,78 @@ const Invoices: NextPage<PropsForTranslation<InvoicesTranslation, InvoicesServer
144115 < h2 className = { tw ( 'font-bold font-space text-3xl' ) } > { translation . invoices } </ h2 >
145116 { ! isError && ! isLoading && data && (
146117 < PaymentDisplay
147- amount = { data . filter ( value => value . status !== 'paid' ) . reduce ( ( previousValue , currentValue ) => previousValue + currentValue . totalAmount , 0 ) } />
118+ amount = { data . filter ( value => value . status !== 'paid' ) . reduce ( ( previousValue , currentValue ) => previousValue + currentValue . totalAmount , 0 ) } />
148119 ) }
149120 </ div >
150121 { ( isError ) && ( < span className = { tw ( 'There was an Error' ) } > </ span > ) }
151- { ! isError && isLoading && ( < LoadingAnimation /> ) }
122+ { ! isError && isLoading && ( < LoadingAnimation /> ) }
152123 { ! isError && ! isLoading && data . length > 0 ? (
153- < div className = { tw ( 'flex flex-wrap gap-x-8 gap-y-12' ) } >
154- < Table
155- className = { tw ( 'w-full h-full' ) }
156- data = { data }
157- identifierMapping = { dataObject => dataObject . uuid }
158- header = { [
159- ( < span key = "date" > { translation . date } </ span > ) ,
160- ( < span key = "name" > { translation . title } </ span > ) ,
161- ( < span key = "price" > { translation . price } </ span > ) ,
162- ( < span key = "paymentStatus" > { translation . paymentStatus } </ span > ) ,
163- ( < span key = "paymentDate" > { translation . paymentDate } </ span > ) ,
164- ( < span key = "actions" > { translation . actions } </ span > ) ,
165- ] }
166- rowMappingToCells = { invoice => [
167- ( < span key = { invoice . uuid + '-date' } > { formatDate ( invoice . date ) } </ span > ) ,
168- ( < span key = { invoice . uuid + '-name' } > { invoice . title } </ span > ) ,
169- (
170- < span key = { invoice . uuid + '-price' } className = { tw ( 'font-semibold' ) } >
171- { invoice . totalAmount + '€' }
124+ < div className = { tw ( 'flex flex-wrap gap-x-8 gap-y-12' ) } >
125+ < Table
126+ className = { tw ( 'w-full h-full' ) }
127+ data = { data }
128+ identifierMapping = { dataObject => dataObject . uuid }
129+ header = { [
130+ ( < span key = "date" > { translation . date } </ span > ) ,
131+ ( < span key = "name" > { translation . title } </ span > ) ,
132+ ( < span key = "price" > { translation . price } </ span > ) ,
133+ ( < span key = "paymentStatus" > { translation . paymentStatus } </ span > ) ,
134+ ( < span key = "paymentDate" > { translation . paymentDate } </ span > ) ,
135+ ( < span key = "actions" > { translation . actions } </ span > ) ,
136+ ] }
137+ rowMappingToCells = { invoice => [
138+ ( < span key = { invoice . uuid + '-date' } > { localeTranslation . formatDate ( invoice . date ) } </ span > ) ,
139+ ( < span key = { invoice . uuid + '-name' } > { invoice . title } </ span > ) ,
140+ (
141+ < span key = { invoice . uuid + '-price' } className = { tw ( 'font-semibold' ) } >
142+ { localeTranslation . formatMoney ( invoice . totalAmount ) }
172143 </ span >
173- ) ,
174- (
175- < span
176- key = { invoice . uuid + '-payment-status' }
177- className = { tx ( 'w-full px-4 py-2 rounded-full' , {
178- 'bg-hw-negative-500 text-white' : invoice . status === 'overdue' ,
179- 'bg-hw-warn-500 text-white' : invoice . status === 'pending' ,
180- 'bg-hw-positive-500 text-white' : invoice . status === 'paid'
181- } ) }
182- >
144+ ) ,
145+ (
146+ < span
147+ key = { invoice . uuid + '-payment-status' }
148+ className = { tx ( 'w-full px-4 py-2 rounded-full' , {
149+ 'bg-hw-negative-500 text-white' : invoice . status === 'overdue' ,
150+ 'bg-hw-warn-500 text-white' : invoice . status === 'pending' ,
151+ 'bg-hw-positive-500 text-white' : invoice . status === 'paid'
152+ } ) }
153+ >
183154 { translation [ invoice . status ] }
184155 </ span >
185- ) ,
186- (
187- < span key = { invoice . uuid + '-payment-date' } >
188- { invoice . status === 'paid' && invoice . createdAt != undefined ? formatDate ( invoice . updatedAt ) : '-' }
156+ ) ,
157+ (
158+ < span key = { invoice . uuid + '-payment-date' } >
159+ { invoice . status === 'paid' && invoice . createdAt != undefined ? localeTranslation . formatDate ( invoice . updatedAt ) : '-' }
189160 </ span >
190- ) ,
191- invoice . status === 'overdue' || invoice . status === 'pending' ?
192- (
193- < EmbeddedCheckoutButton
194- key = { invoice . uuid + '-pay' }
195- invoiceId = { invoice . uuid }
196- >
197- { translation . payNow }
198- </ EmbeddedCheckoutButton >
199- ) : (
200- invoice . status !== 'paid' ?
201- (
202- < Button
203- disabled = { true }
204- variant = "text-border"
205- className = { tw ( 'mr-2' ) } >
206- { translation [ invoice . status ] }
207- </ Button >
208- ) :
209- (
210- < Button >
211- PDF
212- </ Button >
213- )
214- )
215- ] }
216- />
217- </ div >
218- ) :
161+ ) ,
162+ invoice . status === 'overdue' || invoice . status === 'pending' ?
163+ (
164+ < EmbeddedCheckoutButton
165+ key = { invoice . uuid + '-pay' }
166+ invoiceId = { invoice . uuid }
167+ >
168+ { translation . payNow }
169+ </ EmbeddedCheckoutButton >
170+ ) : (
171+ invoice . status !== 'paid' ?
172+ (
173+ < Button
174+ disabled = { true }
175+ variant = "text-border"
176+ className = { tw ( 'mr-2' ) } >
177+ { translation [ invoice . status ] }
178+ </ Button >
179+ ) :
180+ (
181+ < Button >
182+ PDF
183+ </ Button >
184+ )
185+ )
186+ ] }
187+ />
188+ </ div >
189+ ) :
219190 (
220191 < div className = { tw ( 'flex flex-row justify-center w-full text-gray-400' ) } > { translation . noInvoices } </ div >
221192 ) }
0 commit comments