Skip to content

Commit

Permalink
STSMACOM-210: Disallow unassigning of the note if links number is equ…
Browse files Browse the repository at this point in the history
…al to 1 (#532)

* STSMACOM-210: Disallow unassigning of the note if links number is equal to 1
  • Loading branch information
Yurii Danylenko authored May 31, 2019
1 parent c85179e commit cd30281
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 11 deletions.
3 changes: 2 additions & 1 deletion lib/Notes/NoteForm/NoteView.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
KeyValue,
} from '@folio/stripes-components';

import { NOTE_LINKS_MIN_NUMBER } from '../constants';
import AssignmentsList from '../AssignmentsList';
import ReferredRecord from '../ReferredRecord';
import styles from './NoteForm.css';
Expand Down Expand Up @@ -143,7 +144,7 @@ export default class NoteView extends Component {
referredEntityData,
} = this.props;

const hasMoreThanOneAssignment = get(noteData, 'links.length', 0) > 1;
const hasMoreThanOneAssignment = get(noteData, 'links.length', 0) > NOTE_LINKS_MIN_NUMBER;
const canUnassign = hasMoreThanOneAssignment && referredEntityData;

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
}

.assign-checkbox label::before {
margin-left: 10px;
margin-right: 10px;
margin-left: 5px;
margin-right: 5px;
}

.assign-checkbox label::after {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
import {
notesStatuses,
sortOrders,
NOTE_LINKS_MIN_NUMBER,
} from '../../../constants';

import {
Expand Down Expand Up @@ -65,6 +66,7 @@ const notesStatusOptions = [

const noteShape = PropTypes.shape({
id: PropTypes.string.isRequired,
linksNumber: PropTypes.number.isRequired,
status: PropTypes.oneOf([ASSIGNED, UNASSIGNED]),
title: PropTypes.node.isRequired,
});
Expand Down Expand Up @@ -113,20 +115,27 @@ export default class NotesAssigningModal extends React.Component {
};

firstColumnHeaderFormatter = {
[ASSIGNING]: (record) => {
const isAssigned = record.status === ASSIGNED;
[ASSIGNING]: (note) => {
const isAssigned = note.status === ASSIGNED;
const isDisabled = isAssigned && this.getNoteLinksNumber(note) === NOTE_LINKS_MIN_NUMBER;

return (
<Checkbox
role="button"
disabled={isDisabled}
checked={isAssigned}
className={styles['assign-checkbox']}
onClick={() => this.onSingleAssignClick(record)}
onClick={() => this.onSingleAssignClick(note)}
/>
);
},
};

getNoteLinksNumber(note) {
const newStatus = this.state.changedNoteIdToStatusMap.get(note.id);
return newStatus === ASSIGNED ? note.linksNumber + 1 : note.linksNumber;
}

onSingleAssignClick = (note) => {
this.setState((state, props) => {
return {
Expand Down Expand Up @@ -194,10 +203,13 @@ export default class NotesAssigningModal extends React.Component {
selectedStatusFilters,
} = this.state;

this.props.onSearch({
query,
selectedStatusFilters,
});
this.setState(
{ changedNoteIdToStatusMap: new Map() },
this.props.onSearch({
query,
selectedStatusFilters,
})
);
}

onSearchQueryChange = (event) => {
Expand Down Expand Up @@ -246,12 +258,14 @@ export default class NotesAssigningModal extends React.Component {
};
};


getColumnMapping = () => {
return {
[ASSIGNING]: (
<Checkbox
role="button"
name={ASSIGNING}
checked={this.isEveryNoteAssigned()}
className={styles['assign-checkbox']}
onClick={this.onAssignAllClick}
/>
Expand All @@ -261,6 +275,21 @@ export default class NotesAssigningModal extends React.Component {
};
}

isEveryNoteAssigned() {
const { notes } = this.props;
const incomingAssignedNotes = notes.items.filter((note) => note.status === ASSIGNED);

let potentialAssignedNotesCount = 0;

for (const potentialStatus of this.state.changedNoteIdToStatusMap.values()) {
if (potentialStatus === ASSIGNED) {
potentialAssignedNotesCount++;
}
}

return incomingAssignedNotes.length + potentialAssignedNotesCount === notes.items.length;
}

onAssignAllClick = (event) => {
const { checked } = event.target;

Expand All @@ -270,6 +299,9 @@ export default class NotesAssigningModal extends React.Component {

this.props.notes.items.forEach((curIncomingNote) => {
if (curIncomingNote.status !== newStatus) {
if (newStatus === UNASSIGNED && this.getNoteLinksNumber(curIncomingNote) === NOTE_LINKS_MIN_NUMBER) {
return;
}
newChangedNoteIdToStatusMap.set(curIncomingNote.id, newStatus);
} else {
newChangedNoteIdToStatusMap.delete(curIncomingNote.id);
Expand Down Expand Up @@ -311,10 +343,17 @@ export default class NotesAssigningModal extends React.Component {

const {
fetchDomainNotes,
notes,
} = this.props;

const offset = get(this.props, 'notes.items.length');

if (offset === notes.totalCount) {
return;
}

fetchDomainNotes({
offset: get(this.props, 'notes.items.length'),
offset,
query,
selectedStatusFilters,
});
Expand Down
1 change: 1 addition & 0 deletions lib/Notes/NotesSmartAccordion/NotesSmartAccordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class NotesSmartAccordion extends Component {
return {
...pick(domainNote, ['id', 'title']),
status,
linksNumber: domainNote.links.length,
};
});

Expand Down
2 changes: 2 additions & 0 deletions lib/Notes/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ export const sortOrders = {
};

export const STATUS_FILTERS_NUMBER = 2;

export const NOTE_LINKS_MIN_NUMBER = 1;

0 comments on commit cd30281

Please sign in to comment.