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

fix: selecting all pages with advanced/detail search corrected #2282

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
158 changes: 83 additions & 75 deletions app/javascript/src/stores/alt/stores/UIStore.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { List, fromJS } from 'immutable';
/* eslint-disable no-param-reassign */
import { List } from 'immutable';
import alt from 'src/stores/alt/alt';

import UIActions from 'src/stores/alt/actions/UIActions';
Expand Down Expand Up @@ -138,7 +139,6 @@ class UIStore {
});
}


handleRerenderGenericWorkflow(params) {
this.state.propGenericWorkflow = params;
if (params.toggle) { this.state.showGenericWorkflow = !this.state.showGenericWorkflow; }
Expand All @@ -157,20 +157,20 @@ class UIStore {
}

handleShowDeviceManagement() {
this.state.showDeviceManagement = true
this.state.showDeviceManagement = true;
}

handleCloseDeviceManagement() {
this.state.showDeviceManagement = false
this.state.showDeviceManagement = false;
}

handleShowElements() {
this.state.showCollectionManagement = false;
}

handleSelectTab(params = {}) {
let type = params.type || "sample"
let tabKey = params.tabKey || 0
const type = params.type || 'sample';
const tabKey = params.tabKey || 0;
this.state[type].activeTab = tabKey;
}

Expand All @@ -187,31 +187,29 @@ class UIStore {
handleCheckAllElements(params) {
this.waitFor(ElementStore.dispatchToken);

let { type, range } = params;
let { elements } = ElementStore.getState();
const { type, range } = params;
const { elements } = ElementStore.getState();

if (range == 'all') {
if (this.state.currentSearchSelection && elements[type + "s"].ids) {
let ids = elements[type + "s"].ids
this.state[type].checkedAll = false
this.state[type].checkedIds = List(ids)
this.state[type].uncheckedIds = List()
if (range === 'all') {
if (elements[`${type}s`].ids) {
const { ids } = elements[`${type}s`];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.state.currentSearchSelection is not set in detail / advanced search
image
therefore condition removed; the other condition is sufficient

this.state[type].checkedAll = false;
this.state[type].checkedIds = List(ids);
} else {
this.state[type].checkedAll = true;
this.state[type].checkedIds = List();
this.state[type].uncheckedIds = List();
}
} else if (range == 'current') {
let curPageIds = elements[type + "s"].elements.reduce(
function (a, b) { return a.concat(b); }, []
).map((e) => { return e.id });
this.state[type].uncheckedIds = List();
} else if (range === 'current') {
const curPageIds = elements[`${type}s`].elements.reduce((a, b) => a.concat(b), []).map((e) => e.id);
this.state[type].checkedAll = false;
this.state[type].uncheckedIds = List();
let checked = this.state[type].checkedIds
let checked = this.state[type].checkedIds;
// Remove duplicates, conserve sorting
if (checked.size > 0) {
let checkedMap = checked.reduce(function (mp, e) { mp[e] = 1; return mp }, {})
for (var i = 0; i < curPageIds.length; i++) {
const checkedMap = checked.reduce((mp, e) => { mp[e] = 1; return mp; }, {});
// eslint-disable-next-line no-plusplus
for (let i = 0; i < curPageIds.length; i++) {
if (!checkedMap[curPageIds[i]]) {
checked = checked.push(curPageIds[i]);
}
Expand All @@ -221,7 +219,7 @@ class UIStore {
this.state[type].checkedIds = List(curPageIds);
}
} else {
this.handleUncheckAllElements(params)
this.handleUncheckAllElements(params);
}
}

Expand All @@ -231,12 +229,12 @@ class UIStore {
}

handleToggleAdvancedSearch(show) {
if (show == null) show = !this.state.showAdvancedSearch
if (show == null) show = !this.state.showAdvancedSearch;
this.state.showAdvancedSearch = show;
}

handleUncheckAllElements(params) {
let { type, range } = params;
const { type } = params;

if (this.state[type]) {
this.state[type].checkedAll = false;
Expand All @@ -256,31 +254,28 @@ class UIStore {
}

handleCheckElement(element) {
let type = element.type;
const { type } = element;

if (this.state[type].checkedAll) {
this.state[type].uncheckedIds =
ArrayUtils.removeFromListByValue(this.state[type].uncheckedIds,
element.id);
}
else {
this.state[type].checkedIds =
ArrayUtils.pushUniq(this.state[type].checkedIds, element.id);
this.state[type].uncheckedIds = ArrayUtils.removeFromListByValue(
this.state[type].uncheckedIds,
element.id
);
} else {
this.state[type].checkedIds = ArrayUtils.pushUniq(this.state[type].checkedIds, element.id);
}

}

handleUncheckElement(element) {
let type = element.type;
const { type } = element;

if (this.state[type].checkedAll) {
this.state[type].uncheckedIds =
ArrayUtils.pushUniq(this.state[type].uncheckedIds, element.id);
}
else {
this.state[type].checkedIds =
ArrayUtils.removeFromListByValue(this.state[type].checkedIds,
element.id);
this.state[type].uncheckedIds = ArrayUtils.pushUniq(this.state[type].uncheckedIds, element.id);
} else {
this.state[type].checkedIds = ArrayUtils.removeFromListByValue(
this.state[type].checkedIds,
element.id
);
}
}

Expand All @@ -300,29 +295,33 @@ class UIStore {
}

handleSelectCollection(collection, hasChanged = false) {
const state = this.state;
const isSync = collection.is_sync_to_me ? true : false;
const { filterCreatedAt, fromDate, toDate, userLabel, productOnly } = state;
const { state } = this;
const isSync = !!collection.is_sync_to_me;
const {
filterCreatedAt, fromDate, toDate, userLabel, productOnly
} = state;

if (!hasChanged) {
hasChanged = !state.currentCollection;
hasChanged = hasChanged || state.currentCollection.id != collection.id;
hasChanged = hasChanged || isSync != state.isSync;
hasChanged = hasChanged || state.currentCollection.id !== collection.id;
hasChanged = hasChanged || isSync !== state.isSync;
hasChanged = hasChanged || state.currentSearchSelection != null;
hasChanged = hasChanged || state.currentSearchByID != null;
}

if (collection['clearSearch']) {
if (collection.clearSearch) {
this.handleClearSearchSelection();
hasChanged = true;
collection['clearSearch'] = undefined;
collection.clearSearch = undefined;
}

if (hasChanged && !collection.noFetch) {
this.state.isSync = isSync;
this.state.currentCollection = collection;
const per_page = state.number_of_results;
const params = { per_page, filterCreatedAt, fromDate, toDate, userLabel, productOnly };
const perPage = state.number_of_results;
const params = {
perPage, filterCreatedAt, fromDate, toDate, userLabel, productOnly
};
const { profile } = UserStore.getState();

if (profile && profile.data && profile.data.layout) {
Expand All @@ -333,25 +332,30 @@ class UIStore {
} else {
if (layout.sample && layout.sample > 0) {
ElementActions.fetchSamplesByCollectionId(
collection.id, Object.assign(params, { page: state.sample.page }),
isSync, ElementStore.getState().moleculeSort
collection.id,
Object.assign(params, { page: state.sample.page }),
isSync,
ElementStore.getState().moleculeSort
);
}
if (layout.reaction && layout.reaction > 0) {
ElementActions.fetchReactionsByCollectionId(
collection.id, Object.assign(params, { page: state.reaction.page }),
collection.id,
Object.assign(params, { page: state.reaction.page }),
isSync
);
}
if (layout.wellplate && layout.wellplate > 0) {
ElementActions.fetchWellplatesByCollectionId(
collection.id, Object.assign(params, { page: state.wellplate.page }),
collection.id,
Object.assign(params, { page: state.wellplate.page }),
isSync
);
}
if (layout.screen && layout.screen > 0) {
ElementActions.fetchScreensByCollectionId(
collection.id, Object.assign(params, { page: state.screen.page }),
collection.id,
Object.assign(params, { page: state.screen.page }),
isSync
);
}
Expand All @@ -368,7 +372,8 @@ class UIStore {
);
}

Object.keys(layout).filter(l => !['sample', 'reaction', 'screen', 'wellplate', 'research_plan', 'cell_line'].includes(l)).forEach((key) => {
Object.keys(layout).filter((l) => !['sample', 'reaction', 'screen', 'wellplate', 'research_plan', 'cell_line']
.includes(l)).forEach((key) => {
if (typeof layout[key] !== 'undefined' && layout[key] > 0) {
const page = state[key] ? state[key].page : 1;
ElementActions.fetchGenericElsByCollectionId(
Expand All @@ -385,19 +390,21 @@ class UIStore {
}

handleSelectCollectionForSearchById(layout, collection) {
const state = this.state;
const isSync = state.isSync;
const { state } = this;
const { isSync } = state;
const searchResult = { ...state.currentSearchByID };
const { filterCreatedAt, fromDate, toDate, userLabel, productOnly } = state;
const {
filterCreatedAt, fromDate, toDate, userLabel, productOnly
} = state;
const { moleculeSort } = ElementStore.getState();
const per_page = state.number_of_results;
const perPage = state.number_of_results;

Object.keys(state.currentSearchByID).forEach((key) => {
if (layout[key.slice(0, -1)] > 0 && searchResult[key].totalElements > 0) {
if (productOnly && key != 'samples') { return }
if (productOnly && key !== 'samples') { return; }
let filterParams = {};
const elnElements = ['sample', 'reaction', 'screen', 'wellplate', 'research_plan'];
let modelName = !elnElements.includes(key.slice(0, -1)) ? 'element' : key.slice(0, -1);
const modelName = !elnElements.includes(key.slice(0, -1)) ? 'element' : key.slice(0, -1);

if (fromDate || toDate || productOnly || userLabel) {
filterParams = {
Expand All @@ -406,29 +413,29 @@ class UIStore {
to_date: toDate,
user_label: userLabel,
product_only: productOnly,
}
};
}

const with_filter = Object.keys(filterParams).length >= 1 ? true : false;
const withFilter = Object.keys(filterParams).length >= 1;

const selection = {
elementType: 'by_ids',
id_params: {
model_name: modelName,
ids: searchResult[key].ids,
total_elements: searchResult[key].totalElements,
with_filter: with_filter,
withFilter,
},
list_filter_params: filterParams,
search_by_method: 'search_by_ids',
page_size: per_page
page_size: perPage
};

ElementActions.fetchBasedOnSearchResultIds.defer({
selection,
collectionId: collection.id,
isSync: isSync,
page_size: per_page,
isSync,
page_size: perPage,
page: searchResult[key].page,
moleculeSort
});
Expand All @@ -438,7 +445,7 @@ class UIStore {

// FIXME this method is also defined in ElementStore
handleSetPagination(pagination) {
let { type, page } = pagination;
const { type, page } = pagination;
this.state[type].page = page;
}

Expand All @@ -452,7 +459,7 @@ class UIStore {

handleSelectCollectionWithoutUpdating(collection) {
this.state.currentCollection = collection;
this.state.isSync = collection.is_sync_to_me ? true : false;
this.state.isSync = !!collection.is_sync_to_me;
}

handleClearSearchSelection() {
Expand All @@ -467,19 +474,20 @@ class UIStore {
handleChangeNumberOfResultsShown(value) {
this.state.number_of_results = value;
}

handleShowModalChange(params) {
this.state.showModal = params.show ? true : false
this.state.modalParams = params
this.state.showModal = !!params.show;
this.state.modalParams = params;
}

handleHideModal() {
this.state.showModal = false
this.state.showModal = false;
this.state.modalParams = {
show: false,
title: "",
component: null,
action: null
}
};
}

handleSetFilterCreatedAt(filterCreatedAt) {
Expand Down
Loading