Skip to content

Commit b8f4c90

Browse files
committed
external 'goto' and event_callback
1 parent 33709d2 commit b8f4c90

File tree

5 files changed

+33
-13
lines changed

5 files changed

+33
-13
lines changed

public/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<div id="socket1"></div>
2424
<div id="socket2"></div>
2525
<div id="socket3"></div>
26+
<div style="background: red; height: 800px; width: 100px"></div>
2627
</main>
2728
</body>
2829
<script>

src/components/App.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ class App extends Component {
1414
error: props.error,
1515
enquiry_form_info: null,
1616
}
17-
this.url_base = props.config.router_mode === 'history' ? props.config.url_root : '/'
18-
this.url = this.url.bind(this)
17+
this.url = props.url_generator
1918
this.get_text = this.get_text.bind(this)
2019

2120
this.get_enquiry = this.get_enquiry.bind(this)
@@ -28,9 +27,6 @@ class App extends Component {
2827
}
2928
}
3029

31-
url (url_) {
32-
return this.url_base + url_
33-
}
3430

3531
get_text (name, replacements) {
3632
let s = this.props.config.messages[name]
@@ -49,7 +45,9 @@ class App extends Component {
4945

5046
async set_enquiry () {
5147
this.setState({enquiry_form_info: {}})
52-
this.setState({enquiry_form_info: await this.requests.get('enquiry')})
48+
const enquiry_form_info = await this.requests.get('enquiry')
49+
this.props.config.event_callback('get_enquiry_data', enquiry_form_info)
50+
this.setState({enquiry_form_info})
5351
}
5452

5553
ga_event (category, action, label) {

src/components/contractors/Contractors.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Contractors extends Component {
2222
async componentDidMount () {
2323
if (this.state.subjects.length === 0) {
2424
const subjects = await this.props.root.requests.get('subjects')
25+
this.props.config.event_callback('get_subjects', subjects)
2526
this.setState({subjects})
2627
}
2728

@@ -48,8 +49,10 @@ class Contractors extends Component {
4849

4950
this.setState({selected_subject})
5051
const sub_id = selected_subject && selected_subject.id
52+
const contractors = await this.props.root.requests.get('contractors', {subject: sub_id || null})
53+
this.props.config.event_callback('updated_contractors', contractors)
5154
this.setState({
52-
contractors: await this.props.root.requests.get('contractors', {subject: sub_id || null}),
55+
contractors,
5356
got_contractors: true
5457
})
5558
}
@@ -65,7 +68,9 @@ class Contractors extends Component {
6568

6669
async set_contractor_details (url, state_ref) {
6770
this.setState({[state_ref]: null})
68-
this.setState({[state_ref]: await this.props.root.requests.get(url)})
71+
const con_details = await this.props.root.requests.get(url)
72+
this.props.config.event_callback('get_contractor_details', con_details)
73+
this.setState({[state_ref]: con_details})
6974
}
7075

7176
render () {

src/components/shared/Modal.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Modal extends Component {
1010
}
1111
this.close = this.close.bind(this)
1212
}
13+
1314
componentWillMount () {
1415
this.body_overflow_before = document.body.style.overflow
1516
document.body.style.overflow = 'hidden'

src/index.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,28 @@ window.socket = function (public_key, config) {
122122

123123
console.debug('using config:', config)
124124

125-
const Router = config.router_mode === 'history' ? BrowserRouter : HashRouter
126-
127-
const v = ReactDOM.render(<Router><App error={error} public_key={public_key} config={config}/></Router>, el)
128-
// TODO provide a better object here?
125+
const url_base = config.router_mode === 'history' ? config.url_root : '/'
126+
const url_generator = (url_) => url_base + url_
129127

130128
window._tcs_grecaptcha_loaded = () => (
131129
document.dispatchEvent(new Event('_tcs_grecaptcha_loaded'))
132130
)
133-
return v
131+
132+
const Router = config.router_mode === 'history' ? BrowserRouter : HashRouter
133+
134+
const router = ReactDOM.render(<Router><App
135+
error={error}
136+
public_key={public_key}
137+
url_generator={url_generator}
138+
config={config}/></Router>, el)
139+
// for external use and compatibility with old socket
140+
return {
141+
goto: path => {
142+
if (path === 'enquiry-modal') {
143+
router.history.push(url_generator('enquiry'))
144+
} else {
145+
router.history.push(url_generator(path))
146+
}
147+
}
148+
}
134149
}

0 commit comments

Comments
 (0)