Skip to content

Commit 786ebcd

Browse files
committed
[Fix] unbundling aid tree map navigation & drop down bar ordering #488
1 parent b61a348 commit 786ebcd

File tree

2 files changed

+40
-30
lines changed
  • src/components/molecules/UnbundlingAid

2 files changed

+40
-30
lines changed

src/components/molecules/UnbundlingAid/UnbundlingAidToolBar/index.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { KeyValue, Selections } from '../types';
77
import { DragDropContext, Draggable, DropResult, Droppable } from 'react-beautiful-dnd';
88
import { Intro } from '../../../atoms/Intro';
99
import { howTo } from '../../../../utils/howTo';
10+
import { SelectionKey } from '../UnbundlingTreemap';
1011

1112
export interface Props {
1213
aidType: string;
@@ -17,12 +18,12 @@ export interface Props {
1718
position?: number;
1819
values: KeyValue[];
1920
textAlign?: 'left' | 'right'| 'center';
20-
onMove: (key: string) => void;
21+
onMove: (key: string, keys: SelectionKey[]) => void;
2122
onChange: (key: string) => (value: string) => void;
2223
}
2324

2425
export interface State {
25-
keys: string[];
26+
keys: SelectionKey[];
2627
}
2728

2829
const ToolBarContainer: GlamorousComponent<any, any> = glamorous.div<{compact?: boolean}>(
@@ -46,7 +47,7 @@ export default class ToolBar extends React.Component<Props, State> {
4647
return obj ? obj.value : 'All';
4748
}
4849

49-
public static reorder(list: string[], startIndex: number, endIndex: number): string[] {
50+
public static reorder(list: SelectionKey[], startIndex: number, endIndex: number): SelectionKey[] {
5051
const [ removed ] = list.splice(startIndex, 1); // gets a hold of the item
5152
list.splice(endIndex, 0, removed); // adds item in new place
5253

@@ -66,7 +67,7 @@ export default class ToolBar extends React.Component<Props, State> {
6667

6768
constructor(props: Props) {
6869
super(props);
69-
const keys = Object.keys(this.props.toolBarOptions).filter(key => key !== 'years');
70+
const keys = Object.keys(this.props.toolBarOptions).filter(key => key !== 'years') as SelectionKey[];
7071
this.state = { keys };
7172
this.onDragEnd = this.onDragEnd.bind(this);
7273
this.captureVisualSentence = this.captureVisualSentence.bind(this);
@@ -75,12 +76,12 @@ export default class ToolBar extends React.Component<Props, State> {
7576
public onDragEnd(result: DropResult) {
7677
// dropped outside the list
7778
if (!result.destination) { return; }
78-
const keys = ToolBar.reorder(
79+
const keys: SelectionKey[] = ToolBar.reorder(
7980
this.state.keys,
8081
result.source.index,
8182
result.destination.index
8283
);
83-
this.props.onMove(result.draggableId);
84+
this.props.onMove(result.draggableId, keys);
8485
this.setState({
8586
keys
8687
});

src/components/molecules/UnbundlingAid/UnbundlingTreemap/index.tsx

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export interface Props {
2323
refetch: (variables: object) => any;
2424
}
2525

26-
type SelectionKey = 'to' | 'from' | 'sectors' | 'channels' | 'forms' | 'years';
26+
export type SelectionKey = 'to' | 'from' | 'sectors' | 'channels' | 'forms' | 'years';
2727

2828
export interface State {
2929
position: number;
@@ -88,28 +88,28 @@ class UnbundlingTreemap extends React.Component<Props, State> {
8888
return value ? [ ...valuesItems, { key, value } ] : valuesItems;
8989
}
9090

91-
public static getActiveOption = (position: number): string =>
92-
Object.keys(UnbundlingTreemap.groupers)[position]
91+
// public static getActiveOption = (position: number): string =>
92+
// Object.keys(UnbundlingTreemap.groupers)[position]
9393

9494
constructor(props: Props) {
9595
super(props);
9696

97-
const position = 1;
98-
const keys = Object.keys(props.selections) as SelectionKey[];
97+
const position = 0;
98+
const keys = Object.keys(props.selections).filter(key => key !== 'years') as SelectionKey[];
9999
const values = [ { key: 'years', value: props.startYear.toString() } ];
100100
this.state = { position, keys, values, active: 'to' };
101101
this.history.push(this.state);
102102
}
103103

104104
public onZoomIn = ({ id, color }: Selected) => {
105-
const position = this.state.position <= 1 ? 1 : this.state.position;
105+
const position = this.state.position;
106106

107-
if ((position + 1) < Object.keys(UnbundlingTreemap.groupers).length) {
107+
if ((position + 1) < this.state.keys.length) {
108108
// const newKey = this.state.keys[position + 1];
109109
// TODO: check if key already exists
110-
const currentActive = UnbundlingTreemap.getActiveOption(position);
110+
const currentActive = this.state.keys[position];
111111
const values = UnbundlingTreemap.addNewValue({ key: currentActive, value: id }, this.state.values);
112-
const active = UnbundlingTreemap.getActiveOption(position + 1) as SelectionKey;
112+
const active = this.state.keys[position + 1] as SelectionKey;
113113
this.setState({ position: position + 1, values, dimmerColor: color, active });
114114
this.history.push(this.state);
115115
this.fetch(active, values);
@@ -129,19 +129,23 @@ class UnbundlingTreemap extends React.Component<Props, State> {
129129
public updateValue = (key: string) => (value: string) => {
130130
// making sure we dont have duplicates
131131
const values = UnbundlingTreemap.addNewValue({ key, value }, this.state.values);
132-
const active = UnbundlingTreemap.getActiveOption(this.state.position) as SelectionKey;
132+
const active = this.state.keys[this.state.position] as SelectionKey;
133133
this.setState({ values, active });
134134
this.history.push(this.state);
135135
this.fetch(active, values);
136136
}
137137

138-
public onMove = (key: string) => {
139-
const groupersKeys = Object.keys(UnbundlingTreemap.groupers) as SelectionKey[];
140-
const activeKeyPosition = indexOf(key, groupersKeys);
141-
this.setState({ position: activeKeyPosition, active: key as SelectionKey });
142-
this.history.push(this.state);
138+
public onMove = (key: string, keys: SelectionKey[]) => {
139+
// const groupersKeys = Object.keys(UnbundlingTreemap.groupers) as SelectionKey[];
140+
const keyPosition = indexOf(key, keys);
141+
if (keyPosition === this.state.position) {
142+
this.setState({ active: key as SelectionKey, keys });
143+
this.history.push(this.state);
143144

144-
return this.fetch(key, this.state.values);
145+
this.fetch(key, this.state.values);
146+
} else {
147+
this.setState({ keys });
148+
}
145149
}
146150

147151
public fetch(active: string, values: KeyValue[]) {
@@ -215,21 +219,26 @@ class UnbundlingTreemap extends React.Component<Props, State> {
215219
onClick={ this.onZoomIn }
216220
/>
217221
}
218-
{
219-
this.state.position <= 1
220-
? ''
221-
:
222-
<Up className="up" onClick={ this.onZoomOut }>
223-
<Icon name={ 'chevron left' } size="big" inverted />
224-
</Up>
225-
}
222+
{ this.renderBackButton() }
226223
</Intro>
227224
</Container>
228225
}
229226

230227
</div>
231228
);
232229
}
230+
231+
private renderBackButton() {
232+
if (this.state.position > 0) {
233+
return (
234+
<Up className="up" onClick={ this.onZoomOut }>
235+
<Icon name={ 'chevron left' } size="big" inverted />
236+
</Up>
237+
);
238+
}
239+
240+
return null;
241+
}
233242
}
234243

235244
export default UnbundlingTreemap;

0 commit comments

Comments
 (0)