-
Notifications
You must be signed in to change notification settings - Fork 4
Feat/invoice #1007
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Feat/invoice #1007
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0261d4e
feat: invoices api
lissavxo 7f3a121
feat: invoices modal
lissavxo 18cbdc5
refactor: clean up
lissavxo 7ede8f5
fix: improve next invoice number method
lissavxo 047ed4a
refactor: clean up
lissavxo 39371f9
Merge branch 'master' into feat/invoice
chedieck 6b4ef0f
refactor: clean up
lissavxo e8a73e6
feat: invoice transaction optional
lissavxo c94dcf8
refactor: clean up
lissavxo 6f61d12
feat: use prisma decimal
lissavxo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,240 @@ | ||
| import React, { useState, useEffect, ReactElement } from 'react' | ||
| import style from './transaction.module.css' | ||
| import Button from 'components/Button' | ||
| import { CreateInvoicePOSTParameters } from 'utils/validators' | ||
| import axios from 'axios' | ||
| import { Prisma } from '@prisma/client' | ||
|
|
||
| export interface InvoiceData { | ||
| id?: string | ||
| invoiceNumber: Prisma.Decimal | ||
| amount: number | ||
| recipientName: string | ||
| recipientAddress: string | ||
| description: string | ||
| customerName: string | ||
| customerAddress: string | ||
| } | ||
|
|
||
| interface InvoiceModalProps { | ||
| isOpen: boolean | ||
| onClose: () => void | ||
| transaction: any | ||
| invoiceData: InvoiceData | null | ||
| mode: 'create' | 'edit' | 'view' | ||
| } | ||
|
|
||
| export default function InvoiceModal ({ | ||
| isOpen, | ||
| onClose, | ||
| invoiceData, | ||
| transaction, | ||
| mode | ||
| }: InvoiceModalProps): ReactElement | null { | ||
| const [formData, setFormData] = useState<InvoiceData>({ | ||
| invoiceNumber: '', | ||
| amount: Number(transaction?.amount), | ||
| recipientName: '', | ||
| recipientAddress: transaction?.address?.address, | ||
| description: '', | ||
| customerName: '', | ||
| customerAddress: '' | ||
| }) | ||
|
|
||
| useEffect(() => { | ||
| setFormData(invoiceData ?? { | ||
| invoiceNumber: '', | ||
| amount: Number(transaction?.amount), | ||
| recipientName: '', | ||
| recipientAddress: transaction?.address?.address, | ||
| description: '', | ||
| customerName: '', | ||
| customerAddress: '' | ||
| }) | ||
| }, [transaction, mode, invoiceData]) | ||
|
|
||
| if (!isOpen) return null | ||
|
|
||
| const handleChange = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>): void => { | ||
| const { name, value } = e.target | ||
| setFormData(prev => ({ ...prev, [name]: value })) | ||
| } | ||
|
|
||
| const handleModalClose = (): void => { | ||
| setFormData({ | ||
| invoiceNumber: '', | ||
| amount: 0, | ||
| recipientName: '', | ||
| recipientAddress: '', | ||
| description: '', | ||
| customerName: '', | ||
| customerAddress: '' | ||
| }) | ||
| onClose() | ||
| } | ||
|
|
||
| async function handleSubmit (e: React.FormEvent): Promise<void> { | ||
| e.preventDefault() | ||
|
|
||
| if (mode === 'edit') { | ||
| await updateInvoice() | ||
| } else { | ||
| await createInvoice() | ||
| } | ||
| onClose() | ||
| } | ||
| async function createInvoice (): Promise<void> { | ||
| const payload: CreateInvoicePOSTParameters = { | ||
| ...formData, | ||
| transactionId: transaction?.id | ||
| } | ||
|
|
||
| try { | ||
| await axios.post('/api/invoices', payload) | ||
| } catch (err: any) { | ||
| console.error('Invoice submission error:', err) | ||
| } | ||
| } | ||
|
|
||
| async function updateInvoice (): Promise<void> { | ||
| const payload: CreateInvoicePOSTParameters = { | ||
| ...formData, | ||
| transactionId: transaction?.id | ||
| } | ||
|
|
||
| try { | ||
| await axios.put(`/api/invoices/?invoiceId=${invoiceData?.id ?? ''}`, payload) | ||
| onClose() | ||
| } catch (err: any) { | ||
| console.error('Invoice update error:', err) | ||
| } | ||
| } | ||
| const isReadOnly = mode === 'view' | ||
|
|
||
| return ( | ||
| <div className={style.form_ctn_outer}> | ||
| <div className={style.form_ctn_inner}> | ||
| <h4>{mode === 'edit' ? 'Edit Invoice' : mode === 'view' ? 'View Invoice' : 'Create Invoice'}</h4> | ||
| <div className={style.form_ctn}> | ||
| {!isReadOnly | ||
| ? <form onSubmit={(e) => { | ||
| void handleSubmit(e) | ||
| }} method="post"> | ||
| <div style={{ display: 'flex', gap: '1rem' }}> | ||
| <div style={{ display: 'flex', flexDirection: 'column', width: '100%' }}> | ||
| <label htmlFor="invoiceNumber">Invoice Number</label> | ||
| <input | ||
| type="text" | ||
| id="invoiceNumber" | ||
| name="invoiceNumber" | ||
| value={formData.invoiceNumber} | ||
| onChange={handleChange} | ||
| autoFocus | ||
| /> | ||
| </div> | ||
| <div style={{ display: 'flex', flexDirection: 'column' }}> | ||
| <label htmlFor="amount">Amount</label> | ||
| <input | ||
| type="number" | ||
| id="amount" | ||
| name="amount" | ||
| value={formData.amount ?? ''} | ||
| onChange={handleChange} | ||
| disabled={true} | ||
| /> | ||
| </div> | ||
| </div> | ||
|
|
||
| <label htmlFor="recipientName">Recipient Name</label> | ||
| <input | ||
| type="text" | ||
| id="recipientName" | ||
| name="recipientName" | ||
| value={formData.recipientName} | ||
| onChange={handleChange} | ||
| /> | ||
|
|
||
| <label htmlFor="recipientAddress">Recipient Address</label> | ||
| <input | ||
| type="text" | ||
| id="recipientAddress" | ||
| name="recipientAddress" | ||
| value={formData.recipientAddress} | ||
| onChange={handleChange} | ||
| disabled={true} | ||
| /> | ||
|
|
||
| <label htmlFor="description">Description</label> | ||
| <textarea | ||
| id="description" | ||
| name="description" | ||
| value={formData.description} | ||
| onChange={handleChange} | ||
| ></textarea> | ||
|
|
||
| <label htmlFor="customerName">Customer Name</label> | ||
| <input | ||
| type="text" | ||
| id="customerName" | ||
| name="customerName" | ||
| value={formData.customerName} | ||
| onChange={handleChange} | ||
| disabled={isReadOnly} | ||
| /> | ||
|
|
||
| <label htmlFor="customerAddress">Customer Address</label> | ||
| <input | ||
| type="text" | ||
| id="customerAddress" | ||
| name="customerAddress" | ||
| value={formData.customerAddress} | ||
| onChange={handleChange} | ||
| disabled={isReadOnly} | ||
| /> | ||
| <div style={{ display: 'flex', justifyContent: 'space-between', marginTop: '10px' }}> | ||
| <div className="mt-2"> | ||
| <Button type="button" onClick={handleModalClose}> | ||
| {isReadOnly ? 'Close' : 'Cancel'} | ||
| </Button> | ||
| </div> | ||
| {!isReadOnly && ( | ||
| <div className="mt-2"> | ||
| <Button type="submit">Submit</Button> | ||
| </div> | ||
| )} | ||
| </div> | ||
| </form> | ||
| : <div> | ||
| <div className={style.invoice_view}> | ||
| <div className={style.invoice_view_item}> | ||
| <strong>Invoice Number:</strong> {formData.invoiceNumber} | ||
| </div> | ||
| <div className={style.invoice_view_item}> | ||
| <strong>Amount:</strong> {formData.amount} | ||
| </div> | ||
| <div className={style.invoice_view_item}> | ||
| <strong>Recipient Name:</strong> {formData.recipientName} | ||
| </div> | ||
| <div className={style.invoice_view_item}> | ||
| <strong>Recipient Address:</strong> {formData.recipientAddress} | ||
| </div> | ||
| <div className={style.invoice_view_item}> | ||
| <strong>Description:</strong> {formData.description} | ||
| </div> | ||
| <div className={style.invoice_view_item}> | ||
| <strong>Customer Name:</strong> {formData.customerName} | ||
| </div> | ||
| <div className={style.invoice_view_item}> | ||
| <strong>Customer Address:</strong> {formData.customerAddress} | ||
| </div> | ||
| </div> | ||
| <div style={{ marginTop: '20px', display: 'flex', justifyContent: 'flex-end' }}> | ||
| <Button type="button" onClick={handleModalClose}>Close</Button> | ||
| </div> | ||
| </div> | ||
| } | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a good idea to use
numberhere, can get messy with float imprecision. Best to stick toPrisma.DecimalRemember to change this also in the other interfaces you're creating.