Skip to content

Commit

Permalink
Merge pull request #144 from elluga/feat-dfp-url-change
Browse files Browse the repository at this point in the history
completed dfp url change in infinite scroll closes #22
  • Loading branch information
oyilmaztekin authored Apr 11, 2019
2 parents 24d17d7 + 5bc4cbe commit ff8dd08
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 19 deletions.
33 changes: 27 additions & 6 deletions nested/pagedetaillayout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import { Block } from "@comp/layouts";
import Article from "@comp/article/";
import Breadcrumb from "@comp/breadcrumb";
import propTypes from "prop-types";
import { StoreConsumerHOC, InfiniteProvider } from "@utils";
import {
StoreConsumerHOC,
InfiniteConsumerHOC
} from "@utils";
import {
DFPSlotsProvider,
AdSlot
AdSlot,
DFPManager
} from "react-dfp";
import CardManager from "@comp/cardmanager/";
import Infinite from "@comp/infinite";
Expand All @@ -20,11 +24,23 @@ class PageDetailLayout extends Component {
super(props);
}

UNSAFE_componentWillReceiveProps(nextProps) {
if (
nextProps.context.state.activeURL !==
this.props.context.state.activeURL
) {
DFPManager.refresh();
}
}

render() {
const {
content,
store: {
state: { adNetworkID }
},
context: {
state: { activeURL }
}
} = this.props;
const {
Expand All @@ -48,6 +64,7 @@ class PageDetailLayout extends Component {
url: path
}
};

return (
<DFPSlotsProvider
dfpNetworkId={adNetworkID}
Expand Down Expand Up @@ -108,9 +125,7 @@ class PageDetailLayout extends Component {
slotId="HaberDetay_970x250"
/>
</Article>
<InfiniteProvider>
<Infinite dataset="cat-gundem" />
</InfiniteProvider>
<Infinite dataset="cat-gundem" />
</Block>
</Container>
</Block>
Expand All @@ -121,7 +136,13 @@ class PageDetailLayout extends Component {

PageDetailLayout.propTypes = {
content: propTypes.object,
store: propTypes.object
store: propTypes.object,
context: propTypes.object
};

// eslint-disable-next-line
PageDetailLayout = InfiniteConsumerHOC(
PageDetailLayout
);

export default StoreConsumerHOC(PageDetailLayout);
12 changes: 10 additions & 2 deletions nested/pagelayout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Icon from "@comp/icon/";
import { faArrowAltCircleUp } from "@fortawesome/free-solid-svg-icons";
import { StoreProvider } from "@utils";
import { Block } from "@comp/layouts";
import { InfiniteProvider } from "../../utils/providers/infinite";

const PageLayout = props => {
return (
Expand All @@ -23,7 +24,13 @@ const PageLayout = props => {
type="section"
className="content-section"
>
{props.children}
{props.infinite ? (
<InfiniteProvider>
{props.children}
</InfiniteProvider>
) : (
props.children
)}
</Block>
<Footer />

Expand All @@ -40,7 +47,8 @@ const PageLayout = props => {
};

PageLayout.propTypes = {
children: propTypes.node.isRequired
children: propTypes.node.isRequired,
infinite: propTypes.bool
};

export default PageLayout;
2 changes: 1 addition & 1 deletion pages/newsdetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class NewsDetail extends Component {
<title>{title}</title>
</Head>

<PageLayout>
<PageLayout infinite={true}>
<PageDetailLayout content={data} />
</PageLayout>
</>
Expand Down
53 changes: 43 additions & 10 deletions utils/providers/infinite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export class InfiniteProvider extends Component {
super(props);
this.state = {
activeURL: "",
ssrURL: "",
nodesHash: {}
};
}
Expand All @@ -25,19 +26,51 @@ export class InfiniteProvider extends Component {
if (this.state.nodesHash) {
window.addEventListener(
"scroll",
this.scrollListener,
true
this.scrollListener.bind(this),
false
);
}
}

scrollListener(e) {
e.preventDefault();
manipulateURL(url, title) {
document.title = title;
window.history.pushState(null, title, url);
}

@autobind
changeURL(url) {
this.updateValue("activeURL", url);
scrollListener(e) {
e.preventDefault();
if (this.state && this.state.nodesHash) {
let pageOffset = window.pageYOffset;
let nodes = this.state.nodesHash;
Object.entries(nodes).forEach(item => {
let offsetTop = item[1].offsetTop;
let height = item[1].height;
const { activeURL } = this.state;
if (
offsetTop &&
height &&
pageOffset >= offsetTop &&
pageOffset <= offsetTop + height
) {
if (
activeURL &&
activeURL === item[1].url
) {
return;
}
this.manipulateURL(
document.location.origin +
item[1].url,
item[1].title
);
this.updateValue(
"activeURL",
item[1].url
);

}
});
}
}

@autobind
Expand All @@ -53,7 +86,8 @@ export class InfiniteProvider extends Component {
elObj.offsetTop = elTop;
elObj.id = item._id;
elObj.url = item.url;
newNodesHash[`${elHeight}_${elTop}`] = elObj;
elObj.title = item.title;
newNodesHash[elObj.id] = elObj;

this.updateValue("nodesHash", newNodesHash);
}
Expand All @@ -63,8 +97,7 @@ export class InfiniteProvider extends Component {
<InfiniteContext.Provider
value={{
state: this.state,
createNodeHash: this.createNodeHash,
changeURL: this.changeURL
createNodeHash: this.createNodeHash
}}
>
{this.props.children}
Expand Down

0 comments on commit ff8dd08

Please sign in to comment.