Skip to content
Snippets Groups Projects
Commit 9cff25f9 authored by Thor-Herman Van Eggelen's avatar Thor-Herman Van Eggelen
Browse files

Merge branch 'navbuttons-context' into 'master'

Implement context API for navbuttons #8

See merge request it2810-h20/team-75/prosjekt-2!8
parents 986855ef ffa97859
No related branches found
No related tags found
No related merge requests found
...@@ -8,7 +8,6 @@ import '../Style/ArtPage.css'; ...@@ -8,7 +8,6 @@ import '../Style/ArtPage.css';
type props = { type props = {
pageId: number; // The id of the page pageId: number; // The id of the page
changePage: (arg0: boolean) => void; // A function that takes a bool and returns void
} }
type options = { [key: string]: string }; type options = { [key: string]: string };
...@@ -45,10 +44,10 @@ class ArtPage extends React.Component<props, state> { ...@@ -45,10 +44,10 @@ class ArtPage extends React.Component<props, state> {
</OptionsContext.Provider> </OptionsContext.Provider>
<ExhibitionPiece artId={this.props.pageId}/> <ExhibitionPiece artId={this.props.pageId}/>
<div className={'navbutton left'}> <div className={'navbutton left'}>
<NavButton changePage={this.props.changePage} direction={"left"}/> <NavButton direction={"left"}/>
</div> </div>
<div className={'navbutton right'}> <div className={'navbutton right'}>
<NavButton changePage={this.props.changePage} direction={"right"}/> <NavButton direction={"right"}/>
</div> </div>
</div> </div>
); );
......
...@@ -2,6 +2,6 @@ import React from 'react'; ...@@ -2,6 +2,6 @@ import React from 'react';
const FavoriteButton = () => { const FavoriteButton = () => {
return <div>FavoriteButton</div> return <div>FavoriteButton</div>
} };
export default FavoriteButton; export default FavoriteButton;
\ No newline at end of file
...@@ -3,7 +3,6 @@ import NavButton from './NavButton'; ...@@ -3,7 +3,6 @@ import NavButton from './NavButton';
import "../Style/LandingPage.css" import "../Style/LandingPage.css"
type props = { // Typescript definitions for the arguments received via props type props = { // Typescript definitions for the arguments received via props
changePage: (arg0: boolean) => void; // A function that takes a bool and returns void
} }
type state = { // Typescript definitions for the state variables of the class type state = { // Typescript definitions for the state variables of the class
...@@ -34,10 +33,10 @@ class LandingPage extends React.Component<props, state> { ...@@ -34,10 +33,10 @@ class LandingPage extends React.Component<props, state> {
<p className={'welcome-p'}>Welcome to this interactive gallery! In here you will find unique art pieces, consisting of a quote and SVG. If you turn up your volume you may hear some wonderful tunes to accompony you during the viewing. You can change settings to get your desired look! You can also save these settings for future appretiation of the art pieces. <p className={'welcome-p'}>Welcome to this interactive gallery! In here you will find unique art pieces, consisting of a quote and SVG. If you turn up your volume you may hear some wonderful tunes to accompony you during the viewing. You can change settings to get your desired look! You can also save these settings for future appretiation of the art pieces.
</p> </p>
<div className={'navbutton left'}> <div className={'navbutton left'}>
<NavButton direction={"left"} changePage={this.props.changePage} /> <NavButton direction={"left"} />
</div> </div>
<div className={'navbutton right'}> <div className={'navbutton right'}>
<NavButton direction={"right"} changePage={this.props.changePage} /> <NavButton direction={"right"} />
</div> </div>
</div> </div>
); );
......
...@@ -4,44 +4,47 @@ import ExhibitionPiece from './ExhibitionPiece'; ...@@ -4,44 +4,47 @@ import ExhibitionPiece from './ExhibitionPiece';
import LandingPage from './LandingPage'; import LandingPage from './LandingPage';
import ArtPage from './ArtPage'; import ArtPage from './ArtPage';
type props = { type props = {}
}
type state = { type state = {
currentPage: number, currentPage: number,
totalPages: number totalPages: number
} }
export const PageContext = React.createContext((arg0: boolean) => {
});
class MainPage extends React.Component<props, state> { class MainPage extends React.Component<props, state> {
state = { currentPage: 1, totalPages: 11 }; // Index of which page to render. 0 is the landing page state = {currentPage: 1, totalPages: 11}; // Index of which page to render. 0 is the landing page
determineRender() { // Decides what page will be rendered determineRender() { // Decides what page will be rendered
return this.state.currentPage == 0 ? <LandingPage changePage={this.changePage}/> : return this.state.currentPage == 0 ? <LandingPage /> :
<ArtPage changePage={this.changePage} pageId={this.state.currentPage}/>; <ArtPage pageId={this.state.currentPage}/>;
} }
changePage = (increment: boolean) => { changePage = (increment: boolean) => {
const newPage = increment ? this.state.currentPage + 1 : this.state.currentPage - 1; const newPage = increment ? this.state.currentPage + 1 : this.state.currentPage - 1;
if (newPage == -1) if (newPage == -1)
this.setState({ currentPage: this.state.totalPages -1 }); // Negative modulo not supported this.setState({currentPage: this.state.totalPages - 1}); // Negative modulo not supported
else else
this.setState({ currentPage: newPage % this.state.totalPages }); // Go to the new page or loop around if you've hit last page this.setState({currentPage: newPage % this.state.totalPages}); // Go to the new page or loop around if you've hit last page
}; };
render() { render() {
return ( return (
<div className={'main-page'}> <div className={'main-page'}>
<div className={'header'}> <div className={'header'}>
Header Header
</div> </div>
<div className={'body'}> <div className={'body'}>
{this.determineRender()} <PageContext.Provider value={this.changePage}>
</div> {this.determineRender()}
</div> </PageContext.Provider>
); </div>
} </div>
);
}
} }
export default MainPage; export default MainPage;
\ No newline at end of file
import React from 'react'; import React, {useContext} from 'react';
import usePoetry from "./usePoetry";
import {PageContext} from "./MainPage";
type props = { type props = {
changePage: (arg0: boolean) => void;
direction: string; direction: string;
} }
const NavButton = (props: props) => { const NavButton = (props: props) => {
const changePage = useContext(PageContext);
const onClick = () => { const onClick = () => {
const increment: boolean = props.direction == "right"; const increment: boolean = props.direction == "right";
props.changePage(increment); changePage(increment);
} };
return ( return (
<button onClick={onClick}>{props.direction}</button> <button onClick={onClick}>{props.direction}</button>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment