Fix issue with cell colors, improve row API interactions to avoid relationship issues due to API response differences

This commit is contained in:
Andrew Kingston 2023-04-11 22:55:13 +01:00
parent 6290112d06
commit 727d3d5b6d
5 changed files with 30 additions and 28 deletions

View File

@ -83,17 +83,18 @@
textarea { textarea {
padding: var(--cell-padding); padding: var(--cell-padding);
margin: 0; margin: 0;
border: 2px solid var(--spectrum-global-color-blue-400); border: 2px solid var(--cell-color);
background: var(--cell-background); background: var(--cell-background);
font-size: var(--cell-font-size); font-size: var(--cell-font-size);
font-family: var(--font-sans); font-family: var(--font-sans);
color: inherit; color: inherit;
position: absolute; position: absolute;
top: -1px; top: 0;
left: -1px; left: 0;
width: calc(100% + 100px); width: calc(100% + 100px);
height: calc(5 * var(--row-height) + 1px); height: calc(5 * var(--row-height) + 1px);
z-index: 1; z-index: 1;
border-radius: 2px;
} }
textarea.invert { textarea.invert {
transform: translateY(-100%); transform: translateY(-100%);

View File

@ -52,7 +52,6 @@
if (!row?._id) { if (!row?._id) {
return false return false
} }
console.log(lookupMap)
return lookupMap?.[row._id] === true return lookupMap?.[row._id] === true
} }

View File

@ -16,7 +16,7 @@
const getStyle = (width, selectedUser) => { const getStyle = (width, selectedUser) => {
let style = `flex: 0 0 ${width}px;` let style = `flex: 0 0 ${width}px;`
if (selectedUser) { if (selectedUser) {
style += `--user-color:${selectedUser.color};` style += `--cell-color:${selectedUser.color};`
} }
return style return style
} }
@ -70,6 +70,7 @@
background: var(--cell-background); background: var(--cell-background);
position: relative; position: relative;
width: 0; width: 0;
--cell-color: transparent;
} }
/* Cell border */ /* Cell border */
@ -82,7 +83,7 @@
left: 0; left: 0;
height: 100%; height: 100%;
width: 100%; width: 100%;
border: 2px solid transparent; border: 2px solid var(--cell-color);
pointer-events: none; pointer-events: none;
border-radius: 2px; border-radius: 2px;
box-sizing: border-box; box-sizing: border-box;
@ -106,11 +107,11 @@
.cell.focused { .cell.focused {
z-index: 2; z-index: 2;
} }
.cell.focused:after { .cell.focused {
border-color: var(--spectrum-global-color-blue-400); --cell-color: var(--spectrum-global-color-blue-400);
} }
.cell.error:after { .cell.error {
border-color: var(--spectrum-global-color-red-500); --cell-color: var(--spectrum-global-color-red-500);
} }
.cell:not(.focused) { .cell:not(.focused) {
user-select: none; user-select: none;

View File

@ -25,12 +25,12 @@
} }
const duplicate = async () => { const duplicate = async () => {
menu.actions.close()
const newRow = await rows.actions.duplicateRow($focusedRow) const newRow = await rows.actions.duplicateRow($focusedRow)
if (newRow) { if (newRow) {
const column = $stickyColumn?.name || $columns[0].name const column = $stickyColumn?.name || $columns[0].name
$focusedCellId = `${newRow._id}-${column}` $focusedCellId = `${newRow._id}-${column}`
} }
menu.actions.close()
} }
</script> </script>

View File

@ -190,7 +190,8 @@ export const deriveStores = context => {
const addRow = async (row, idx, bubble = false) => { const addRow = async (row, idx, bubble = false) => {
try { try {
// Create row // Create row
const newRow = await API.saveRow({ ...row, tableId: get(tableId) }) let newRow = await API.saveRow({ ...row, tableId: get(tableId) })
newRow = await fetchRow(newRow._id)
// Update state // Update state
if (idx != null) { if (idx != null) {
@ -202,6 +203,8 @@ export const deriveStores = context => {
} else { } else {
handleNewRows([newRow]) handleNewRows([newRow])
} }
// Refresh row to ensure data is in the correct format
notifications.success("Row created successfully") notifications.success("Row created successfully")
return newRow return newRow
} catch (error) { } catch (error) {
@ -220,26 +223,14 @@ export const deriveStores = context => {
delete clone._rev delete clone._rev
delete clone.__idx delete clone.__idx
try { try {
const newRow = await addRow(clone, row.__idx + 1, true) return await addRow(clone, row.__idx + 1, true)
// We deliberately re-use the majority of the existing row as the API
// returns different metadata for relationships when saving a row compared
// to using the search endpoint. We always want data in the shape that the
// search endpoint returns it.
return {
...row,
__idx: row.__idx + 1,
_id: newRow._id,
_rev: newRow._rev,
}
} catch (error) { } catch (error) {
handleValidationError(row._id, error) handleValidationError(row._id, error)
} }
} }
// Refreshes a specific row, handling updates, addition or deletion // Fetches a row by ID using the search endpoint
const refreshRow = async id => { const fetchRow = async id => {
// Fetch row from the server again
const res = await API.searchTable({ const res = await API.searchTable({
tableId: get(tableId), tableId: get(tableId),
limit: 1, limit: 1,
@ -250,7 +241,13 @@ export const deriveStores = context => {
}, },
paginate: false, paginate: false,
}) })
let newRow = res?.rows?.[0] return res?.rows?.[0]
}
// Refreshes a specific row, handling updates, addition or deletion
const refreshRow = async id => {
// Fetch row from the server again
const newRow = await fetchRow(id)
// Get index of row to check if it exists // Get index of row to check if it exists
const $rows = get(rows) const $rows = get(rows)
@ -331,6 +328,10 @@ export const deriveStores = context => {
// Deletes an array of rows // Deletes an array of rows
const deleteRows = async rowsToDelete => { const deleteRows = async rowsToDelete => {
if (!rowsToDelete?.length) {
return
}
// Actually delete rows // Actually delete rows
rowsToDelete.forEach(row => { rowsToDelete.forEach(row => {
delete row.__idx delete row.__idx