|
1 |
| -import React, { memo, useState, useEffect } from 'react'; |
| 1 | +import React, { memo, useState, useEffect, useMemo } from 'react'; |
2 | 2 | import { useDispatch, useSelector } from 'react-redux';
|
3 | 3 | import { CATEGORY_TYPE, CATEGORY_ID, CATEGORY_TYPE_BY_ID } from '../../../../constants/constants';
|
4 | 4 | import { ColorPicker } from '../../../common/Components/ColorPicker';
|
5 | 5 | import {
|
6 | 6 | DEFAULT_CATEGORY,
|
7 | 7 | DEFAULT_TAG_COLOR,
|
8 | 8 | augumentTagObjectWithId,
|
9 |
| - createMoleculeTagObject |
| 9 | + createMoleculeTagObject, |
| 10 | + getEditNewTagCategories |
10 | 11 | } from '../utils/tagUtils';
|
11 | 12 | import { DJANGO_CONTEXT } from '../../../../utils/djangoContext';
|
12 | 13 | import { updateTagProp, removeSelectedTag } from '../redux/dispatchActions';
|
@@ -73,11 +74,15 @@ const NewTagDetailRow = memo(({ moleculesToEditIds, moleculesToEdit }) => {
|
73 | 74 | const allMolList = useSelector(state => state.apiReducers.all_mol_lists);
|
74 | 75 | const categoriesList = useSelector(state => state.apiReducers.categoryList);
|
75 | 76 |
|
76 |
| - const [newTagCategory, setNewTagCategory] = useState(1); |
| 77 | + const [newTagCategory, setNewTagCategory] = useState(DEFAULT_CATEGORY); |
77 | 78 | const [newTagColor, setNewTagColor] = useState(DEFAULT_TAG_COLOR);
|
78 | 79 | const [newTagName, setNewTagName] = useState('');
|
79 | 80 | const [newTagLink, setNewTagLink] = useState('');
|
80 | 81 |
|
| 82 | + const comboCategories = useMemo(() => { |
| 83 | + return getEditNewTagCategories(categoriesList); |
| 84 | + }, [categoriesList]); |
| 85 | + |
81 | 86 | useEffect(() => {
|
82 | 87 | const category = dispatch(getCategoryById(DEFAULT_CATEGORY));
|
83 | 88 | if (category) {
|
@@ -160,18 +165,32 @@ const NewTagDetailRow = memo(({ moleculesToEditIds, moleculesToEdit }) => {
|
160 | 165 | const updateTag = () => {
|
161 | 166 | if (tagToEdit && newTagCategory && newTagName) {
|
162 | 167 | // 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 | + } |
175 | 194 | // reset tag/fields after updating selected one
|
176 | 195 | resetTagToEditState();
|
177 | 196 | }
|
@@ -240,7 +259,7 @@ const NewTagDetailRow = memo(({ moleculesToEditIds, moleculesToEdit }) => {
|
240 | 259 | onChange={onCategoryForNewTagChange}
|
241 | 260 | disabled={!DJANGO_CONTEXT.pk}
|
242 | 261 | >
|
243 |
| - {categoriesList?.map(c => ( |
| 262 | + {comboCategories?.map(c => ( |
244 | 263 | <MenuItem key={`tag-editor-new-category-${c.id}`} value={c.id}>
|
245 | 264 | {c.category}
|
246 | 265 | </MenuItem>
|
|
0 commit comments