Skip to content

Commit

Permalink
Tabs improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanhogan committed Jul 12, 2020
1 parent aac0464 commit 551f56f
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 25 deletions.
22 changes: 13 additions & 9 deletions src/components/page/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { withRouter } from 'react-router-dom'
import { fetchPage } from '../../lib/api'
import { stripTags } from '../../lib/html'
import { addTab } from '../../store/tabs'
import { storePage } from '../../store/pages'

import Loading from '../loading'
import Sections from '../sections'
Expand Down Expand Up @@ -34,18 +35,21 @@ class Page extends Component {
}

fetchPage (title) {
const { pages, storePage } = this.props
const pageIsCached = !!pages[title]

this.setState({ loading: true })

return fetchPage(title)
.then(({ title, content, sections }) => {
document.title = `${stripTags(title)} - Wikipadia`
return Promise.resolve()
.then(() => pageIsCached ? pages[title] : fetchPage(title))
.then(page => {
document.title = `${title} - Wikipadia`
storePage(title, page)

this.setState(
{
loading: false,
title,
content,
sections
...page
},
() => {
const { location: { hash } } = this.props
Expand Down Expand Up @@ -78,7 +82,7 @@ class Page extends Component {
const url = e.target.href.replace(window.location.origin, '')

if (tabs.length === 0) {
this.props.addTab(title, `/${encodeURIComponent(title.replace(/ /g, '_'))}`)
this.props.addTab(stripTags(title), `/${encodeURIComponent(stripTags(title).replace(/ /g, '_'))}`)
}

this.props.addTab(decodeURIComponent(url.split('/')[1].replace(/_/g, ' ')), url)
Expand Down Expand Up @@ -114,9 +118,9 @@ class Page extends Component {
}
}

const mapStateToProps = ({ tabs }) => ({ tabs })
const mapStateToProps = ({ pages, tabs }) => ({ pages, tabs })

export default compose(
connect(mapStateToProps, { addTab }),
connect(mapStateToProps, { addTab, storePage }),
withRouter
)(Page)
44 changes: 31 additions & 13 deletions src/components/tabs/index.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,51 @@
import React, { useState } from 'react'
import { compose } from 'redux'
import { connect } from 'react-redux'
import { withRouter } from 'react-router-dom'
import { removeTab } from '../../store/tabs'

import { Link } from 'react-router-dom'

const Tabs = ({ tabs, removeTab }) => {
const Tabs = ({ tabs, removeTab, history }) => {
if (tabs.length) {
const [active, setActive] = useState(tabs[0].id)

const handleClick = tab => e => {
window.scrollTo(0, 0)
return setActive(tab.id)
}

const handleRemove = tab => e => {
e.preventDefault()

const currentIndex = tabs.indexOf(tab)
const newTab = tabs[currentIndex + 1] || tabs[currentIndex - 1]

if (newTab) {
setActive(newTab.id)
window.scrollTo(0, 0)
history.push(newTab.path)
}

return removeTab(tab.id)
}

return (
<nav className='tabs'>
{tabs.map(tab => {
const handleClick = e => {
window.scrollTo(0, 0)
return setActive(tab.id)
}

const handleRemove = e => {
e.preventDefault()
return removeTab(tab.id)
}
const isActive = active === tab.id

return (
<div key={tab.id} className={['tab-link', isActive && 'tab-link-active'].filter(Boolean).join(' ')}>
<Link
to={tab.path}
onClick={handleClick}
onClick={handleClick(tab)}
>
{tab.name}
</Link>
<button className='remove-tab-link' onClick={handleRemove}>Remove tab</button>
{isActive && (
<button className='remove-tab-link' onClick={handleRemove(tab)}>Remove tab</button>
)}
</div>
)
})}
Expand All @@ -41,4 +56,7 @@ const Tabs = ({ tabs, removeTab }) => {
return null
}

export default connect(null, { removeTab })(Tabs)
export default compose(
connect(null, { removeTab }),
withRouter
)(Tabs)
2 changes: 2 additions & 0 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { combineReducers, createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk'
import createLogger from 'redux-logger'

import pages from './pages'
import settings from './settings'
import tabs from './tabs'

Expand All @@ -10,6 +11,7 @@ const initialState = {}
export default () =>
createStore(
combineReducers({
pages,
settings,
tabs
}),
Expand Down
22 changes: 22 additions & 0 deletions src/store/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

export const storePage = (title, data) => dispatch =>
dispatch({
type: 'PAGES/STORE',
payload: { title, data }
})

export default (state = {}, action) => {
switch (action.type) {
case 'PAGES/STORE': {
const { title, data } = action.payload

return {
...state,
[title]: data
}
}

default:
return state
}
}
5 changes: 5 additions & 0 deletions src/store/tabs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export default (state = [], action) => {

case 'TABS/REMOVE': {
const { id } = action.payload

if (state.length === 2) {
return []
}

return state.filter(tab => tab.id !== id)
}

Expand Down
6 changes: 5 additions & 1 deletion src/styles/_layout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
border-bottom: thin solid rgba(128, 128, 128, 0.5);
font-weight: bold;
text-align: center;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
flex: 1 1 auto;
Expand All @@ -99,6 +100,9 @@
text-decoration: none;
display: block;
padding: 0 1.5rem 0 1rem;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
}

Expand All @@ -116,7 +120,7 @@
width: 2rem;
height: 2rem;
margin-top: -1rem;
line-height: 1.8rem;
line-height: 2rem;
padding: 0;
text-indent: -9999px;
text-align: center;
Expand Down
8 changes: 6 additions & 2 deletions src/styles/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ table {
border-bottom: thin solid rgba(128, 128, 128, 0.5);
font-weight: bold;
text-align: center;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
flex: 1 1 auto;
Expand All @@ -157,7 +158,10 @@ table {
color: currentColor !important;
text-decoration: none;
display: block;
padding: 0 1.5rem 0 1rem; }
padding: 0 1.5rem 0 1rem;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden; }
.tabs .tab-link.tab-link-active {
border-bottom-color: transparent;
border-right-color: rgba(128, 128, 128, 0.25);
Expand All @@ -170,7 +174,7 @@ table {
width: 2rem;
height: 2rem;
margin-top: -1rem;
line-height: 1.8rem;
line-height: 2rem;
padding: 0;
text-indent: -9999px;
text-align: center;
Expand Down

0 comments on commit 551f56f

Please sign in to comment.