2021-06-22 11:13:44 +02:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
import { IFablab } from '../../models/fablab';
|
|
|
|
import { FabInput } from '../base/fab-input';
|
|
|
|
import { FabButton } from '../base/fab-button';
|
|
|
|
import { Price } from '../../models/price';
|
2021-06-30 15:32:10 +02:00
|
|
|
import FormatLib from '../../lib/format';
|
2021-06-22 11:13:44 +02:00
|
|
|
|
2021-07-01 12:34:10 +02:00
|
|
|
declare let Fablab: IFablab;
|
2021-06-22 11:13:44 +02:00
|
|
|
|
|
|
|
interface EditablePriceProps {
|
|
|
|
price: Price,
|
|
|
|
onSave: (price: Price) => void,
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the given price.
|
|
|
|
* When the user clics on the price, switch to the edition mode to allow him modifying the price.
|
|
|
|
*/
|
|
|
|
export const EditablePrice: React.FC<EditablePriceProps> = ({ price, onSave }) => {
|
|
|
|
const [edit, setEdit] = useState<boolean>(false);
|
2021-06-22 17:56:13 +02:00
|
|
|
const [tempPrice, setTempPrice] = useState<string>(`${price.amount}`);
|
2021-06-22 11:13:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves the new price
|
|
|
|
*/
|
|
|
|
const handleValidateEdit = (): void => {
|
|
|
|
const newPrice: Price = Object.assign({}, price);
|
2021-06-22 17:56:13 +02:00
|
|
|
newPrice.amount = parseFloat(tempPrice);
|
2021-06-22 11:13:44 +02:00
|
|
|
onSave(newPrice);
|
2021-06-22 17:56:13 +02:00
|
|
|
toggleEdit();
|
2021-07-01 12:34:10 +02:00
|
|
|
};
|
2021-06-22 11:13:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable or disable the edit mode
|
|
|
|
*/
|
2021-06-22 17:56:13 +02:00
|
|
|
const toggleEdit = (): void => {
|
2021-06-22 11:13:44 +02:00
|
|
|
setEdit(!edit);
|
2021-07-01 12:34:10 +02:00
|
|
|
};
|
2021-06-22 11:13:44 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<span className="editable-price">
|
2021-06-30 15:32:10 +02:00
|
|
|
{!edit && <span className="display-price" onClick={toggleEdit}>{FormatLib.price(price.amount)}</span>}
|
2021-06-22 11:13:44 +02:00
|
|
|
{edit && <span>
|
2021-06-22 17:56:13 +02:00
|
|
|
<FabInput id="price" type="number" step={0.01} defaultValue={price.amount} addOn={Fablab.intl_currency} onChange={setTempPrice} required/>
|
2021-06-22 11:13:44 +02:00
|
|
|
<FabButton icon={<i className="fas fa-check" />} className="approve-button" onClick={handleValidateEdit} />
|
|
|
|
<FabButton icon={<i className="fas fa-times" />} className="cancel-button" onClick={toggleEdit} />
|
|
|
|
</span>}
|
|
|
|
</span>
|
|
|
|
);
|
2021-07-01 12:34:10 +02:00
|
|
|
};
|