Skip to content

Commit

Permalink
Add a button to add new ranks in trees
Browse files Browse the repository at this point in the history
  • Loading branch information
CarolineDenis committed Mar 18, 2024
1 parent 0c6b825 commit 19bc954
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 5 deletions.
73 changes: 69 additions & 4 deletions specifyweb/frontend/js_src/lib/components/TreeView/Tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ import { userPreferences } from '../Preferences/userPreferences';
import type { Conformations, Row, Stats } from './helpers';
import { fetchStats } from './helpers';
import { TreeRow } from './Row';
import { commonText } from '../../localization/common';
import { Dialog } from '../Molecules/Dialog';
import { Form, Input, Label } from '../Atoms/Form';
import { Submit } from '../Atoms/Submit';
import { ping } from '../../utils/ajax/ping';
import { LoadingContext } from '../Core/Contexts';

const treeToPref = {
Geography: 'geography',
Expand Down Expand Up @@ -100,6 +106,25 @@ export function Tree<SCHEMA extends AnyTree>({
[baseUrl, statsThreshold]
);

const loading = React.useContext(LoadingContext);
const rankId = useId('add-tree-rank')('');
const [isAddingRank, setIsAddingRank] = React.useState(false);
const [newRankName, setNewRankName] = React.useState('');
function addRank(parentRankName: string): void {
const url = `/api/specify_tree/${tableName.toLowerCase()}/add_tree_rank/`;
loading(
ping(url, {
method: 'POST',
body: {
newRankName: newRankName,
parentRankName: parentRankName,
treeName: tableName.toLowerCase(),
},
}).then(() => globalThis.location.reload())
);
setIsAddingRank(false);
}

return (
<div
className={`
Expand Down Expand Up @@ -172,10 +197,50 @@ export function Tree<SCHEMA extends AnyTree>({
: rankName) as LocalizedString
}
</Button.LikeLink>
{isEditingRanks &&
collapsedRanks?.includes(rank.rankId) !== true ? (
<EditTreeRank rank={rank} />
) : undefined}
{isEditingRanks && (
<>
{collapsedRanks?.includes(rank.rankId) !== true ? (
<EditTreeRank rank={rank} />
) : undefined}
<Button.Icon
icon="plus"
title={treeText.addNewRank()}
onClick={() => setIsAddingRank(true)}
/>
</>
)}
{isAddingRank && (
<Dialog
buttons={
<>
<Button.DialogClose>
{commonText.cancel()}
</Button.DialogClose>
<Submit.Info form={rankId}>
{commonText.create()}
</Submit.Info>
</>
}
onClose={() => setIsAddingRank(false)}
header={treeText.addNewRank()}
>
<Form
id={rankId}
onSubmit={(): void => {
addRank(rankName);
}}
>
<Label.Block>
{treeText.newRankName()}
<Input.Text
required
value={newRankName}
onValueChange={setNewRankName}
/>
</Label.Block>
</Form>
</Dialog>
)}
</div>
);
})}
Expand Down
8 changes: 8 additions & 0 deletions specifyweb/frontend/js_src/lib/components/TreeView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ function TreeView<SCHEMA extends AnyTree>({
setLastFocusedTree={() => setLastFocusedTree(type)}
tableName={tableName}
treeDefinitionItems={treeDefinitionItems}
onRefresh={(): void => {
// Force re-load
setRows(undefined);
globalThis.setTimeout(() => {
setLastFocusedRow(undefined);
setRows(rows);
}, 0);
}}
/>
);

Expand Down
6 changes: 6 additions & 0 deletions specifyweb/frontend/js_src/lib/localization/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,4 +459,10 @@ export const treeText = createDictionary({
'uk-ua': 'Синхронізувати',
'ru-ru': 'Это приведет к безвозвратному удалению следующего ресурса',
},
addNewRank: {
'en-us': 'Add New Rank',
},
newRankName: {
'en-us': 'New Rank Name',
},
} as const);
5 changes: 4 additions & 1 deletion specifyweb/specify/tree_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ def add_tree_rank(request, tree) -> HttpResponse:
new_rank_name = data.get('newRankName')
parent_rank_name = data.get('parentRankName')
tree_id = data.get('treeID', 1)
tree_name = data.get('name')
new_rank_title = data.get('newRankTitle', new_rank_name)
use_default_rank_ids = data.get('useDefaultRankIDs', True)

Expand All @@ -533,7 +534,7 @@ def add_tree_rank(request, tree) -> HttpResponse:

# Determine the new rank id parameters
new_rank_id = None
tree_def = tree_def_model.objects.get(id=tree_id)
tree_def = tree_def_model.objects.get(name=tree_name) if tree_name else tree_def_model.objects.get(id=tree_id)
parent_rank = tree_def_item_model.objects.filter(treedef=tree_def, name=parent_rank_name).first()
if parent_rank is None and parent_rank_name != 'root':
return HttpResponseBadRequest('Target rank name does not exist')
Expand Down Expand Up @@ -684,6 +685,7 @@ def delete_tree_rank(request, tree) -> HttpResponse:
data = json.loads(request.body)
rank_name = data.get('rankName')
tree_id = data.get('treeID', 1)
tree_name = data.get('name')

# Throw exceptions if the required parameters are not given correctly
if rank_name is None:
Expand All @@ -697,6 +699,7 @@ def delete_tree_rank(request, tree) -> HttpResponse:
tree_def_model = getattr(spmodels, tree_def_model_name)
tree_def_item_model = getattr(spmodels, tree_def_item_model_name)
tree_def = tree_def_model.objects.get(id=tree_id)
tree_def = tree_def_model.objects.get(name=tree_name) if tree_name else tree_def_model.objects.get(id=tree_id)

# Make sure no nodes are present in the rank before deleting rank
rank = tree_def_item_model.objects.get(name=rank_name)
Expand Down

0 comments on commit 19bc954

Please sign in to comment.