Skip to content

Commit b84dadc

Browse files
committed
adding pagination
1 parent 294bf7b commit b84dadc

File tree

6 files changed

+80
-14
lines changed

6 files changed

+80
-14
lines changed

public/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@
4040
<script>
4141
var public_key = '9c79f14df986a1ec693c' // '61e9d8d03109e44d7c67'
4242
var api_root = null // 'https://socket-beta.tutorcruncher.com' 'http://localhost:8000'
43-
window.socket = socket(public_key, {
43+
window.socket1 = socket(public_key, {
4444
element: '#socket1',
4545
router_mode: 'history',
4646
api_root: api_root,
47+
pagination: 8,
4748
})
4849
socket(public_key, {
4950
element: '#socket2',

src/components/contractors/Contractors.js

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { Component } from 'react'
2-
import {Route} from 'react-router-dom'
2+
import {Link, Route} from 'react-router-dom'
33
import {async_start, slugify} from '../../utils'
44
import {If} from '../shared/Tools'
55
import {Grid, List} from './List'
@@ -12,12 +12,16 @@ class Contractors extends Component {
1212
this.state = {
1313
contractors: [],
1414
got_contractors: false,
15+
page: 1,
16+
more_pages: false,
1517
subjects: [],
1618
selected_subject: null,
1719
}
1820
this.update_contractors = this.update_contractors.bind(this)
1921
this.get_contractor_details = this.get_contractor_details.bind(this)
2022
this.set_contractor_details = this.set_contractor_details.bind(this)
23+
this.subject_url = this.subject_url.bind(this)
24+
this.page_url = this.page_url.bind(this)
2125
this.subject_change = this.subject_change.bind(this)
2226
}
2327

@@ -31,12 +35,24 @@ class Contractors extends Component {
3135
await this.update_contractors()
3236
}
3337

34-
subject_change (selected_subject) {
38+
subject_url (selected_subject) {
3539
if (selected_subject) {
36-
this.props.history.push(this.props.root.url(`subject/${selected_subject.id}-${slugify(selected_subject.name)}`))
40+
return this.props.root.url(`subject/${selected_subject.id}-${slugify(selected_subject.name)}`)
3741
} else {
38-
this.props.history.push(this.props.root.url(''))
42+
return this.props.root.url('')
3943
}
44+
}
45+
46+
page_url (new_page) {
47+
let url = this.subject_url(this.state.selected_subject)
48+
if (new_page > 1) {
49+
url += `${url.substr(-1) === '/' ? '' : '/'}page/${new_page}`
50+
}
51+
return url
52+
}
53+
54+
subject_change (selected_subject) {
55+
this.props.history.push(this.subject_url(selected_subject))
4056
this.update_contractors(selected_subject)
4157
}
4258

@@ -49,14 +65,19 @@ class Contractors extends Component {
4965
}
5066
}
5167

52-
this.setState({selected_subject})
53-
const sub_id = selected_subject && selected_subject.id
54-
const args = Object.assign({}, this.props.config.contractor_filter, {subject: sub_id || null})
68+
const m = this.props.history.location.pathname.match(/page\/(\d+)/)
69+
const page = m ? parseInt(m[1], 10) : 1
70+
this.setState({selected_subject, page})
71+
const args = Object.assign({}, this.props.config.contractor_filter, {
72+
subject: selected_subject ? selected_subject.id : null,
73+
pagination: this.props.config.pagination,
74+
page: page,
75+
})
5576
const contractors = await this.props.root.requests.get('contractors', args)
56-
contractors.reverse()
5777
this.props.config.event_callback('updated_contractors', contractors)
5878
this.setState({
5979
contractors,
80+
more_pages: contractors.length === this.props.config.pagination,
6081
got_contractors: true
6182
})
6283
}
@@ -90,6 +111,22 @@ class Contractors extends Component {
90111
</If>
91112
<DisplayComponent contractors={this.state.contractors} root={this.props.root}/>
92113

114+
<If v={this.state.page > 1 || this.state.more_pages}>
115+
<div className="tcs-pagination">
116+
<Link
117+
to={this.page_url(this.state.page - 1)}
118+
onClick={() => setTimeout(() => this.update_contractors(), 0)}
119+
className={'tcs-previous' + (this.state.page > 1 ? '' : ' tcs-disable')}>
120+
&lsaquo;&lsaquo; {this.props.root.get_text('previous')}
121+
</Link>
122+
<Link
123+
to={this.page_url(this.state.page + 1)}
124+
onClick={() => setTimeout(() => this.update_contractors(), 0)}
125+
className={'tcs-next' + (this.state.more_pages ? '' : ' tcs-disable')}>
126+
{this.props.root.get_text('next')} &rsaquo;&rsaquo;
127+
</Link>
128+
</div>
129+
</If>
93130
<Route path={this.props.root.url(':id(\\d+):_extra')} render={props => (
94131
<ConModal id={props.match.params.id}
95132
contractors={this.state.contractors}

src/components/contractors/List.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react'
22
import {Link} from 'react-router-dom'
3-
import {Footer, Location, Markdown} from '../shared/Tools'
3+
import {Location, Markdown} from '../shared/Tools'
44
import Stars from './Stars'
55

66
export const Grid = ({contractors, root}) => (
@@ -20,7 +20,7 @@ export const List = ({contractors, root}) => (
2020
<div className="tcs-list">
2121
{contractors.map((contractor, i) => (
2222
<Link key={i} to={root.url(contractor.link)} className="tcs-item">
23-
<div className="tcs-image-col">
23+
<div className="tcs-image-col tcs-box">
2424
<img src={contractor.photo} alt={contractor.name} className="tcs-thumb"/>
2525
<button className="tcs-button">
2626
{root.get_text('view_profile')}
@@ -48,6 +48,5 @@ export const List = ({contractors, root}) => (
4848
</div>
4949
</Link>
5050
))}
51-
<Footer/>
5251
</div>
5352
)

src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const raven_config = {
2121
}
2222
Raven.config(process.env.REACT_APP_RAVEN_DSN, raven_config).install()
2323

24-
// TODO these need a consist prefix
2524
const STRINGS = {
2625
skills_label: 'Skills',
2726
contractor_enquiry: 'Please enter your details below to enquire about tutoring with {contractor_name}.',
@@ -40,6 +39,8 @@ const STRINGS = {
4039
subject_filter_summary_plural: '{subject}: showing {count} results',
4140
view_profile: 'View Profile',
4241
review_hours: '({hours} hours)',
42+
previous: 'Previous',
43+
next: 'Next',
4344
}
4445

4546
const MODES = ['grid', 'list', 'enquiry', 'enquiry-modal']
@@ -128,6 +129,7 @@ window.socket = async function (public_key, config) {
128129
return
129130
}
130131

132+
config.pagination = config.pagination || 100
131133
config.messages = config.messages || {}
132134
for (let k of Object.keys(STRINGS)) {
133135
if (!config.messages[k]) {
@@ -156,7 +158,7 @@ window.socket = async function (public_key, config) {
156158
console.debug('using config:', config)
157159

158160
const url_base = config.router_mode === 'history' ? config.url_root : '/'
159-
const url_generator = (url_) => url_base + url_
161+
const url_generator = (url_) => url_base + (url_ || '')
160162

161163
window._tcs_grecaptcha_loaded = () => (
162164
document.dispatchEvent(new Event('_tcs_grecaptcha_loaded'))

src/styles/contractors.scss

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,31 @@
4545
}
4646
}
4747

48+
$pagination-radius: 4px;
49+
.tcs-pagination {
50+
display: flex;
51+
justify-content: center;
52+
margin: 8px 0 0;
53+
.tcs-previous, .tcs-next {
54+
display: inline-block;
55+
padding: 8px;
56+
border: 1px solid #ccc;
57+
text-decoration: none !important;
58+
color: $brand-colour;
59+
}
60+
.tcs-previous {
61+
border-right: 0;
62+
border-radius: $pagination-radius 0 0 $pagination-radius;
63+
}
64+
.tcs-next {
65+
border-radius: 0 $pagination-radius $pagination-radius 0;
66+
}
67+
.tcs-disable {
68+
pointer-events: none;
69+
color: #888;
70+
}
71+
}
72+
4873
.tcs-aside {
4974
font-size: 20px;
5075
margin-bottom: 10px;

src/utils.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ export const to_markdown = t => {
1717
export const auto_url_root = path => {
1818
// remove :
1919
// * /subject/.*
20+
// * /page/.*
2021
// * contractor slug
2122
// * /enquiry
2223
path = path
2324
.replace(/\/subject\/\d+-[^/]+$/, '/')
25+
.replace(/\/page\/\d+-[^/]+$/, '/')
2426
.replace(/\/\d+-[^/]+$/, '/')
2527
.replace(/\/enquiry$/, '/')
2628
return path

0 commit comments

Comments
 (0)