Skip to content

Commit 4e89a7b

Browse files
authored
Merge pull request #9 from bestprogrammingclub/view_pages
Added Rabbit Hole Path at the top of the page
2 parents 7a2ce3e + 61aa2cb commit 4e89a7b

File tree

1 file changed

+49
-35
lines changed

1 file changed

+49
-35
lines changed

src/App.js

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ const RabbitHolePage = withRouter(
8080
super(props);
8181
this.state = {
8282
wikiData: {},
83+
firstPageTitle: "",
8384
};
8485
}
8586

@@ -88,20 +89,7 @@ const RabbitHolePage = withRouter(
8889
if (this.props.location.search) {
8990
this.getMostRecentPage();
9091
} else {
91-
fetch(
92-
`https://en.wikipedia.org/api/rest_v1/page/random/mobile-sections`,
93-
{
94-
headers: {
95-
'Content-Type': 'application/json',
96-
},
97-
}
98-
)
99-
.then((resp) => resp.json())
100-
.then((resp) => {
101-
console.log('resp', resp);
102-
103-
this.setState({ wikiData: resp });
104-
});
92+
this.startNewRabbithole();
10593
}
10694
}
10795

@@ -125,20 +113,7 @@ const RabbitHolePage = withRouter(
125113
const wikiValueArray = wikiValue.split('|');
126114
const mostRecentPage = wikiValueArray[wikiValueArray.length - 1];
127115

128-
fetch(
129-
`https://en.wikipedia.org/api/rest_v1/page/mobile-sections/${mostRecentPage}`,
130-
{
131-
headers: {
132-
'Content-Type': 'application/json',
133-
},
134-
}
135-
)
136-
.then((resp) => resp.json())
137-
.then((resp) => {
138-
console.log('resp', resp);
139-
140-
this.setState({ wikiData: resp });
141-
});
116+
this.fetchPage(mostRecentPage);
142117
}
143118
}
144119

@@ -166,34 +141,73 @@ const RabbitHolePage = withRouter(
166141
// 3. /wiki/Mammal -> /#/?wiki=Pet_door|Dog|Mammal
167142
// TODO Handle if URL is too long, show message like
168143
// "you've been in the rabbit hole too long"
169-
`/#/?wiki=${wikiValue ? `${wikiValue}|` : ''}`
144+
`/#/?wiki=${wikiValue ? `${wikiValue}` : `${this.state.firstPageTitle}`}|`
170145
);
171146
}
172147

173-
startNewRabbithole() {
174-
console.log('TODO');
148+
fetchPage(pageTitle) {
175149
fetch(
176-
`https://en.wikipedia.org/api/rest_v1/page/random/mobile-sections`,
150+
`https://en.wikipedia.org/api/rest_v1/page/mobile-sections/${pageTitle}`,
177151
{
178152
headers: {
179153
'Content-Type': 'application/json',
180154
},
181155
}
182156
)
183157
.then((resp) => resp.json())
184-
.then((resp) => {
185-
console.log('resp', resp);
158+
.then((data) => {
159+
console.log('resp', data);
186160

187-
this.setState({ wikiData: resp });
161+
this.setState({ wikiData: data });
188162
});
189163
}
190164

165+
startNewRabbithole() {
166+
fetch(
167+
`https://en.wikipedia.org/api/rest_v1/page/random/title`,
168+
{
169+
headers: {
170+
'Content-Type': 'application/json',
171+
},
172+
}
173+
)
174+
.then((resp) => resp.json())
175+
.then((randomTitleData) => {
176+
const randomPageTitle = randomTitleData.items[0].title;
177+
console.log('TITLE FETCH resp', randomPageTitle);
178+
this.setState({ firstPageTitle: randomPageTitle });
179+
180+
this.fetchPage(randomPageTitle);
181+
182+
});
183+
}
184+
185+
186+
191187
render() {
188+
var rabbitHolePath = [];
189+
const searchParams = new URLSearchParams(this.props.location.search);
190+
const wikiValue = searchParams.get('wiki');
191+
192+
if (wikiValue) {
193+
rabbitHolePath = wikiValue.replace(/_/g, ' ').split('|');
194+
}
195+
console.log(rabbitHolePath);
196+
192197
return (
193198
<div>
194199
<a href="/#/" onClick={() => this.startNewRabbithole()}>
195200
START OVER
196201
</a>
202+
203+
<ul>
204+
{rabbitHolePath.map((pageTitle, index) => (
205+
<li key={index}>{pageTitle}</li>
206+
)
207+
)}
208+
</ul>
209+
210+
197211
{this.state.wikiData.lead &&
198212
this.state.wikiData.lead.sections &&
199213
this.state.wikiData.lead.sections.map((section) => (

0 commit comments

Comments
 (0)