Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STSMACOM-792: Extend Tags component to accept mutateEntity prop #1420

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Show the username in the "last updated" accordion in the Note editing pane. Fixes STSMACOM-748.
* Added `indexRef` and `inputRef` props to `<SearchAndSort>`. Refs STSMACOM-788.
* Extend NotesAccordion and NotesSmartAccodion components to accept a prop hideNewButton. Refs STSMACOM-789.
* Extend `Tags` component to accept `mutateEntity` prop. Refs STSMACOM-792.

## [9.0.0](https://github.com/folio-org/stripes-smart-components/tree/v9.0.0) (2023-10-11)
[Full Changelog](https://github.com/folio-org/stripes-smart-components/compare/v8.0.0...v9.0.0)
Expand Down
36 changes: 22 additions & 14 deletions lib/Tags/Tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Tags extends React.Component {
entityTagsPath: PropTypes.string,
getEntity: PropTypes.func,
getEntityTags: PropTypes.func,
mutateEntity: PropTypes.func,
link: PropTypes.string.isRequired,
mutator: PropTypes.shape({
entities: PropTypes.shape({ PUT: PropTypes.func.isRequired }),
Expand Down Expand Up @@ -84,25 +85,32 @@ class Tags extends React.Component {
this.saveTags(tags);
};

onError = error => {
const errorId = error.status === OPTIMISTIC_LOCKING_STATUS
? 'stripes-components.optimisticLocking.saveError'
: 'stripes-smart-components.cannotSaveTagToRecord';

this.showCallout('error', errorId);
};

onRemove = tag => {
const {
getEntity,
getEntityTags,
entityTagsPath,
mutateEntity,
} = this.props;
const entity = getEntity(this.props);
const tags = getEntityTags(this.props);
const tagList = tags.filter(t => t !== tag);

set(entity, entityTagsPath, { tagList });
this.props.mutator.entities.PUT(entity)
.catch(err => {
if (err.status !== OPTIMISTIC_LOCKING_STATUS) {
return;
}

this.showCallout('error', 'stripes-components.optimisticLocking.saveError');
});
if (mutateEntity) {
mutateEntity(entity, { onError: error => this.onError(error) });
} else {
this.props.mutator.entities.PUT(entity).catch(error => this.onError(error));
}
};

// add tag to the list of entity tags
Expand All @@ -111,19 +119,19 @@ class Tags extends React.Component {
getEntity,
getEntityTags,
entityTagsPath,
mutateEntity,
} = this.props;
const entity = getEntity(this.props);
const tagList = getEntityTags(this.props);
const tagsToSave = { tagList: sortBy(uniq([...tags, ...tagList])) };

set(entity, entityTagsPath, tagsToSave);
this.props.mutator.entities.PUT(entity)
.catch(err => {
const errorId = err.status === OPTIMISTIC_LOCKING_STATUS
? 'stripes-components.optimisticLocking.saveError'
: 'stripes-smart-components.cannotSaveTagToRecord';

this.showCallout('error', errorId);
});
if (mutateEntity) {
mutateEntity(entity, { onError: error => this.onError(error) });
} else {
this.props.mutator.entities.PUT(entity).catch(error => this.onError(error));
}
}

// add tags to global list of tags
Expand Down