Skip to content

Commit 4121519

Browse files
- implemented #1357
1 parent 4d96ff4 commit 4121519

File tree

4 files changed

+64
-21
lines changed

4 files changed

+64
-21
lines changed

js/components/preview/tags/details/newTagDetailRow.js

+35-16
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import React, { memo, useState, useEffect } from 'react';
1+
import React, { memo, useState, useEffect, useMemo } from 'react';
22
import { useDispatch, useSelector } from 'react-redux';
33
import { CATEGORY_TYPE, CATEGORY_ID, CATEGORY_TYPE_BY_ID } from '../../../../constants/constants';
44
import { ColorPicker } from '../../../common/Components/ColorPicker';
55
import {
66
DEFAULT_CATEGORY,
77
DEFAULT_TAG_COLOR,
88
augumentTagObjectWithId,
9-
createMoleculeTagObject
9+
createMoleculeTagObject,
10+
getEditNewTagCategories
1011
} from '../utils/tagUtils';
1112
import { DJANGO_CONTEXT } from '../../../../utils/djangoContext';
1213
import { updateTagProp, removeSelectedTag } from '../redux/dispatchActions';
@@ -73,11 +74,15 @@ const NewTagDetailRow = memo(({ moleculesToEditIds, moleculesToEdit }) => {
7374
const allMolList = useSelector(state => state.apiReducers.all_mol_lists);
7475
const categoriesList = useSelector(state => state.apiReducers.categoryList);
7576

76-
const [newTagCategory, setNewTagCategory] = useState(1);
77+
const [newTagCategory, setNewTagCategory] = useState(DEFAULT_CATEGORY);
7778
const [newTagColor, setNewTagColor] = useState(DEFAULT_TAG_COLOR);
7879
const [newTagName, setNewTagName] = useState('');
7980
const [newTagLink, setNewTagLink] = useState('');
8081

82+
const comboCategories = useMemo(() => {
83+
return getEditNewTagCategories(categoriesList);
84+
}, [categoriesList]);
85+
8186
useEffect(() => {
8287
const category = dispatch(getCategoryById(DEFAULT_CATEGORY));
8388
if (category) {
@@ -160,18 +165,32 @@ const NewTagDetailRow = memo(({ moleculesToEditIds, moleculesToEdit }) => {
160165
const updateTag = () => {
161166
if (tagToEdit && newTagCategory && newTagName) {
162167
// update all props at once
163-
dispatch(
164-
updateTagProp(
165-
Object.assign({}, tagToEdit, {
166-
category: newTagCategory,
167-
colour: newTagColor,
168-
tag: newTagName,
169-
discourse_url: newTagLink
170-
}),
171-
newTagName,
172-
'tag'
173-
)
174-
);
168+
if (newTagCategory) {
169+
dispatch(
170+
updateTagProp(
171+
Object.assign({}, tagToEdit, {
172+
category: newTagCategory,
173+
colour: newTagColor,
174+
tag: newTagName,
175+
discourse_url: newTagLink
176+
}),
177+
newTagName,
178+
'tag'
179+
)
180+
);
181+
} else {
182+
dispatch(
183+
updateTagProp(
184+
Object.assign({}, tagToEdit, {
185+
colour: newTagColor,
186+
tag: newTagName,
187+
discourse_url: newTagLink
188+
}),
189+
newTagName,
190+
'tag'
191+
)
192+
);
193+
}
175194
// reset tag/fields after updating selected one
176195
resetTagToEditState();
177196
}
@@ -240,7 +259,7 @@ const NewTagDetailRow = memo(({ moleculesToEditIds, moleculesToEdit }) => {
240259
onChange={onCategoryForNewTagChange}
241260
disabled={!DJANGO_CONTEXT.pk}
242261
>
243-
{categoriesList?.map(c => (
262+
{comboCategories?.map(c => (
244263
<MenuItem key={`tag-editor-new-category-${c.id}`} value={c.id}>
245264
{c.category}
246265
</MenuItem>

js/components/preview/tags/tagCategory.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useSelector } from 'react-redux';
44
import TagCategoryView from './tagCategoryListView';
55
import TagCategoryGridView from './tagCategoryGridView';
66
import { CATEGORY_TYPE } from '../../../constants/constants';
7-
import { compareTagsAsc } from './utils/tagUtils';
7+
import { compareTagsAsc, getProhibitedCategoriesForEditIds } from './utils/tagUtils';
88

99
const useStyles = makeStyles(theme => ({
1010
category: {
@@ -16,10 +16,11 @@ const TagCategory = memo(({ tagClickCallback, disabled = false }) => {
1616
const classes = useStyles();
1717

1818
const categoryList = useSelector(state => state.apiReducers.categoryList);
19+
const listOfProhibitedCategories = getProhibitedCategoriesForEditIds(categoryList);
1920
let tagList = useSelector(state => state.apiReducers.tagList);
2021
tagList = tagList
2122
.filter(t => {
22-
if (t.additional_info?.downloadName) {
23+
if (t.additional_info?.downloadName || listOfProhibitedCategories.some(cid => cid === t.category)) {
2324
return false;
2425
} else {
2526
return true;

js/components/preview/tags/tagView.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,11 @@ const TagView = memo(
130130
if (tagCategory) {
131131
if (!tag.colour || tag.colour === '') {
132132
setBgColor(`#${tagCategory.colour}`);
133+
} else {
134+
setBgColor(`${tag.colour}`);
133135
}
134136
}
135-
}, [tagCategory]);
137+
}, [tagCategory, tag.colour, tagCategories]);
136138

137139
useEffect(() => {
138140
if (assignTagView === undefined) {

js/components/preview/tags/utils/tagUtils.js

+23-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import {
33
CATEGORY_TYPE_BY_ID,
44
OBSERVATION_TAG_CATEGORIES,
55
COMPOUND_PRIO_TAG_CATEGORIES,
6-
TAG_DETAILS_REMOVED_CATEGORIES
6+
TAG_DETAILS_REMOVED_CATEGORIES,
7+
NON_ASSIGNABLE_CATEGORIES
78
} from '../../../../constants/constants';
89

910
export const DEFAULT_TAG_COLOR = '#E0E0E0';
10-
export const DEFAULT_CATEGORY = 1;
11+
export const DEFAULT_CATEGORY = 8;
1112

1213
export const createMoleculeTagObject = (
1314
tagName,
@@ -247,3 +248,23 @@ export const getAllTagsForLHSCmp = (observations, tagList, tagCategoryList) => {
247248
export const getDefaultTagDiscoursePostText = tag => {
248249
return `This post for tag ${tag.tag} is here to discuss its contents.`;
249250
};
251+
252+
export const getEditNewTagCategories = tagCategoryList => {
253+
let result = [];
254+
255+
result = tagCategoryList?.filter(categ => !NON_ASSIGNABLE_CATEGORIES.some(c => c === categ.category)) || [];
256+
257+
return result;
258+
};
259+
260+
export const getProhibitedCategoriesForEdit = tagCategoryList => {
261+
let result = [];
262+
263+
result = tagCategoryList?.filter(categ => NON_ASSIGNABLE_CATEGORIES.some(c => c === categ.category)) || [];
264+
265+
return result;
266+
};
267+
268+
export const getProhibitedCategoriesForEditIds = tagCategoryList => {
269+
return getProhibitedCategoriesForEdit(tagCategoryList).map(c => c.id);
270+
};

0 commit comments

Comments
 (0)