Skip to content

Commit

Permalink
Update URL on node click (apache#23593)
Browse files Browse the repository at this point in the history
  • Loading branch information
darkhan.nausharipov committed Oct 21, 2022
1 parent 3ab9507 commit e3b4385
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 26 deletions.
29 changes: 21 additions & 8 deletions learning/tour-of-beam/frontend/lib/models/group.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,28 @@ import 'parent_node.dart';
class GroupModel extends ParentNodeModel {
const GroupModel({
required super.id,
required super.title,
required super.nodes,
required super.parent,
required super.title,
});

GroupModel.fromResponse(GroupResponseModel group)
: super(
id: group.id,
title: group.title,
nodes:
group.nodes.map(NodeModel.fromResponse).toList(growable: false),
);
factory GroupModel.fromResponse(
GroupResponseModel groupResponse,
ParentNodeModel parent,
) {
final group = GroupModel(
id: groupResponse.id,
nodes: [],
parent: parent,
title: groupResponse.title,
);

group.nodes.addAll(
groupResponse.nodes.map<NodeModel>(
(node) => NodeModel.fromResponse(node, group),
),
);

return group;
}
}
29 changes: 19 additions & 10 deletions learning/tour-of-beam/frontend/lib/models/module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,27 @@ class ModuleModel extends ParentNodeModel {

const ModuleModel({
required super.id,
required super.title,
required super.nodes,
required super.parent,
required super.title,
required this.complexity,
});

ModuleModel.fromResponse(ModuleResponseModel module)
: complexity = module.complexity,
super(
id: module.id,
title: module.title,
nodes: module.nodes
.map<NodeModel>(NodeModel.fromResponse)
.toList(growable: false),
);
factory ModuleModel.fromResponse(ModuleResponseModel moduleResponse) {
final module = ModuleModel(
complexity: moduleResponse.complexity,
nodes: [],
id: moduleResponse.id,
parent: null,
title: moduleResponse.title,
);

module.nodes.addAll(
moduleResponse.nodes.map<NodeModel>(
(node) => NodeModel.fromResponse(node, module),
),
);

return module;
}
}
16 changes: 11 additions & 5 deletions learning/tour-of-beam/frontend/lib/models/node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@
import '../repositories/models/node.dart';
import '../repositories/models/node_type_enum.dart';
import 'group.dart';
import 'parent_node.dart';
import 'unit.dart';

abstract class NodeModel {
final String id;
final String title;
final NodeModel? parent;

const NodeModel({
required this.id,
required this.title,
required this.parent,
});

/// Constructs nodes from the response data.
Expand All @@ -36,20 +39,23 @@ abstract class NodeModel {
/// because they come from a golang backend which does not
/// support inheritance, and so they use an extra layer of composition
/// which is inconvenient in Flutter.
static List<NodeModel> fromMaps(List json) {
static List<NodeModel> fromMaps(List json, ParentNodeModel parent) {
return json
.cast<Map<String, dynamic>>()
.map<NodeResponseModel>(NodeResponseModel.fromJson)
.map(fromResponse)
.map((nodeResponse) => fromResponse(nodeResponse, parent))
.toList();
}

static NodeModel fromResponse(NodeResponseModel node) {
static NodeModel fromResponse(
NodeResponseModel node,
ParentNodeModel parent,
) {
switch (node.type) {
case NodeType.group:
return GroupModel.fromResponse(node.group!);
return GroupModel.fromResponse(node.group!, parent);
case NodeType.unit:
return UnitModel.fromResponse(node.unit!);
return UnitModel.fromResponse(node.unit!, parent);
}
}
}
1 change: 1 addition & 0 deletions learning/tour-of-beam/frontend/lib/models/parent_node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ abstract class ParentNodeModel extends NodeModel {

const ParentNodeModel({
required super.id,
required super.parent,
required super.title,
required this.nodes,
});
Expand Down
9 changes: 7 additions & 2 deletions learning/tour-of-beam/frontend/lib/models/unit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@

import '../repositories/models/unit.dart';
import 'node.dart';
import 'parent_node.dart';

class UnitModel extends NodeModel {
UnitModel.fromResponse(UnitResponseModel unit)
: super(id: unit.id, title: unit.title);
UnitModel.fromResponse(UnitResponseModel unit, ParentNodeModel parent)
: super(
id: unit.id,
parent: parent,
title: unit.title,
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,16 @@ class ContentTreeController extends ChangeNotifier {
}

_currentNode = node;
// TODO(alexeyinkin): Set _treeIds from node.
_treeIds = _getNodeAncestors(node, [node.id]);
notifyListeners();
}

List<String> _getNodeAncestors(NodeModel node, List<String> ancestors) {
if (node.parent != null) {
ancestors.add(node.parent!.id);
return _getNodeAncestors(node.parent!, ancestors);
} else {
return ancestors.reversed.toList();
}
}
}
1 change: 1 addition & 0 deletions learning/tour-of-beam/frontend/lib/pages/tour/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class TourNotifier extends ChangeNotifier with PageStateMixin<void> {
);

void _onChanged() {
emitPathChanged();
final currentNode = contentTreeController.currentNode;
if (currentNode is UnitModel) {
final content = _unitContentCache.getUnitContent(
Expand Down

0 comments on commit e3b4385

Please sign in to comment.