chore(react/promoted_attributes): add logic to remove multi labels

This commit is contained in:
Elian Doran 2025-11-23 11:19:41 +02:00
parent 33b19e40e0
commit 598bb6d742
No known key found for this signature in database
2 changed files with 35 additions and 37 deletions

View File

@ -6,6 +6,8 @@ import FAttribute from "../entities/fattribute";
import clsx from "clsx"; import clsx from "clsx";
import { t } from "../services/i18n"; import { t } from "../services/i18n";
import { DefinitionObject } from "../services/promoted_attribute_definition_parser"; import { DefinitionObject } from "../services/promoted_attribute_definition_parser";
import server from "../services/server";
import FNote from "../entities/fnote";
interface Cell { interface Cell {
definitionAttr: FAttribute; definitionAttr: FAttribute;
@ -15,6 +17,8 @@ interface Cell {
} }
interface CellProps { interface CellProps {
note: FNote;
componentId: string;
cell: Cell, cell: Cell,
cells: Cell[], cells: Cell[],
shouldFocus: boolean; shouldFocus: boolean;
@ -23,7 +27,7 @@ interface CellProps {
} }
export default function PromotedAttributes() { export default function PromotedAttributes() {
const { note } = useNoteContext(); const { note, componentId } = useNoteContext();
const [ cells, setCells ] = useState<Cell[]>(); const [ cells, setCells ] = useState<Cell[]>();
const [ viewType ] = useNoteLabel(note, "viewType"); const [ viewType ] = useNoteLabel(note, "viewType");
const [ cellToFocus, setCellToFocus ] = useState<Cell>(); const [ cellToFocus, setCellToFocus ] = useState<Cell>();
@ -71,7 +75,12 @@ export default function PromotedAttributes() {
return ( return (
<div className="promoted-attributes-widget"> <div className="promoted-attributes-widget">
<div className="promoted-attributes-container"> <div className="promoted-attributes-container">
{cells?.map(cell => <PromotedAttributeCell cell={cell} cells={cells} setCells={setCells} shouldFocus={cell === cellToFocus} setCellToFocus={setCellToFocus} />)} {note && cells?.map(cell => <PromotedAttributeCell
cell={cell}
cells={cells} setCells={setCells}
shouldFocus={cell === cellToFocus} setCellToFocus={setCellToFocus}
componentId={componentId} note={note}
/>)}
</div> </div>
</div> </div>
); );
@ -112,7 +121,7 @@ function ActionCell() {
) )
} }
function MultiplicityCell({ cell, cells, setCells, setCellToFocus }: CellProps) { function MultiplicityCell({ cell, cells, setCells, setCellToFocus, note, componentId }: CellProps) {
return (cell.definition.multiplicity === "multi" && return (cell.definition.multiplicity === "multi" &&
<td className="multiplicity"> <td className="multiplicity">
<PromotedActionButton <PromotedActionButton
@ -140,8 +149,30 @@ function MultiplicityCell({ cell, cells, setCells, setCellToFocus }: CellProps)
<PromotedActionButton <PromotedActionButton
icon="bx bx-trash" icon="bx bx-trash"
title={t("promoted_attributes.remove_this_attribute")} title={t("promoted_attributes.remove_this_attribute")}
onClick={() => { onClick={async () => {
// Remove the attribute from the server if it exists.
const { attributeId, type } = cell.valueAttr;
const valueName = cell.valueName;
if (attributeId) {
await server.remove(`notes/${note.noteId}/attributes/${attributeId}`, componentId);
}
const index = cells.indexOf(cell);
const isLastOneOfType = cells.filter(c => c.valueAttr.type === type && c.valueAttr.name === valueName).length < 2;
const newOnesToInsert: Cell[] = [];
if (isLastOneOfType) {
newOnesToInsert.push({
...cell,
valueAttr: {
attributeId: "",
type: cell.valueAttr.type,
name: cell.valueName,
value: ""
}
})
}
console.log("Delete at ", index, isLastOneOfType);
setCells(cells.toSpliced(index, 1, ...newOnesToInsert));
}} }}
/> />
</td> </td>

View File

@ -163,39 +163,6 @@ export default class PromotedAttributesWidget extends NoteContextAwareWidget {
return; return;
} }
if (definition.multiplicity === "multi") {
const $removeButton = $("<span>")
.on("click", async () => {
const attributeId = $input.attr("data-attribute-id");
if (attributeId) {
await server.remove(`notes/${this.noteId}/attributes/${attributeId}`, this.componentId);
}
// if it's the last one the create new empty form immediately
const sameAttrSelector = `input[data-attribute-type='${valueAttr.type}'][data-attribute-name='${valueName}']`;
if (this.$widget.find(sameAttrSelector).length <= 1) {
const $new = await this.createPromotedAttributeCell(
definitionAttr,
{
attributeId: "",
type: valueAttr.type,
name: valueName,
value: ""
},
valueName
);
if ($new) {
$wrapper.after($new);
}
}
$wrapper.remove();
});
}
return $wrapper; return $wrapper;
} }