Fix newline flicker and implement name and description update

This commit is contained in:
2024-06-12 17:47:32 +02:00
parent a527554bca
commit 6c1a6c1305
3 changed files with 50 additions and 25 deletions

View File

@@ -17,13 +17,19 @@
let per100Element: HTMLTableCellElement let per100Element: HTMLTableCellElement
async function update(event: KeyboardEvent & { currentTarget: (EventTarget & HTMLTableCellElement) }) { async function update(event: KeyboardEvent & { currentTarget: (EventTarget & HTMLTableCellElement) }) {
name = name.trim()
amount = amount.trim()
description = description.trim()
per100 = per100.trim()
if (!per100Edited && event.currentTarget === per100Element) if (!per100Edited && event.currentTarget === per100Element)
per100Edited = true per100Edited = true
if (event.key == 'Enter') { if (event.key == 'Enter') {
item.food = name.trim() event.preventDefault()
item.amount = parseInt(amount.trim()) item.food = name
item.description = description.trim() item.amount = parseInt(amount)
item.description = description
const [dbFood, err]: [Food[], Err] = await FoodService.Create(item) const [dbFood, err]: [Food[], Err] = await FoodService.Create(item)
name = '' name = ''
amount = '' amount = ''
@@ -65,29 +71,29 @@
<td bind:innerText={name} <td bind:innerText={name}
class:border-[3px]={!name} class:border-[3px]={!name}
class:border-red-600={!name} class:border-red-600={!name}
class="px-6 py-4" class="px-6 py-4 overflow-hidden"
contenteditable="true" contenteditable="true"
on:keyup={update}> on:keydown={update}>
</td> </td>
<td bind:innerText={description} <td bind:innerText={description}
class="px-6 py-4 bg-gray-50 dark:bg-gray-800" class="px-6 py-4 bg-gray-50 dark:bg-gray-800 overflow-hidden"
contenteditable="true" contenteditable="true"
on:keyup={update}> on:keydown={update}>
</td> </td>
<td bind:innerText={amount} <td bind:innerText={amount}
class:border-[3px]={!amount} class:border-[3px]={!amount}
class:border-red-600={!amount} class:border-red-600={!amount}
class="px-6 py-4" class="px-6 py-4 overflow-hidden"
contenteditable="true" contenteditable="true"
on:keyup={update}> on:keydown={update}>
</td> </td>
<td bind:this={per100Element} <td bind:this={per100Element}
bind:innerText={per100} bind:innerText={per100}
class="px-6 py-4 bg-gray-50 dark:bg-gray-800" class="px-6 py-4 bg-gray-50 dark:bg-gray-800 overflow-hidden"
class:border-[3px]={!per100} class:border-[3px]={!per100}
class:border-orange-600={!per100} class:border-orange-600={!per100}
contenteditable="true" contenteditable="true"
on:keyup={update}> on:keydown={update}>
</td> </td>
<td class="px-6 py-4"> <td class="px-6 py-4">
</td> </td>

View File

@@ -4,14 +4,27 @@
export let item: Food export let item: Food
let amount: string = item.amount.toString() let amount: string = item.amount.toString()
let per100: string = item.per100?.toString() ?? '' let per100: string = item.per100?.toString() ?? ''
let description: string = item.description ?? ''
let name: string = item.food
async function update(event: KeyboardEvent & { currentTarget: (EventTarget & HTMLTableCellElement) }) { async function update(event: KeyboardEvent & { currentTarget: (EventTarget & HTMLTableCellElement) }) {
amount = amount.trim()
per100 = per100.trim()
description = description.trim()
name = name.trim()
if (event.key == 'Enter') { if (event.key == 'Enter') {
item.amount = parseInt(amount.trim()) event.preventDefault()
item.per100 = parseInt(per100.trim()) item.food = name
item.description = description
item.amount = parseInt(amount)
item.per100 = parseInt(per100)
const [newItem, err] = await FoodService.Update(item) const [newItem, err] = await FoodService.Update(item)
amount = amount.trim() name = item.food
per100 = per100.trim() description = item.description ?? ''
amount = item.amount.toString()
per100 = item.per100?.toString() ?? ''
if (newItem && !err) { if (newItem && !err) {
item = newItem item = newItem
@@ -26,21 +39,25 @@
scope="row"> scope="row">
{item.date} {item.date}
</th> </th>
<td class="px-6 py-4"> <td class="px-6 py-4"
{item.food} contenteditable="true"
bind:innerText={name}
on:keydown={update}>
</td> </td>
<td class="px-6 py-4 bg-gray-50 dark:bg-gray-800"> <td class="px-6 py-4 bg-gray-50 dark:bg-gray-800"
{item.description} contenteditable="true"
bind:innerText={description}
on:keydown={update}>
</td> </td>
<td class="px-6 py-4" <td class="px-6 py-4"
contenteditable="true" contenteditable="true"
bind:innerText={amount} bind:innerText={amount}
on:keyup={update}> on:keydown={update}>
</td> </td>
<td class="px-6 py-4 bg-gray-50 dark:bg-gray-800" <td class="px-6 py-4 bg-gray-50 dark:bg-gray-800"
contenteditable="true" contenteditable="true"
bind:innerText={per100} bind:innerText={per100}
on:keyup={update}> on:keydown={update}>
</td> </td>
<td class="px-6 py-4"> <td class="px-6 py-4">
{item.energy} {item.energy}

View File

@@ -81,10 +81,12 @@ limit 1
async Update(food: Food): Promise<[Food, Err]> { async Update(food: Food): Promise<[Food, Err]> {
await db.execute(` await db.execute(`
update food set update food set
amount = $1, food = $1,
per100 = $2 description = $2,
where rowid = $3 amount = $3,
`, [food.amount, food.per100, food.rowid]) per100 = $4
where rowid = $5
`, [food.food, food.description, food.amount, food.per100, food.rowid])
const res = await db.select<Food[]>(`select ${columns.join(', ')} from foodView where rowid = $1`, [food.rowid]) const res = await db.select<Food[]>(`select ${columns.join(', ')} from foodView where rowid = $1`, [food.rowid])
if (!res) return [food, 'no data found'] if (!res) return [food, 'no data found']
if (res.length == 0) return [food, 'no data found'] if (res.length == 0) return [food, 'no data found']