Skip to content

Commit

Permalink
feat: add total price to invoice generator (#548)
Browse files Browse the repository at this point in the history
* add total price to invoice generator

* pass props directly & fix extra render

* update color to gray
  • Loading branch information
emiliarepo authored Oct 31, 2024
1 parent 99057c1 commit f157547
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 42 deletions.
81 changes: 39 additions & 42 deletions apps/web/src/components/invoice-generator/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,42 +33,20 @@ function InputLabel({ name, htmlId }: { name: string; htmlId: string }) {
}

function InputRow({
placeholder,
label,
id,
name,
autoComplete,
type,
defaultValue,
multiple,
required,
step,
min,
onBeforeInput: onInput,
unit,
maxLength,
label,
name,
...restProps
}: GenericFieldProps) {
const htmlId = id ?? `inputfield.${name}`;

return (
<div>
<InputLabel name={label} htmlId={htmlId} />
<span className="flex">
<Input
id={htmlId}
name={name}
type={type ?? "text"}
placeholder={placeholder}
autoComplete={autoComplete}
defaultValue={defaultValue}
required={required}
multiple={multiple}
step={step}
min={min}
onBeforeInput={onInput}
maxLength={maxLength}
/>
{unit ? <span className="ml-2 translate-y-1">{unit}</span> : null}
<Input id={htmlId} type={type ?? "text"} {...restProps} />
{unit ? <span className="ml-1 translate-y-1">{unit}</span> : null}
</span>
</div>
);
Expand Down Expand Up @@ -239,6 +217,10 @@ function InvoiceItem({
}) {
const t = useScopedI18n("invoicegenerator");

const [quantity, setQuantity] = useState(0);
const [unitPrice, setUnitPrice] = useState(0);
const totalPrice = quantity * unitPrice;

return (
<fieldset>
<ErrorMessageBlock
Expand All @@ -254,7 +236,7 @@ function InvoiceItem({
/>
</ErrorMessageBlock>
<fieldset className="flex">
<span className="mr-0.5 grow">
<span className="mr-1 grow">
<ErrorMessageBlock
elementName={`rows[${index.toString()}].quantity`}
formState={state}
Expand All @@ -264,11 +246,12 @@ function InvoiceItem({
name="rows.quantity"
id={`rows[${index.toString()}].quantity`}
type="number"
onInput={(e) => setQuantity(Number(e.currentTarget.value))}

Check warning on line 249 in apps/web/src/components/invoice-generator/index.tsx

View workflow job for this annotation

GitHub Actions / Format, Lint, Check types & Build

Returning a void expression from an arrow function shorthand is forbidden. Please add braces to the arrow function
required
/>
</ErrorMessageBlock>
</span>
<span className="ml-0.5 grow">
<span className="ml-1 grow">
<ErrorMessageBlock
elementName={`rows[${index.toString()}].unit`}
formState={state}
Expand All @@ -284,23 +267,37 @@ function InvoiceItem({
</ErrorMessageBlock>
</span>
</fieldset>
<ErrorMessageBlock
elementName={`rows[${index.toString()}].unit_price`}
formState={state}
>
<span>
<fieldset className="flex">
<span className="mr-1 grow">
<ErrorMessageBlock
elementName={`rows[${index.toString()}].unit_price`}
formState={state}
>
<InputRow
label={t("Unit price")}
type="number"
name="rows.unit_price"
id={`rows[${index.toString()}].unit_price`}
onInput={(e) => setUnitPrice(Number(e.currentTarget.value))}

Check warning on line 281 in apps/web/src/components/invoice-generator/index.tsx

View workflow job for this annotation

GitHub Actions / Format, Lint, Check types & Build

Returning a void expression from an arrow function shorthand is forbidden. Please add braces to the arrow function
required
step={0.01}
min={0}
unit="€"
/>
</ErrorMessageBlock>
</span>
<span className="ml-1 grow">
<InputRow
label={t("Unit price")}
type="number"
name="rows.unit_price"
id={`rows[${index.toString()}].unit_price`}
required
step={0.01}
min={0}
label={t("Total price")}
type="text"
name="rows.total_price"
value={totalPrice.toFixed(2)}
id={`rows[${index.toString()}].total_price`}
unit="€"
disabled={true}

Check warning on line 297 in apps/web/src/components/invoice-generator/index.tsx

View workflow job for this annotation

GitHub Actions / Format, Lint, Check types & Build

Value must be omitted for boolean attribute `disabled`
/>
</span>
</ErrorMessageBlock>
</fieldset>
</fieldset>
);
}
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const en = {
"invoicegenerator.Quantity": "Quantity",
"invoicegenerator.Unit": "Unit",
"invoicegenerator.Unit price": "Unit price",
"invoicegenerator.Total price": "Total price",
"invoicegenerator.Attachment": "Attachment",
"invoicegenerator.Attachments": "Attachments",
"invoicegenerator.Items": "Items",
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/locales/fi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const fi = {
"invoicegenerator.Quantity": "Määrä",
"invoicegenerator.Unit": "Yksikkö",
"invoicegenerator.Unit price": "Yksikköhinta",
"invoicegenerator.Total price": "Yhteensä",
"invoicegenerator.Attachment": "Liite",
"invoicegenerator.Attachments": "Liitteet",
"invoicegenerator.Items": "Erittely",
Expand Down

0 comments on commit f157547

Please sign in to comment.