Skip to content

Commit

Permalink
Merge pull request #20 from concord-consortium/184835669-new-attr-nam…
Browse files Browse the repository at this point in the history
…e-fix

If any collection has attr with same name, add index. (PT-184835669)
  • Loading branch information
lublagg authored May 3, 2023
2 parents 0cd2df5 + 0048095 commit 4749525
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/components/hierarchy-view/add-buttons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const AddAttribute = ({collection, handleAddAttribute}: IProps) => {
return (
<div ref={ref} className={css.createNewAttr}>
<input type="textbox" defaultValue={"newAttr"} onChange={handleChange}></input>
<button onClick={handleNewAttrNameClick}>Add Attribute</button>
<button onClick={handleNewAttrNameClick}>Create</button>
</div>
);
};
Expand Down
6 changes: 4 additions & 2 deletions src/hooks/useCodapState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,13 @@ export const useCodapState = () => {
const handleAddAttribute = async (collection: ICollection, attrName: string) => {
const proposedName = attrName.length ? attrName : "newAttr";
let newAttributeName;
const attrNameAlreadyUsed = collection.attrs.find((attr) => attr.name === proposedName);
const allAttributes: Array<any> = [];
collections.map((coll) => coll.attrs.map((attr) => allAttributes.push(attr)));
const attrNameAlreadyUsed = allAttributes.find((attr) => attr.name === proposedName);
if (!attrNameAlreadyUsed) {
newAttributeName = proposedName;
} else {
const attrsWithSameName = collection.attrs.filter((attr) => attr.name.startsWith(proposedName));
const attrsWithSameName = allAttributes.filter((attr) => attr.name.startsWith(proposedName));
const indexes = attrsWithSameName.map((attr) => Number(attr.name.slice(proposedName.length)));
const highestIndex = Math.max(...indexes);
if (!highestIndex) {
Expand Down

0 comments on commit 4749525

Please sign in to comment.