@spaceinvoices/react-ui 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +340 -0
- package/cli/dist/index.js +922 -0
- package/package.json +87 -0
- package/registry.json +600 -0
- package/spaceinvoices.schema.json +47 -0
- package/src/app.tsx +25 -0
- package/src/common/autocomplete.tsx +135 -0
- package/src/components/activities/activity-timeline.tsx +160 -0
- package/src/components/activities/index.ts +1 -0
- package/src/components/activities/locales/de.ts +30 -0
- package/src/components/activities/locales/sl.ts +30 -0
- package/src/components/advance-invoices/advance-invoices.hooks.ts +75 -0
- package/src/components/advance-invoices/create/create-advance-invoice-form.tsx +702 -0
- package/src/components/advance-invoices/create/locales/de.ts +29 -0
- package/src/components/advance-invoices/create/locales/sl.ts +25 -0
- package/src/components/advance-invoices/create/prepare-advance-invoice-submission.ts +74 -0
- package/src/components/advance-invoices/index.ts +5 -0
- package/src/components/advance-invoices/list/index.ts +3 -0
- package/src/components/advance-invoices/list/list-row-actions.tsx +119 -0
- package/src/components/advance-invoices/list/list-table.tsx +178 -0
- package/src/components/advance-invoices/list/locales/de.ts +32 -0
- package/src/components/advance-invoices/list/locales/sl.ts +32 -0
- package/src/components/advance-invoices/list/use-advance-invoice-download.ts +63 -0
- package/src/components/button-loader.tsx +11 -0
- package/src/components/combobox.tsx +96 -0
- package/src/components/company-registry/company-registry-autocomplete.tsx +151 -0
- package/src/components/company-registry/company-registry.hooks.ts +67 -0
- package/src/components/company-registry/index.ts +7 -0
- package/src/components/credit-notes/create/create-credit-note-form.tsx +332 -0
- package/src/components/credit-notes/create/index.ts +1 -0
- package/src/components/credit-notes/create/locales/de.ts +69 -0
- package/src/components/credit-notes/create/locales/sl.ts +67 -0
- package/src/components/credit-notes/credit-notes.hooks.ts +22 -0
- package/src/components/credit-notes/index.ts +10 -0
- package/src/components/credit-notes/list/index.ts +3 -0
- package/src/components/credit-notes/list/list-row-actions.tsx +116 -0
- package/src/components/credit-notes/list/list-table.tsx +183 -0
- package/src/components/credit-notes/list/locales/de.ts +33 -0
- package/src/components/credit-notes/list/locales/sl.ts +33 -0
- package/src/components/credit-notes/list/use-credit-note-download.ts +65 -0
- package/src/components/customers/create-customer-form/create-customer-form.tsx +134 -0
- package/src/components/customers/create-customer-form/locales/de.ts +20 -0
- package/src/components/customers/create-customer-form/locales/sl.ts +20 -0
- package/src/components/customers/customer-autocomplete.tsx +173 -0
- package/src/components/customers/customer-combobox.tsx +130 -0
- package/src/components/customers/customer-list-table/customer-list-row-actions.tsx +48 -0
- package/src/components/customers/customer-list-table/customer-list-table.tsx +124 -0
- package/src/components/customers/customer-list-table/index.ts +2 -0
- package/src/components/customers/customer-list-table/locales/de.ts +16 -0
- package/src/components/customers/customer-list-table/locales/sl.ts +16 -0
- package/src/components/customers/customers.hooks.test.ts +348 -0
- package/src/components/customers/customers.hooks.ts +57 -0
- package/src/components/customers/index.ts +5 -0
- package/src/components/dashboard/chart-empty-state.tsx +29 -0
- package/src/components/dashboard/collection-rate-card/collection-rate-card.tsx +80 -0
- package/src/components/dashboard/collection-rate-card/index.ts +4 -0
- package/src/components/dashboard/collection-rate-card/locales/sl.ts +3 -0
- package/src/components/dashboard/collection-rate-card/use-collection-rate.ts +74 -0
- package/src/components/dashboard/index.ts +54 -0
- package/src/components/dashboard/invoice-status-chart/index.ts +4 -0
- package/src/components/dashboard/invoice-status-chart/invoice-status-chart.tsx +130 -0
- package/src/components/dashboard/invoice-status-chart/locales/sl.ts +9 -0
- package/src/components/dashboard/invoice-status-chart/use-invoice-status.ts +105 -0
- package/src/components/dashboard/loading-card.tsx +19 -0
- package/src/components/dashboard/payment-methods-chart/index.ts +4 -0
- package/src/components/dashboard/payment-methods-chart/locales/sl.ts +12 -0
- package/src/components/dashboard/payment-methods-chart/payment-methods-chart.tsx +152 -0
- package/src/components/dashboard/payment-methods-chart/use-payment-methods.ts +50 -0
- package/src/components/dashboard/payment-trend-chart/index.ts +4 -0
- package/src/components/dashboard/payment-trend-chart/locales/sl.ts +5 -0
- package/src/components/dashboard/payment-trend-chart/payment-trend-chart.tsx +137 -0
- package/src/components/dashboard/payment-trend-chart/use-payment-trend.ts +92 -0
- package/src/components/dashboard/revenue-card.tsx +49 -0
- package/src/components/dashboard/revenue-trend-chart/index.ts +4 -0
- package/src/components/dashboard/revenue-trend-chart/locales/sl.ts +5 -0
- package/src/components/dashboard/revenue-trend-chart/revenue-trend-chart.tsx +137 -0
- package/src/components/dashboard/revenue-trend-chart/use-revenue-trend.ts +93 -0
- package/src/components/dashboard/shared/index.ts +5 -0
- package/src/components/dashboard/shared/use-revenue-data.ts +160 -0
- package/src/components/dashboard/shared/use-stats-counts.ts +89 -0
- package/src/components/dashboard/shared/use-stats-query.ts +38 -0
- package/src/components/dashboard/stat-card.tsx +41 -0
- package/src/components/dashboard/tax-collected-card/index.ts +2 -0
- package/src/components/dashboard/tax-collected-card/tax-collected-card.tsx +77 -0
- package/src/components/dashboard/tax-collected-card/use-tax-collected.ts +145 -0
- package/src/components/dashboard/top-customers-chart/index.ts +4 -0
- package/src/components/dashboard/top-customers-chart/locales/sl.ts +5 -0
- package/src/components/dashboard/top-customers-chart/top-customers-chart.tsx +130 -0
- package/src/components/dashboard/top-customers-chart/use-top-customers.ts +72 -0
- package/src/components/documents/create/document-add-item-form.tsx +379 -0
- package/src/components/documents/create/document-add-item-tax-rate-field.tsx +120 -0
- package/src/components/documents/create/document-details-section.tsx +597 -0
- package/src/components/documents/create/document-items-section.tsx +133 -0
- package/src/components/documents/create/document-recipient-section.tsx +101 -0
- package/src/components/documents/create/form-types.ts +36 -0
- package/src/components/documents/create/index.ts +9 -0
- package/src/components/documents/create/live-preview.tsx +235 -0
- package/src/components/documents/create/mark-as-paid-section.tsx +82 -0
- package/src/components/documents/create/prepare-document-submission.test.ts +132 -0
- package/src/components/documents/create/prepare-document-submission.ts +187 -0
- package/src/components/documents/create/prepare-preview-data.test.ts +155 -0
- package/src/components/documents/create/prepare-preview-data.ts +16 -0
- package/src/components/documents/create/smart-code-insert-button.tsx +139 -0
- package/src/components/documents/create/use-document-customer-form.ts +161 -0
- package/src/components/documents/document-preview.tsx +13 -0
- package/src/components/documents/documents.hooks.ts +146 -0
- package/src/components/documents/index.ts +23 -0
- package/src/components/documents/shared/document-preview-display.tsx +172 -0
- package/src/components/documents/shared/index.ts +3 -0
- package/src/components/documents/shared/scaled-document-preview.tsx +70 -0
- package/src/components/documents/shared/use-a4-scaling.ts +62 -0
- package/src/components/documents/types.ts +61 -0
- package/src/components/documents/view/document-actions-bar.tsx +328 -0
- package/src/components/documents/view/document-details-card.tsx +179 -0
- package/src/components/documents/view/document-payments-list.tsx +256 -0
- package/src/components/documents/view/index.ts +4 -0
- package/src/components/documents/view/locales/de.ts +85 -0
- package/src/components/documents/view/locales/sl.ts +84 -0
- package/src/components/documents/view/use-document-download.ts +125 -0
- package/src/components/entities/create-entity-form.tsx +105 -0
- package/src/components/entities/entities.hooks.ts +50 -0
- package/src/components/entities/entity-settings-form/email-template-variables-info.tsx +103 -0
- package/src/components/entities/entity-settings-form/entity-settings-form.tsx +1326 -0
- package/src/components/entities/entity-settings-form/image-upload-with-crop.tsx +222 -0
- package/src/components/entities/entity-settings-form/index.ts +2 -0
- package/src/components/entities/entity-settings-form/input-with-preview.tsx +190 -0
- package/src/components/entities/entity-settings-form/locales/de.ts +192 -0
- package/src/components/entities/entity-settings-form/locales/sl.ts +188 -0
- package/src/components/entities/furs-settings-form/furs-settings-form.tsx +410 -0
- package/src/components/entities/furs-settings-form/furs-settings.hooks.ts +320 -0
- package/src/components/entities/furs-settings-form/index.ts +3 -0
- package/src/components/entities/furs-settings-form/locales/de.ts +233 -0
- package/src/components/entities/furs-settings-form/locales/en.ts +194 -0
- package/src/components/entities/furs-settings-form/locales/sl.ts +196 -0
- package/src/components/entities/furs-settings-form/sections/certificate-settings-section.tsx +242 -0
- package/src/components/entities/furs-settings-form/sections/enable-fiscalization-section.tsx +139 -0
- package/src/components/entities/furs-settings-form/sections/general-settings-section.tsx +252 -0
- package/src/components/entities/furs-settings-form/sections/premises-management-section.tsx +370 -0
- package/src/components/entities/furs-settings-form/sections/register-premise-dialog.tsx +420 -0
- package/src/components/entities/keys.ts +2 -0
- package/src/components/entities/settings/branding-settings-form.tsx +274 -0
- package/src/components/entities/settings/company-settings-form.tsx +256 -0
- package/src/components/entities/settings/defaults-settings-form.tsx +501 -0
- package/src/components/entities/settings/email-settings-form.tsx +288 -0
- package/src/components/entities/settings/eslog-settings-form.tsx +113 -0
- package/src/components/entities/settings/index.ts +8 -0
- package/src/components/entities/settings/number-format-settings-form.tsx +244 -0
- package/src/components/entities/settings/pdf-template-selector/demo-invoice-data.ts +164 -0
- package/src/components/entities/settings/pdf-template-selector/index.ts +2 -0
- package/src/components/entities/settings/pdf-template-selector/locales/de.ts +18 -0
- package/src/components/entities/settings/pdf-template-selector/locales/sl.ts +18 -0
- package/src/components/entities/settings/pdf-template-selector/pdf-template-cards.tsx +49 -0
- package/src/components/entities/settings/settings-footer.tsx +16 -0
- package/src/components/entities/settings/tax-rules-settings-form.tsx +346 -0
- package/src/components/estimates/create/create-estimate-form.tsx +384 -0
- package/src/components/estimates/create/locales/de.ts +64 -0
- package/src/components/estimates/create/locales/sl.ts +63 -0
- package/src/components/estimates/create/prepare-estimate-submission.ts +39 -0
- package/src/components/estimates/create/use-estimate-customer-form.ts +5 -0
- package/src/components/estimates/estimates.hooks.ts +15 -0
- package/src/components/estimates/index.ts +6 -0
- package/src/components/estimates/list/index.ts +3 -0
- package/src/components/estimates/list/list-row-actions.tsx +103 -0
- package/src/components/estimates/list/list-table.tsx +171 -0
- package/src/components/estimates/list/locales/de.ts +26 -0
- package/src/components/estimates/list/locales/sl.ts +26 -0
- package/src/components/estimates/list/use-estimate-download.ts +63 -0
- package/src/components/export/document-export-form.tsx +288 -0
- package/src/components/export/index.ts +2 -0
- package/src/components/form/form-input.tsx +89 -0
- package/src/components/form/index.ts +1 -0
- package/src/components/invoices/create/create-invoice-form.tsx +852 -0
- package/src/components/invoices/create/eslog-validation.test.ts +242 -0
- package/src/components/invoices/create/eslog-validation.ts +208 -0
- package/src/components/invoices/create/locales/de.ts +118 -0
- package/src/components/invoices/create/locales/sl.ts +114 -0
- package/src/components/invoices/create/prepare-invoice-submission.test.ts +777 -0
- package/src/components/invoices/create/prepare-invoice-submission.ts +79 -0
- package/src/components/invoices/create/use-invoice-customer-form.ts +5 -0
- package/src/components/invoices/index.ts +9 -0
- package/src/components/invoices/invoices-furs.hooks.ts +28 -0
- package/src/components/invoices/invoices.hooks.ts +110 -0
- package/src/components/invoices/list/index.ts +3 -0
- package/src/components/invoices/list/list-row-actions.tsx +132 -0
- package/src/components/invoices/list/list-table.tsx +165 -0
- package/src/components/invoices/list/locales/de.ts +33 -0
- package/src/components/invoices/list/locales/sl.ts +33 -0
- package/src/components/invoices/list/use-invoice-download.ts +62 -0
- package/src/components/invoices/send-email-dialog/index.ts +1 -0
- package/src/components/invoices/send-email-dialog/locales/de.ts +18 -0
- package/src/components/invoices/send-email-dialog/locales/sl.ts +17 -0
- package/src/components/invoices/send-email-dialog/send-email-dialog.tsx +289 -0
- package/src/components/invoices/send-email-dialog.tsx +2 -0
- package/src/components/invoices/shared/index.ts +2 -0
- package/src/components/invoices/shared/scaled-document-preview.tsx +32 -0
- package/src/components/invoices/shared/use-a4-scaling.tsx +39 -0
- package/src/components/invoices/view/eslog-info-display.tsx +160 -0
- package/src/components/invoices/view/furs-info-display.tsx +213 -0
- package/src/components/items/create-item-form/create-item-form.tsx +155 -0
- package/src/components/items/create-item-form/locales/de.ts +14 -0
- package/src/components/items/create-item-form/locales/en.ts +9 -0
- package/src/components/items/create-item-form/locales/sl.ts +14 -0
- package/src/components/items/item-combobox.tsx +147 -0
- package/src/components/items/item-list-table/item-list-header.tsx +33 -0
- package/src/components/items/item-list-table/item-list-row-actions.tsx +48 -0
- package/src/components/items/item-list-table/item-list-row.tsx +32 -0
- package/src/components/items/item-list-table/item-list-table.tsx +76 -0
- package/src/components/items/item-list-table/locales/de.ts +10 -0
- package/src/components/items/item-list-table/locales/en.ts +10 -0
- package/src/components/items/item-list-table/locales/sl.ts +10 -0
- package/src/components/items/items.hooks.ts +63 -0
- package/src/components/loading-spinner.tsx +24 -0
- package/src/components/payments/create-payment-form/create-payment-form.tsx +222 -0
- package/src/components/payments/create-payment-form/locales/de.ts +20 -0
- package/src/components/payments/create-payment-form/locales/sl.ts +20 -0
- package/src/components/payments/edit-payment-form/edit-payment-form.tsx +230 -0
- package/src/components/payments/edit-payment-form/index.ts +1 -0
- package/src/components/payments/edit-payment-form/locales/de.ts +20 -0
- package/src/components/payments/edit-payment-form/locales/sl.ts +20 -0
- package/src/components/payments/index.ts +4 -0
- package/src/components/payments/list/index.ts +2 -0
- package/src/components/payments/list/list-row-actions.tsx +98 -0
- package/src/components/payments/list/list-table.tsx +186 -0
- package/src/components/payments/list/locales/de.ts +19 -0
- package/src/components/payments/list/locales/sl.ts +19 -0
- package/src/components/payments/payments.hooks.ts +15 -0
- package/src/components/request-logs/index.ts +3 -0
- package/src/components/request-logs/request-log-detail.tsx +242 -0
- package/src/components/request-logs/request-log-list-table.tsx +266 -0
- package/src/components/request-logs/request-logs-page.tsx +10 -0
- package/src/components/table/README.md +410 -0
- package/src/components/table/data-table.tsx +251 -0
- package/src/components/table/date-cell.tsx +35 -0
- package/src/components/table/filter-bar.tsx +114 -0
- package/src/components/table/filter-panel.tsx +407 -0
- package/src/components/table/hooks/use-table-fetch.ts +17 -0
- package/src/components/table/hooks/use-table-query.ts +36 -0
- package/src/components/table/hooks/use-table-state.ts +293 -0
- package/src/components/table/index.ts +35 -0
- package/src/components/table/search-input.tsx +85 -0
- package/src/components/table/sortable-header.tsx +56 -0
- package/src/components/table/table-empty-state.tsx +40 -0
- package/src/components/table/table-no-results.tsx +41 -0
- package/src/components/table/table-pagination.tsx +42 -0
- package/src/components/table/table-skeleton.tsx +54 -0
- package/src/components/table/types.ts +136 -0
- package/src/components/tax-reports/index.ts +1 -0
- package/src/components/tax-reports/kir-export-form.tsx +172 -0
- package/src/components/taxes/create-tax-form/create-tax-form.tsx +112 -0
- package/src/components/taxes/create-tax-form/locales/de.ts +8 -0
- package/src/components/taxes/create-tax-form/locales/en.ts +7 -0
- package/src/components/taxes/create-tax-form/locales/sl.ts +8 -0
- package/src/components/taxes/tax-list-table/locales/de.ts +11 -0
- package/src/components/taxes/tax-list-table/locales/en.ts +10 -0
- package/src/components/taxes/tax-list-table/locales/sl.ts +11 -0
- package/src/components/taxes/tax-list-table/tax-list-header.tsx +29 -0
- package/src/components/taxes/tax-list-table/tax-list-row-actions.tsx +43 -0
- package/src/components/taxes/tax-list-table/tax-list-row.tsx +46 -0
- package/src/components/taxes/tax-list-table/tax-list-table.tsx +59 -0
- package/src/components/taxes/taxes.hooks.ts +35 -0
- package/src/components/ui/alert-dialog.tsx +61 -0
- package/src/components/ui/alert.tsx +72 -0
- package/src/components/ui/badge.tsx +48 -0
- package/src/components/ui/breadcrumb.tsx +132 -0
- package/src/components/ui/button.tsx +61 -0
- package/src/components/ui/calendar.tsx +213 -0
- package/src/components/ui/card.tsx +94 -0
- package/src/components/ui/chart.tsx +380 -0
- package/src/components/ui/checkbox.tsx +27 -0
- package/src/components/ui/collapsible.tsx +56 -0
- package/src/components/ui/command.tsx +187 -0
- package/src/components/ui/dialog.tsx +187 -0
- package/src/components/ui/drawer.tsx +123 -0
- package/src/components/ui/dropdown-menu.tsx +291 -0
- package/src/components/ui/form.tsx +166 -0
- package/src/components/ui/input-group.tsx +149 -0
- package/src/components/ui/input.tsx +20 -0
- package/src/components/ui/label.tsx +18 -0
- package/src/components/ui/loading-spinner.tsx +16 -0
- package/src/components/ui/popover.tsx +108 -0
- package/src/components/ui/radio-group.tsx +37 -0
- package/src/components/ui/select.tsx +200 -0
- package/src/components/ui/separator.tsx +23 -0
- package/src/components/ui/sheet.tsx +145 -0
- package/src/components/ui/sidebar.tsx +771 -0
- package/src/components/ui/skeleton.tsx +13 -0
- package/src/components/ui/sonner.tsx +60 -0
- package/src/components/ui/spinner.tsx +10 -0
- package/src/components/ui/sticky-form-footer.tsx +55 -0
- package/src/components/ui/switch.tsx +30 -0
- package/src/components/ui/table.tsx +101 -0
- package/src/components/ui/tabs.tsx +80 -0
- package/src/components/ui/textarea.tsx +18 -0
- package/src/components/ui/tooltip.tsx +89 -0
- package/src/components/wl-subscription/index.ts +2 -0
- package/src/components/wl-subscription/locked-feature.tsx +173 -0
- package/src/components/wl-subscription/upgrade-modal.tsx +209 -0
- package/src/frontend.tsx +28 -0
- package/src/generate-schemas.ts +265 -0
- package/src/generated/schemas/advanceinvoice.ts +177 -0
- package/src/generated/schemas/creditnote.ts +187 -0
- package/src/generated/schemas/customer.ts +29 -0
- package/src/generated/schemas/entity.ts +252 -0
- package/src/generated/schemas/estimate.ts +159 -0
- package/src/generated/schemas/furssettings.ts +25 -0
- package/src/generated/schemas/index.ts +24 -0
- package/src/generated/schemas/invoice.ts +167 -0
- package/src/generated/schemas/item.ts +38 -0
- package/src/generated/schemas/payment.ts +44 -0
- package/src/generated/schemas/previewadvanceinvoice_body.ts +354 -0
- package/src/generated/schemas/previewestimate_body.ts +309 -0
- package/src/generated/schemas/registerfursmovablepremise_body.ts +22 -0
- package/src/generated/schemas/registerfursrealestatepremise_body.ts +32 -0
- package/src/generated/schemas/renderdocument_body.ts +594 -0
- package/src/generated/schemas/sendemail_body.ts +26 -0
- package/src/generated/schemas/startpdfexport_body.ts +20 -0
- package/src/generated/schemas/tax.ts +48 -0
- package/src/generated/schemas/uploadfile_body.ts +23 -0
- package/src/generated/schemas/uploadfurscertificate_body.ts +20 -0
- package/src/generated/schemas/userfurssettings.ts +19 -0
- package/src/hooks/create-resource-hooks.test.ts +483 -0
- package/src/hooks/create-resource-hooks.ts +300 -0
- package/src/hooks/use-debounce.ts +12 -0
- package/src/hooks/use-duplicate-document.ts +185 -0
- package/src/hooks/use-media-query.tsx +19 -0
- package/src/hooks/use-mobile.ts +39 -0
- package/src/hooks/use-next-document-number.ts +57 -0
- package/src/hooks/use-resource-mutation.ts +118 -0
- package/src/hooks/use-vies-check.ts +130 -0
- package/src/index.css +11 -0
- package/src/index.html +13 -0
- package/src/index.tsx +12 -0
- package/src/lib/auth.ts +4 -0
- package/src/lib/browser-cookies.ts +70 -0
- package/src/lib/constants.ts +287 -0
- package/src/lib/cookies.ts +36 -0
- package/src/lib/schemas/advance-invoice.ts +43 -0
- package/src/lib/schemas/credit-note.ts +32 -0
- package/src/lib/schemas/estimate.ts +31 -0
- package/src/lib/schemas/index.ts +18 -0
- package/src/lib/schemas/invoice.ts +43 -0
- package/src/lib/schemas/shared.ts +79 -0
- package/src/lib/translation.ts +38 -0
- package/src/lib/utils.ts +6 -0
- package/src/providers/entities-context.tsx +41 -0
- package/src/providers/entities-provider.tsx +201 -0
- package/src/providers/form-footer-context.tsx +72 -0
- package/src/providers/sdk-provider.tsx +164 -0
- package/src/providers/white-label-provider.tsx +91 -0
- package/src/providers/wl-subscription-provider.tsx +277 -0
- package/src/utils/string-helpers.ts +111 -0
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, test } from "bun:test";
|
|
2
|
+
import { renderHook, waitFor } from "@testing-library/react";
|
|
3
|
+
import { createMockSDK, createTestSetup, resetSDKMocks } from "../../../test/test-utils";
|
|
4
|
+
import { CUSTOMERS_CACHE_KEY, useCustomerSearch, useRecentCustomers } from "./customers.hooks";
|
|
5
|
+
|
|
6
|
+
describe("customers.hooks", () => {
|
|
7
|
+
let sdk: ReturnType<typeof createMockSDK>;
|
|
8
|
+
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
sdk = createMockSDK();
|
|
11
|
+
resetSDKMocks(sdk);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
describe("useCustomerSearch", () => {
|
|
15
|
+
test("searches customers with search term", async () => {
|
|
16
|
+
const { wrapper } = createTestSetup({ sdk });
|
|
17
|
+
const entityId = "entity-123";
|
|
18
|
+
const searchTerm = "John";
|
|
19
|
+
|
|
20
|
+
sdk.customers.list.mockResolvedValueOnce({
|
|
21
|
+
data: [
|
|
22
|
+
{ id: "customer-1", name: "John Doe", email: "john@example.com" },
|
|
23
|
+
{ id: "customer-2", name: "John Smith", email: "johnsmith@example.com" },
|
|
24
|
+
],
|
|
25
|
+
pagination: { total: 2 },
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const { result } = renderHook(() => useCustomerSearch(entityId, searchTerm), { wrapper });
|
|
29
|
+
|
|
30
|
+
await waitFor(() => {
|
|
31
|
+
expect(result.current.isSuccess).toBe(true);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
expect(sdk.customers.list).toHaveBeenCalledWith({
|
|
35
|
+
entity_id: entityId,
|
|
36
|
+
search: searchTerm,
|
|
37
|
+
limit: 10,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
expect(result.current.data?.data).toHaveLength(2);
|
|
41
|
+
expect(result.current.data?.data[0].name).toBe("John Doe");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("does not call API when search term is empty", async () => {
|
|
45
|
+
const { wrapper } = createTestSetup({ sdk });
|
|
46
|
+
const entityId = "entity-123";
|
|
47
|
+
const searchTerm = "";
|
|
48
|
+
|
|
49
|
+
renderHook(() => useCustomerSearch(entityId, searchTerm), { wrapper });
|
|
50
|
+
|
|
51
|
+
// Wait a bit to ensure no API call is made
|
|
52
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
53
|
+
|
|
54
|
+
// Should not call API when search is empty
|
|
55
|
+
expect(sdk.customers.list).not.toHaveBeenCalled();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("is disabled when entityId is missing", () => {
|
|
59
|
+
const { wrapper } = createTestSetup({ sdk });
|
|
60
|
+
const searchTerm = "John";
|
|
61
|
+
|
|
62
|
+
const { result } = renderHook(() => useCustomerSearch("", searchTerm), { wrapper });
|
|
63
|
+
|
|
64
|
+
// Should not make API call
|
|
65
|
+
expect(sdk.customers.list).not.toHaveBeenCalled();
|
|
66
|
+
|
|
67
|
+
// Query should be disabled
|
|
68
|
+
expect(result.current.isFetching).toBe(false);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("is disabled when search term is empty", () => {
|
|
72
|
+
const { wrapper } = createTestSetup({ sdk });
|
|
73
|
+
const entityId = "entity-123";
|
|
74
|
+
|
|
75
|
+
const { result } = renderHook(() => useCustomerSearch(entityId, ""), { wrapper });
|
|
76
|
+
|
|
77
|
+
// Query should be disabled (but returns empty data immediately)
|
|
78
|
+
expect(result.current.isFetching).toBe(false);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test("limits results to 10 customers", async () => {
|
|
82
|
+
const { wrapper } = createTestSetup({ sdk });
|
|
83
|
+
const entityId = "entity-123";
|
|
84
|
+
const searchTerm = "test";
|
|
85
|
+
|
|
86
|
+
sdk.customers.list.mockResolvedValueOnce({
|
|
87
|
+
data: Array.from({ length: 10 }, (_, i) => ({
|
|
88
|
+
id: `customer-${i}`,
|
|
89
|
+
name: `Test Customer ${i}`,
|
|
90
|
+
email: `test${i}@example.com`,
|
|
91
|
+
})),
|
|
92
|
+
pagination: { total: 10 },
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
const { result } = renderHook(() => useCustomerSearch(entityId, searchTerm), { wrapper });
|
|
96
|
+
|
|
97
|
+
await waitFor(() => {
|
|
98
|
+
expect(result.current.isSuccess).toBe(true);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
expect(sdk.customers.list).toHaveBeenCalledWith({
|
|
102
|
+
entity_id: entityId,
|
|
103
|
+
search: searchTerm,
|
|
104
|
+
limit: 10,
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("creates unique cache keys for different searches", async () => {
|
|
109
|
+
const { wrapper, queryClient } = createTestSetup({ sdk });
|
|
110
|
+
const entityId = "entity-123";
|
|
111
|
+
|
|
112
|
+
sdk.customers.list.mockResolvedValue({
|
|
113
|
+
data: [{ id: "customer-1", name: "Test", email: "test@example.com" }],
|
|
114
|
+
pagination: { total: 1 },
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
// First search
|
|
118
|
+
const { result: result1 } = renderHook(() => useCustomerSearch(entityId, "john"), { wrapper });
|
|
119
|
+
|
|
120
|
+
await waitFor(() => {
|
|
121
|
+
expect(result1.current.isSuccess).toBe(true);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
// Second search with different term
|
|
125
|
+
const { result: result2 } = renderHook(() => useCustomerSearch(entityId, "jane"), { wrapper });
|
|
126
|
+
|
|
127
|
+
await waitFor(() => {
|
|
128
|
+
expect(result2.current.isSuccess).toBe(true);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
// Should have made two separate API calls
|
|
132
|
+
expect(sdk.customers.list).toHaveBeenCalledTimes(2);
|
|
133
|
+
|
|
134
|
+
// Check cache has both queries
|
|
135
|
+
const cache = queryClient.getQueryCache().getAll();
|
|
136
|
+
const customerSearchQueries = cache.filter(
|
|
137
|
+
(query) => query.queryKey[0] === CUSTOMERS_CACHE_KEY && query.queryKey[1] === "search",
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
expect(customerSearchQueries.length).toBeGreaterThanOrEqual(2);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test("handles API errors gracefully", async () => {
|
|
144
|
+
const { wrapper } = createTestSetup({ sdk });
|
|
145
|
+
const entityId = "entity-123";
|
|
146
|
+
const searchTerm = "error";
|
|
147
|
+
|
|
148
|
+
sdk.customers.list.mockRejectedValueOnce(new Error("API Error"));
|
|
149
|
+
|
|
150
|
+
const { result } = renderHook(() => useCustomerSearch(entityId, searchTerm), { wrapper });
|
|
151
|
+
|
|
152
|
+
await waitFor(() => {
|
|
153
|
+
expect(result.current.isError).toBe(true);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
expect(result.current.error).toBeDefined();
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
describe("useRecentCustomers", () => {
|
|
161
|
+
test("fetches recent customers", async () => {
|
|
162
|
+
const { wrapper } = createTestSetup({ sdk });
|
|
163
|
+
const entityId = "entity-123";
|
|
164
|
+
|
|
165
|
+
const mockCustomers = [
|
|
166
|
+
{ id: "customer-1", name: "Recent Customer 1", email: "recent1@example.com", created_at: "2024-01-15" },
|
|
167
|
+
{ id: "customer-2", name: "Recent Customer 2", email: "recent2@example.com", created_at: "2024-01-14" },
|
|
168
|
+
];
|
|
169
|
+
|
|
170
|
+
sdk.customers.list.mockResolvedValueOnce({
|
|
171
|
+
data: mockCustomers,
|
|
172
|
+
pagination: { total: 2 },
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
const { result } = renderHook(() => useRecentCustomers(entityId), { wrapper });
|
|
176
|
+
|
|
177
|
+
await waitFor(() => {
|
|
178
|
+
expect(result.current.isSuccess).toBe(true);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
expect(sdk.customers.list).toHaveBeenCalledWith({
|
|
182
|
+
entity_id: entityId,
|
|
183
|
+
limit: 5,
|
|
184
|
+
order_by: "-created_at",
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
expect(result.current.data?.data).toHaveLength(2);
|
|
188
|
+
expect(result.current.data?.data[0].name).toBe("Recent Customer 1");
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
test("limits results to 5 customers", async () => {
|
|
192
|
+
const { wrapper } = createTestSetup({ sdk });
|
|
193
|
+
const entityId = "entity-123";
|
|
194
|
+
|
|
195
|
+
sdk.customers.list.mockResolvedValueOnce({
|
|
196
|
+
data: Array.from({ length: 5 }, (_, i) => ({
|
|
197
|
+
id: `customer-${i}`,
|
|
198
|
+
name: `Customer ${i}`,
|
|
199
|
+
email: `customer${i}@example.com`,
|
|
200
|
+
created_at: new Date(Date.now() - i * 1000).toISOString(),
|
|
201
|
+
})),
|
|
202
|
+
pagination: { total: 5 },
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
const { result } = renderHook(() => useRecentCustomers(entityId), { wrapper });
|
|
206
|
+
|
|
207
|
+
await waitFor(() => {
|
|
208
|
+
expect(result.current.isSuccess).toBe(true);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
expect(sdk.customers.list).toHaveBeenCalledWith({
|
|
212
|
+
entity_id: entityId,
|
|
213
|
+
limit: 5,
|
|
214
|
+
order_by: "-created_at",
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test("is disabled when entityId is missing", () => {
|
|
219
|
+
const { wrapper } = createTestSetup({ sdk });
|
|
220
|
+
|
|
221
|
+
const { result } = renderHook(() => useRecentCustomers(""), { wrapper });
|
|
222
|
+
|
|
223
|
+
// Should not make API call
|
|
224
|
+
expect(sdk.customers.list).not.toHaveBeenCalled();
|
|
225
|
+
|
|
226
|
+
// Query should be disabled
|
|
227
|
+
expect(result.current.isFetching).toBe(false);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
test("sorts customers by created_at descending", async () => {
|
|
231
|
+
const { wrapper } = createTestSetup({ sdk });
|
|
232
|
+
const entityId = "entity-123";
|
|
233
|
+
|
|
234
|
+
sdk.customers.list.mockResolvedValueOnce({
|
|
235
|
+
data: [],
|
|
236
|
+
pagination: { total: 0 },
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
renderHook(() => useRecentCustomers(entityId), { wrapper });
|
|
240
|
+
|
|
241
|
+
await waitFor(() => {
|
|
242
|
+
expect(sdk.customers.list).toHaveBeenCalled();
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
// Verify order_by parameter
|
|
246
|
+
expect(sdk.customers.list).toHaveBeenCalledWith(
|
|
247
|
+
expect.objectContaining({
|
|
248
|
+
order_by: "-created_at",
|
|
249
|
+
}),
|
|
250
|
+
);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
test("caches results for 5 minutes", async () => {
|
|
254
|
+
const { wrapper } = createTestSetup({ sdk });
|
|
255
|
+
const entityId = "entity-123";
|
|
256
|
+
|
|
257
|
+
sdk.customers.list.mockResolvedValue({
|
|
258
|
+
data: [{ id: "customer-1", name: "Test", email: "test@example.com", created_at: "" }],
|
|
259
|
+
pagination: { total: 1 },
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
const { result, rerender } = renderHook(() => useRecentCustomers(entityId), { wrapper });
|
|
263
|
+
|
|
264
|
+
await waitFor(() => {
|
|
265
|
+
expect(result.current.isSuccess).toBe(true);
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
expect(sdk.customers.list).toHaveBeenCalledTimes(1);
|
|
269
|
+
|
|
270
|
+
// Rerender to trigger another fetch (but should use cache)
|
|
271
|
+
rerender();
|
|
272
|
+
|
|
273
|
+
// Wait a bit to ensure it doesn't refetch
|
|
274
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
275
|
+
|
|
276
|
+
// Should still only have been called once (cached)
|
|
277
|
+
expect(sdk.customers.list).toHaveBeenCalledTimes(1);
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
test("creates unique cache keys for different entities", async () => {
|
|
281
|
+
const { wrapper, queryClient } = createTestSetup({ sdk });
|
|
282
|
+
|
|
283
|
+
sdk.customers.list.mockResolvedValue({
|
|
284
|
+
data: [{ id: "customer-1", name: "Test", email: "test@example.com", created_at: "" }],
|
|
285
|
+
pagination: { total: 1 },
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
// First entity
|
|
289
|
+
const { result: result1 } = renderHook(() => useRecentCustomers("entity-1"), { wrapper });
|
|
290
|
+
|
|
291
|
+
await waitFor(() => {
|
|
292
|
+
expect(result1.current.isSuccess).toBe(true);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
// Second entity
|
|
296
|
+
const { result: result2 } = renderHook(() => useRecentCustomers("entity-2"), { wrapper });
|
|
297
|
+
|
|
298
|
+
await waitFor(() => {
|
|
299
|
+
expect(result2.current.isSuccess).toBe(true);
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
// Should have made two separate API calls
|
|
303
|
+
expect(sdk.customers.list).toHaveBeenCalledTimes(2);
|
|
304
|
+
|
|
305
|
+
// Check cache has both queries
|
|
306
|
+
const cache = queryClient.getQueryCache().getAll();
|
|
307
|
+
const recentCustomersQueries = cache.filter(
|
|
308
|
+
(query) => query.queryKey[0] === CUSTOMERS_CACHE_KEY && query.queryKey[1] === "recent",
|
|
309
|
+
);
|
|
310
|
+
|
|
311
|
+
expect(recentCustomersQueries.length).toBeGreaterThanOrEqual(2);
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
test("handles API errors gracefully", async () => {
|
|
315
|
+
const { wrapper } = createTestSetup({ sdk });
|
|
316
|
+
const entityId = "entity-123";
|
|
317
|
+
|
|
318
|
+
sdk.customers.list.mockRejectedValueOnce(new Error("API Error"));
|
|
319
|
+
|
|
320
|
+
const { result } = renderHook(() => useRecentCustomers(entityId), { wrapper });
|
|
321
|
+
|
|
322
|
+
await waitFor(() => {
|
|
323
|
+
expect(result.current.isError).toBe(true);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
expect(result.current.error).toBeDefined();
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
test("handles empty results", async () => {
|
|
330
|
+
const { wrapper } = createTestSetup({ sdk });
|
|
331
|
+
const entityId = "entity-123";
|
|
332
|
+
|
|
333
|
+
sdk.customers.list.mockResolvedValueOnce({
|
|
334
|
+
data: [],
|
|
335
|
+
pagination: { total: 0 },
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
const { result } = renderHook(() => useRecentCustomers(entityId), { wrapper });
|
|
339
|
+
|
|
340
|
+
await waitFor(() => {
|
|
341
|
+
expect(result.current.isSuccess).toBe(true);
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
expect(result.current.data?.data).toEqual([]);
|
|
345
|
+
expect(result.current.data?.pagination.total).toBe(0);
|
|
346
|
+
});
|
|
347
|
+
});
|
|
348
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { CreateCustomerBody, Customer } from "@spaceinvoices/js-sdk";
|
|
2
|
+
|
|
3
|
+
import { useQuery } from "@tanstack/react-query";
|
|
4
|
+
|
|
5
|
+
import { createResourceHooks } from "@/ui/hooks/create-resource-hooks";
|
|
6
|
+
import { useSDK } from "@/ui/providers/sdk-provider";
|
|
7
|
+
|
|
8
|
+
// Define a constant for the customers cache key
|
|
9
|
+
export const CUSTOMERS_CACHE_KEY = "customers";
|
|
10
|
+
|
|
11
|
+
export const useCustomerSearch = (entityId: string, search: string) => {
|
|
12
|
+
const { sdk } = useSDK();
|
|
13
|
+
|
|
14
|
+
return useQuery({
|
|
15
|
+
// Don't include SDK in query key - it's guaranteed non-null by provider
|
|
16
|
+
// Including it breaks cache due to object reference changes
|
|
17
|
+
queryKey: [CUSTOMERS_CACHE_KEY, "search", entityId, search],
|
|
18
|
+
queryFn: async () => {
|
|
19
|
+
if (!search) return { data: [] };
|
|
20
|
+
|
|
21
|
+
const response = await sdk.customers.list({
|
|
22
|
+
entity_id: entityId,
|
|
23
|
+
search,
|
|
24
|
+
limit: 10,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
return response;
|
|
28
|
+
},
|
|
29
|
+
// SDK is always available, only check search and entityId
|
|
30
|
+
enabled: Boolean(entityId && search),
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export const useRecentCustomers = (entityId: string) => {
|
|
35
|
+
const { sdk } = useSDK();
|
|
36
|
+
|
|
37
|
+
return useQuery({
|
|
38
|
+
queryKey: [CUSTOMERS_CACHE_KEY, "recent", entityId],
|
|
39
|
+
queryFn: async () => {
|
|
40
|
+
const response = await sdk.customers.list({
|
|
41
|
+
entity_id: entityId,
|
|
42
|
+
limit: 5,
|
|
43
|
+
order_by: "-created_at", // Sort by most recently created
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
return response;
|
|
47
|
+
},
|
|
48
|
+
enabled: Boolean(entityId),
|
|
49
|
+
staleTime: 1000 * 60 * 5, // Cache for 5 minutes
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export const {
|
|
54
|
+
useCreateResource: useCreateCustomer,
|
|
55
|
+
useUpdateResource: useUpdateCustomer,
|
|
56
|
+
useDeleteResource: useDeleteCustomer,
|
|
57
|
+
} = createResourceHooks<Customer, CreateCustomerBody>("customers", CUSTOMERS_CACHE_KEY);
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { default as CreateCustomerForm } from "./create-customer-form/create-customer-form";
|
|
2
|
+
export { CustomerAutocomplete } from "./customer-autocomplete";
|
|
3
|
+
export { CustomerCombobox } from "./customer-combobox";
|
|
4
|
+
export * from "./customer-list-table";
|
|
5
|
+
export * from "./customers.hooks";
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import type { ReactNode } from "react";
|
|
4
|
+
|
|
5
|
+
type ChartEmptyStateProps = {
|
|
6
|
+
/** The chart to render (will be muted) */
|
|
7
|
+
children: ReactNode;
|
|
8
|
+
/** Label to show over the chart */
|
|
9
|
+
label?: string;
|
|
10
|
+
/** Height of the container */
|
|
11
|
+
className?: string;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Empty state wrapper that shows a muted chart with an overlay label
|
|
16
|
+
*/
|
|
17
|
+
export function ChartEmptyState({ children, label = "No data", className }: ChartEmptyStateProps) {
|
|
18
|
+
return (
|
|
19
|
+
<div className={`relative ${className || ""}`}>
|
|
20
|
+
{/* Muted chart */}
|
|
21
|
+
<div className="pointer-events-none opacity-30 grayscale">{children}</div>
|
|
22
|
+
|
|
23
|
+
{/* Overlay label */}
|
|
24
|
+
<div className="absolute inset-0 flex items-center justify-center">
|
|
25
|
+
<div className="rounded-md bg-muted/80 px-3 py-1.5 text-muted-foreground text-sm">{label}</div>
|
|
26
|
+
</div>
|
|
27
|
+
</div>
|
|
28
|
+
);
|
|
29
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { Card, CardContent, CardHeader, CardTitle } from "@/ui/components/ui/card";
|
|
4
|
+
import { createTranslation } from "@/ui/lib/translation";
|
|
5
|
+
import { LoadingCard } from "../loading-card";
|
|
6
|
+
import sl from "./locales/sl";
|
|
7
|
+
import { useCollectionRateData } from "./use-collection-rate";
|
|
8
|
+
|
|
9
|
+
const translations = { sl } as const;
|
|
10
|
+
|
|
11
|
+
type BaseProps = {
|
|
12
|
+
locale?: string;
|
|
13
|
+
t?: (key: string) => string;
|
|
14
|
+
namespace?: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type DataProps = BaseProps & {
|
|
18
|
+
collectionRate: number;
|
|
19
|
+
totalCollected: number;
|
|
20
|
+
totalInvoiced: number;
|
|
21
|
+
currency: string;
|
|
22
|
+
entityId?: never;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
type TurnkeyProps = BaseProps & {
|
|
26
|
+
entityId: string;
|
|
27
|
+
collectionRate?: never;
|
|
28
|
+
totalCollected?: never;
|
|
29
|
+
totalInvoiced?: never;
|
|
30
|
+
currency?: never;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type CollectionRateCardProps = DataProps | TurnkeyProps;
|
|
34
|
+
|
|
35
|
+
export function CollectionRateCard(props: CollectionRateCardProps) {
|
|
36
|
+
const { locale, t: externalT, namespace } = props;
|
|
37
|
+
const t = createTranslation({ t: externalT, namespace, locale, translations });
|
|
38
|
+
|
|
39
|
+
// Turnkey mode - fetch own data
|
|
40
|
+
const hookResult = useCollectionRateData("entityId" in props ? props.entityId : undefined);
|
|
41
|
+
|
|
42
|
+
// Determine data source
|
|
43
|
+
const collectionRate = "entityId" in props ? hookResult.data.collectionRate : props.collectionRate;
|
|
44
|
+
const totalCollected = "entityId" in props ? hookResult.data.totalCollected : props.totalCollected;
|
|
45
|
+
const totalInvoiced = "entityId" in props ? hookResult.data.totalInvoiced : props.totalInvoiced;
|
|
46
|
+
const currency = "entityId" in props ? hookResult.data.currency : props.currency;
|
|
47
|
+
const isLoading = "entityId" in props ? hookResult.isLoading : false;
|
|
48
|
+
|
|
49
|
+
if (isLoading) {
|
|
50
|
+
return <LoadingCard />;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const formatCurrency = (value: number) =>
|
|
54
|
+
new Intl.NumberFormat(locale, {
|
|
55
|
+
style: "currency",
|
|
56
|
+
currency,
|
|
57
|
+
minimumFractionDigits: 0,
|
|
58
|
+
maximumFractionDigits: 0,
|
|
59
|
+
}).format(value);
|
|
60
|
+
|
|
61
|
+
const getVariantColor = (rate: number) => {
|
|
62
|
+
if (rate >= 80) return "text-green-600 dark:text-green-400";
|
|
63
|
+
if (rate >= 50) return "text-yellow-600 dark:text-yellow-400";
|
|
64
|
+
return "text-red-600 dark:text-red-400";
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
return (
|
|
68
|
+
<Card>
|
|
69
|
+
<CardHeader className="pb-2">
|
|
70
|
+
<CardTitle className="font-medium text-muted-foreground text-sm">{t("Collection Rate")}</CardTitle>
|
|
71
|
+
</CardHeader>
|
|
72
|
+
<CardContent>
|
|
73
|
+
<div className={`font-bold text-2xl ${getVariantColor(collectionRate)}`}>{collectionRate.toFixed(1)}%</div>
|
|
74
|
+
<p className="mt-1 text-muted-foreground text-xs">
|
|
75
|
+
{formatCurrency(totalCollected)} / {formatCurrency(totalInvoiced)}
|
|
76
|
+
</p>
|
|
77
|
+
</CardContent>
|
|
78
|
+
</Card>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export type { CollectionRateCardProps } from "./collection-rate-card";
|
|
2
|
+
export { CollectionRateCard } from "./collection-rate-card";
|
|
3
|
+
export type { CollectionRateData } from "./use-collection-rate";
|
|
4
|
+
export { COLLECTION_RATE_CACHE_KEY, useCollectionRateData } from "./use-collection-rate";
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Collection rate hook using the entity stats API.
|
|
3
|
+
* Server-side aggregation for accurate totals.
|
|
4
|
+
*/
|
|
5
|
+
import { useQueries } from "@tanstack/react-query";
|
|
6
|
+
import { useSDK } from "@/ui/providers/sdk-provider";
|
|
7
|
+
import { STATS_QUERY_CACHE_KEY } from "../shared/use-stats-query";
|
|
8
|
+
|
|
9
|
+
export const COLLECTION_RATE_CACHE_KEY = "dashboard-collection-rate";
|
|
10
|
+
|
|
11
|
+
export type CollectionRateData = {
|
|
12
|
+
collectionRate: number;
|
|
13
|
+
totalCollected: number;
|
|
14
|
+
totalInvoiced: number;
|
|
15
|
+
currency: string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export function useCollectionRateData(entityId: string | undefined) {
|
|
19
|
+
const { sdk } = useSDK();
|
|
20
|
+
|
|
21
|
+
const queries = useQueries({
|
|
22
|
+
queries: [
|
|
23
|
+
// Total invoiced (non-voided)
|
|
24
|
+
{
|
|
25
|
+
queryKey: [STATS_QUERY_CACHE_KEY, entityId, "total-invoiced"],
|
|
26
|
+
queryFn: async () => {
|
|
27
|
+
if (!entityId || !sdk) throw new Error("Missing entity or SDK");
|
|
28
|
+
return sdk.entityStats.queryEntityStats(
|
|
29
|
+
{
|
|
30
|
+
metrics: [{ type: "sum", field: "total_with_tax", alias: "total" }],
|
|
31
|
+
table: "invoices",
|
|
32
|
+
filters: { is_draft: false, voided_at: null },
|
|
33
|
+
},
|
|
34
|
+
{ entity_id: entityId },
|
|
35
|
+
);
|
|
36
|
+
},
|
|
37
|
+
enabled: !!entityId && !!sdk,
|
|
38
|
+
staleTime: 30_000,
|
|
39
|
+
},
|
|
40
|
+
// Total collected (payments)
|
|
41
|
+
{
|
|
42
|
+
queryKey: [STATS_QUERY_CACHE_KEY, entityId, "total-collected"],
|
|
43
|
+
queryFn: async () => {
|
|
44
|
+
if (!entityId || !sdk) throw new Error("Missing entity or SDK");
|
|
45
|
+
return sdk.entityStats.queryEntityStats(
|
|
46
|
+
{
|
|
47
|
+
metrics: [{ type: "sum", field: "amount", alias: "total" }],
|
|
48
|
+
table: "payments",
|
|
49
|
+
},
|
|
50
|
+
{ entity_id: entityId },
|
|
51
|
+
);
|
|
52
|
+
},
|
|
53
|
+
enabled: !!entityId && !!sdk,
|
|
54
|
+
staleTime: 30_000,
|
|
55
|
+
},
|
|
56
|
+
],
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const [invoicedQuery, collectedQuery] = queries;
|
|
60
|
+
|
|
61
|
+
const totalInvoiced = Number(invoicedQuery.data?.data?.[0]?.total) || 0;
|
|
62
|
+
const totalCollected = Number(collectedQuery.data?.data?.[0]?.total) || 0;
|
|
63
|
+
const collectionRate = totalInvoiced > 0 ? (totalCollected / totalInvoiced) * 100 : 0;
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
data: {
|
|
67
|
+
collectionRate,
|
|
68
|
+
totalCollected,
|
|
69
|
+
totalInvoiced,
|
|
70
|
+
currency: "EUR", // TODO: Get from entity settings
|
|
71
|
+
} as CollectionRateData,
|
|
72
|
+
isLoading: queries.some((q) => q.isLoading),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// Chart components with hooks (turnkey)
|
|
2
|
+
|
|
3
|
+
// Simple presentational components (no hooks)
|
|
4
|
+
export { ChartEmptyState } from "./chart-empty-state";
|
|
5
|
+
export type { CollectionRateCardProps, CollectionRateData } from "./collection-rate-card";
|
|
6
|
+
export {
|
|
7
|
+
COLLECTION_RATE_CACHE_KEY,
|
|
8
|
+
CollectionRateCard,
|
|
9
|
+
useCollectionRateData,
|
|
10
|
+
} from "./collection-rate-card";
|
|
11
|
+
export type { InvoiceStatusChartData, InvoiceStatusChartProps, InvoiceStatusData } from "./invoice-status-chart";
|
|
12
|
+
export {
|
|
13
|
+
INVOICE_STATUS_CACHE_KEY,
|
|
14
|
+
InvoiceStatusChart,
|
|
15
|
+
useInvoiceStatusData,
|
|
16
|
+
} from "./invoice-status-chart";
|
|
17
|
+
export type { LoadingCardProps } from "./loading-card";
|
|
18
|
+
export { LoadingCard } from "./loading-card";
|
|
19
|
+
export type { PaymentMethodsChartData, PaymentMethodsChartProps, PaymentMethodsData } from "./payment-methods-chart";
|
|
20
|
+
export {
|
|
21
|
+
PAYMENT_METHODS_CACHE_KEY,
|
|
22
|
+
PaymentMethodsChart,
|
|
23
|
+
usePaymentMethodsData,
|
|
24
|
+
} from "./payment-methods-chart";
|
|
25
|
+
export type { PaymentTrendChartData, PaymentTrendChartProps, PaymentTrendData } from "./payment-trend-chart";
|
|
26
|
+
export {
|
|
27
|
+
PAYMENT_TREND_CACHE_KEY,
|
|
28
|
+
PaymentTrendChart,
|
|
29
|
+
usePaymentTrendData,
|
|
30
|
+
} from "./payment-trend-chart";
|
|
31
|
+
export type { RevenueCardProps } from "./revenue-card";
|
|
32
|
+
export { RevenueCard } from "./revenue-card";
|
|
33
|
+
export type { RevenueTrendChartData, RevenueTrendChartProps, RevenueTrendData } from "./revenue-trend-chart";
|
|
34
|
+
export {
|
|
35
|
+
REVENUE_TREND_CACHE_KEY,
|
|
36
|
+
RevenueTrendChart,
|
|
37
|
+
useRevenueTrendData,
|
|
38
|
+
} from "./revenue-trend-chart";
|
|
39
|
+
export type { RevenueData, StatsCountsData } from "./shared";
|
|
40
|
+
// Shared hooks for presentational cards
|
|
41
|
+
export {
|
|
42
|
+
REVENUE_DATA_CACHE_KEY,
|
|
43
|
+
STATS_COUNTS_CACHE_KEY,
|
|
44
|
+
useRevenueData,
|
|
45
|
+
useStatsCountsData,
|
|
46
|
+
} from "./shared";
|
|
47
|
+
export type { StatCardProps } from "./stat-card";
|
|
48
|
+
export { StatCard } from "./stat-card";
|
|
49
|
+
export type { TopCustomersChartData, TopCustomersChartProps, TopCustomersData } from "./top-customers-chart";
|
|
50
|
+
export {
|
|
51
|
+
TOP_CUSTOMERS_CACHE_KEY,
|
|
52
|
+
TopCustomersChart,
|
|
53
|
+
useTopCustomersData,
|
|
54
|
+
} from "./top-customers-chart";
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export type { InvoiceStatusChartData, InvoiceStatusChartProps } from "./invoice-status-chart";
|
|
2
|
+
export { InvoiceStatusChart } from "./invoice-status-chart";
|
|
3
|
+
export type { InvoiceStatusData } from "./use-invoice-status";
|
|
4
|
+
export { INVOICE_STATUS_CACHE_KEY, useInvoiceStatusData } from "./use-invoice-status";
|