Skip to content

Commit

Permalink
Create initial view switch button (#718)
Browse files Browse the repository at this point in the history
  • Loading branch information
GoelBiju committed Jul 20, 2021
1 parent c815d41 commit 6c05c44
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
3 changes: 2 additions & 1 deletion packages/datagateway-dataview/public/res/default.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"app": {
"results": "Results",
"toggle_cards": "Toggle Cards",
"view_table": "Display as table",
"view_cards": "Display as cards",
"max_results": "Max Results"
},
"breadcrumbs": {
Expand Down
67 changes: 38 additions & 29 deletions packages/datagateway-dataview/src/page/pageContainer.component.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import {
FormControlLabel,
Grid,
LinearProgress,
Paper,
Switch,
Typography,
Theme,
withStyles,
createStyles,
IconButton,
Badge,
makeStyles,
Button,
} from '@material-ui/core';
import ShoppingCartIcon from '@material-ui/icons/ShoppingCart';
import SearchIcon from '@material-ui/icons/Search';
Expand Down Expand Up @@ -246,27 +245,35 @@ const NavBar = (props: {
);
};

const CardSwitch = (props: {
toggleCard: boolean;
handleToggleChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
const ViewSwitch = (props: {
viewCards: boolean;
handleButtonChange: (event: React.MouseEvent<HTMLButtonElement>) => void;
}): React.ReactElement => {
const [t] = useTranslation();

return (
<FormControlLabel
// <FormControlLabel
// className="tour-dataview-toggle-card"
// value="start"
// control={
// <Switch
// checked={props.toggleCard}
// onChange={props.handleToggleChange}
// name="toggleCard"
// inputProps={{ 'aria-label': 'secondary checkbox' }}
// />
// }
// label={t('app.toggle_cards')}
// labelPlacement="start"
// />
<Button
className="tour-dataview-toggle-card"
value="start"
control={
<Switch
checked={props.toggleCard}
onChange={props.handleToggleChange}
name="toggleCard"
inputProps={{ 'aria-label': 'secondary checkbox' }}
/>
}
label={t('app.toggle_cards')}
labelPlacement="start"
/>
variant="contained"
color="primary"
onClick={props.handleButtonChange}
>
{props.viewCards ? t('app.view_table') : t('app.view_cards')}
</Button>
);
};

Expand Down Expand Up @@ -376,7 +383,7 @@ type PageContainerCombinedProps = PageContainerStateProps &

interface PageContainerState {
paths: string[];
toggleCard: boolean;
viewCards: boolean;
modifiedLocation: LocationType;
}

Expand All @@ -396,7 +403,7 @@ class PageContainer extends React.Component<
paths: Object.values(paths.toggle).concat(
Object.values(paths.studyHierarchy.toggle)
),
toggleCard: this.getToggle(),
viewCards: this.getToggle(),
modifiedLocation: props.location,
};
}
Expand Down Expand Up @@ -435,7 +442,7 @@ class PageContainer extends React.Component<
if (prevProps.query.view !== this.props.query.view) {
this.setState({
...this.state,
toggleCard: this.getToggle(),
viewCards: this.getToggle(),
});
}
}
Expand Down Expand Up @@ -481,13 +488,15 @@ class PageContainer extends React.Component<
return 'table';
};

public handleToggleChange = (
event: React.ChangeEvent<HTMLInputElement>
public handleButtonChange = (
event: React.MouseEvent<HTMLButtonElement>
): void => {
const nextView = event.target.checked ? 'card' : 'table';
// TODO: Need state variable to track if clicked or not? Or use state toggleCard?
// const nextView = event.target.checked ? 'card' : 'table';
const nextView = !this.state.viewCards ? 'card' : 'table';

// Save the current view information to state and restore the previous view information.
this.props.saveView(this.state.toggleCard ? 'card' : 'table');
this.props.saveView(nextView);

// Set the view in local storage.
this.storeDataView(nextView);
Expand All @@ -498,7 +507,7 @@ class PageContainer extends React.Component<
// Set the state with the toggled card option and the saved query.
this.setState({
...this.state,
toggleCard: event.target.checked,
viewCards: !this.state.viewCards,
});
};

Expand All @@ -519,9 +528,9 @@ class PageContainer extends React.Component<
exact
path={this.state.paths}
render={() => (
<CardSwitch
toggleCard={this.state.toggleCard}
handleToggleChange={this.handleToggleChange}
<ViewSwitch
viewCards={this.state.viewCards}
handleButtonChange={this.handleButtonChange}
/>
)}
/>
Expand Down

0 comments on commit 6c05c44

Please sign in to comment.