|
@@ -3,7 +3,9 @@ import { IconButton } from '../button/IconButton';
|
|
import styles from './caloriesInput.css';
|
|
import styles from './caloriesInput.css';
|
|
|
|
|
|
type CaloriesInputProps = {
|
|
type CaloriesInputProps = {
|
|
- addCalories: (calories: { title: string, count: number}) => void
|
|
|
|
|
|
+ addCalories: (calories: { title: string, count: number}) => void,
|
|
|
|
+ saveCalories: (calories: { title: string, count: number}) => void,
|
|
|
|
+ item?: CalorieValue
|
|
};
|
|
};
|
|
|
|
|
|
interface ICalorieInputForm extends HTMLFormElement {
|
|
interface ICalorieInputForm extends HTMLFormElement {
|
|
@@ -11,7 +13,7 @@ interface ICalorieInputForm extends HTMLFormElement {
|
|
caloriesCount: HTMLInputElement;
|
|
caloriesCount: HTMLInputElement;
|
|
}
|
|
}
|
|
|
|
|
|
-function CaloriesInput ({ addCalories }: CaloriesInputProps) {
|
|
|
|
|
|
+function CaloriesInput ({ addCalories, saveCalories, item }: CaloriesInputProps) {
|
|
function onSubmit (event: FormEvent<ICalorieInputForm>) {
|
|
function onSubmit (event: FormEvent<ICalorieInputForm>) {
|
|
event.preventDefault();
|
|
event.preventDefault();
|
|
|
|
|
|
@@ -19,30 +21,43 @@ function CaloriesInput ({ addCalories }: CaloriesInputProps) {
|
|
const title = form.caloriesTitle.value;
|
|
const title = form.caloriesTitle.value;
|
|
const count = Number(form.caloriesCount.value) || 0;
|
|
const count = Number(form.caloriesCount.value) || 0;
|
|
|
|
|
|
- addCalories({ title, count });
|
|
|
|
|
|
+ if (!item) {
|
|
|
|
+ addCalories({ title, count });
|
|
|
|
|
|
- form.querySelectorAll('input').forEach((formElement) => {
|
|
|
|
- formElement.value = '';
|
|
|
|
- formElement.blur();
|
|
|
|
- });
|
|
|
|
|
|
+ form.querySelectorAll('input').forEach((formElement) => {
|
|
|
|
+ formElement.value = '';
|
|
|
|
+ formElement.blur();
|
|
|
|
+ });
|
|
|
|
+ } else {
|
|
|
|
+ saveCalories({ title, count});
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ const {
|
|
|
|
+ title: itemTitle = '',
|
|
|
|
+ count: itemCount = ''
|
|
|
|
+ } = item || {};
|
|
|
|
+
|
|
|
|
+ const buttonIcon = item
|
|
|
|
+ ? 'tick'
|
|
|
|
+ : 'plus';
|
|
|
|
+
|
|
return (
|
|
return (
|
|
<form onSubmit={onSubmit} className={styles.caloriesInput}>
|
|
<form onSubmit={onSubmit} className={styles.caloriesInput}>
|
|
<div className={styles.inputWrapper}>
|
|
<div className={styles.inputWrapper}>
|
|
<div className={styles.formRow}>
|
|
<div className={styles.formRow}>
|
|
<label htmlFor="caloriesTitle">Nahrungsmittel:</label>
|
|
<label htmlFor="caloriesTitle">Nahrungsmittel:</label>
|
|
- <input id="caloriesTitle" name="caloriesTitle" type="text" required />
|
|
|
|
|
|
+ <input id="caloriesTitle" name="caloriesTitle" type="text" defaultValue={itemTitle} required />
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<div className={styles.formRow}>
|
|
<div className={styles.formRow}>
|
|
<label htmlFor="caloriesCount">Kalorien:</label>
|
|
<label htmlFor="caloriesCount">Kalorien:</label>
|
|
- <input id="caloriesCount" name="caloriesCount" type="number" required />
|
|
|
|
|
|
+ <input id="caloriesCount" name="caloriesCount" type="number" defaultValue={itemCount} required />
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<div className={styles.buttonWrapper}>
|
|
<div className={styles.buttonWrapper}>
|
|
- <IconButton icon="plus" buttonClassName={styles.addButton} />
|
|
|
|
|
|
+ <IconButton icon={buttonIcon} buttonClassName={styles.addButton} />
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</form>
|
|
);
|
|
);
|