diff --git a/apps/client/src/widgets/PromotedAttributes.tsx b/apps/client/src/widgets/PromotedAttributes.tsx index a7a0c9d7a..1a62b49e1 100644 --- a/apps/client/src/widgets/PromotedAttributes.tsx +++ b/apps/client/src/widgets/PromotedAttributes.tsx @@ -6,6 +6,8 @@ import FAttribute from "../entities/fattribute"; import clsx from "clsx"; import { t } from "../services/i18n"; import { DefinitionObject } from "../services/promoted_attribute_definition_parser"; +import server from "../services/server"; +import FNote from "../entities/fnote"; interface Cell { definitionAttr: FAttribute; @@ -15,6 +17,8 @@ interface Cell { } interface CellProps { + note: FNote; + componentId: string; cell: Cell, cells: Cell[], shouldFocus: boolean; @@ -23,7 +27,7 @@ interface CellProps { } export default function PromotedAttributes() { - const { note } = useNoteContext(); + const { note, componentId } = useNoteContext(); const [ cells, setCells ] = useState(); const [ viewType ] = useNoteLabel(note, "viewType"); const [ cellToFocus, setCellToFocus ] = useState(); @@ -71,7 +75,12 @@ export default function PromotedAttributes() { return (
- {cells?.map(cell => )} + {note && cells?.map(cell => )}
); @@ -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" && { + 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)); }} /> diff --git a/apps/client/src/widgets/promoted_attributes.ts b/apps/client/src/widgets/promoted_attributes.ts index 8d10b34da..eb73c6885 100644 --- a/apps/client/src/widgets/promoted_attributes.ts +++ b/apps/client/src/widgets/promoted_attributes.ts @@ -163,39 +163,6 @@ export default class PromotedAttributesWidget extends NoteContextAwareWidget { return; } - if (definition.multiplicity === "multi") { - const $removeButton = $("") - .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; }