Skip to content

Commit

Permalink
Merge pull request abalone0204#5 from abalone0204/feature/provider
Browse files Browse the repository at this point in the history
Feature/provider
  • Loading branch information
abalone0204 committed Jun 8, 2016
2 parents d016964 + b1e4fa0 commit 68095d8
Show file tree
Hide file tree
Showing 15 changed files with 250 additions and 49 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,8 @@
## Firfox Plugin

## Web


## Trouble

- fetch 不能在 header 裡面用中文
10 changes: 4 additions & 6 deletions front-end/app/API/fetchJob.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@ export default function fetchJob({
const {
backend
} = config

const options = {
method: 'GET',
headers: {
"X-Company-Name": company_name,
"X-Job-Name": job_name,
"X-E04-Job-No": e04_job_no
},
mode: 'cors'
}
return fetch(`${backend}/jobs`, options)

const url = `${backend}/jobs?company_name=${company_name}&job_name=${job_name}&e04_job_no=${e04_job_no}`
return fetch(url, options)
.then(checkStatus)
.then(parseJSON)
}
14 changes: 13 additions & 1 deletion front-end/app/containers/App.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import getProvider, {
getProviderName,
getJobQuery
} from '../providers'

import {
connect
} from 'react-redux'
Expand Down Expand Up @@ -84,6 +89,7 @@ class App extends React.Component {
const {
dispatch
} = this.props

chrome.storage.sync.get('access_token', (item) => {
console.log('item ==>', item);
if (!!item['access_token']) {
Expand All @@ -92,6 +98,12 @@ class App extends React.Component {
console.log('access token not found');
}
})

const provider = getProvider()
const query = getJobQuery(provider)
dispatch(requestFetchJob(query))


}

render() {
Expand All @@ -104,7 +116,7 @@ class App extends React.Component {
const {
access_token
} = user
console.log('job==>',job);
console.log('job==>', job);
return (
<div>
test app container
Expand Down
16 changes: 6 additions & 10 deletions front-end/app/containers/Root.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { Provider } from 'react-redux'
import App from './App'

class Root extends React.Component {
render() {
const {store} = this.props
return (
<Provider store={store}>
<App/>
</Provider>
)
}
}

const Root = ({store}) => (
<Provider store={store}>
<App/>
</Provider>
)

Root.propTypes = {
store: React.PropTypes.object.isRequired
Expand Down
8 changes: 6 additions & 2 deletions front-end/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import Root from './containers/Root'
import store from './store.js'


export default function main(containerId) {
ReactDOM.render(<Root store={store}/>, document.getElementById(containerId))
export default function main(containerId, {
job_name,
company_name
}) {
ReactDOM.render(<Root store={store} company_name={company_name}/>,
document.getElementById(containerId))
}
10 changes: 6 additions & 4 deletions front-end/app/providers/104.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const getJobTitile = () => document.querySelector('#job h1')
const get_job_name = () => document.querySelector('#job h1')
.firstChild
.textContent.trim()
const getCompanyName = () => document.querySelector('#job .company a')
const get_company_name = () => document.querySelector('#job .company a')
.firstChild
.textContent.trim()
const get_e04_job_no = () => location.search.match(/\?jobno=([^\&]+)/)[1]

const provider = {
getJobTitile,
getCompanyName
get_job_name,
get_company_name,
get_e04_job_no
}

export default provider
19 changes: 18 additions & 1 deletion front-end/app/providers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,21 @@ const rootProvider = {
["104"]: p104
}

export default rootProvider
export function getProviderName() {
const matcher = location.host.match(/([^w\.]+)\.com/)
if (matcher) {
return matcher[1]
}
}

export function getJobQuery(provider) {
const jobQuery = {}
Object.keys(provider).forEach(key => {
jobQuery[key.substring(4)] = provider[key]()
})
return jobQuery
}

export default function getProvider () {
return rootProvider[getProviderName()]
}
32 changes: 27 additions & 5 deletions front-end/app/sagas/fetchJob.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ import {
SUCCESS_FETCH_JOB
} from '../actions/fetchJob.js'

import {
REQUEST_CREATE_JOB
} from '../actions/createJob.js'

import {
REQUEST_FETCH_COMMENTS
} from '../actions/fetchComments.js'

import fetchJob from '../API/fetchJob.js'

export function* watchRequestFetchJob() {
Expand All @@ -21,15 +29,29 @@ export function* watchRequestFetchJob() {

export function* fetchJobFlow(action) {
try {

const job = yield call(fetchJob, action.query)
yield put({
type: SUCCESS_FETCH_JOB,
job
})
yield call(jobHandler, job, action)

} catch (error) {
yield put({
type: FAIL_TO_FETCH_JOB,
error
})
}
}
}

export function* jobHandler(job, action) {
if (job === null) {
yield put({
type: REQUEST_CREATE_JOB,
params: action.query
})
} else {
yield put({
type: SUCCESS_FETCH_JOB,
job
})
}
}

7 changes: 6 additions & 1 deletion front-end/app/sagas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ import {watchRequestLeaveComment} from './leaveComment.js'
import {watchRequestFetchComments} from './fetchComments.js'
import {watchRequestFetchJob} from './fetchJob.js'
import {watchRequestCreateJob} from './createJob.js'
import {
transactionOfJobAndComments
} from './transactionFlows.js'

export default function* rootSaga() {
yield [
watchRequestLogin(),
watchRequestLeaveComment(),
watchRequestFetchComments(),
watchRequestFetchJob(),
watchRequestCreateJob()
watchRequestCreateJob(),
transactionOfJobAndComments()
]
}
4 changes: 0 additions & 4 deletions front-end/app/sagas/leaveComment.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import {
import checkAccessToken from '../API/checkAccessToken.js'
import createComment from '../API/createComment.js'

import {

} from '../actions/fetchComments.js'

import {
REQUEST_LEAVE_COMMENT,
FAIL_TO_LEAVE_COMMENT,
Expand Down
28 changes: 28 additions & 0 deletions front-end/app/sagas/transactionFlows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
takeEvery
} from 'redux-saga'

import {
put,
call
} from 'redux-saga/effects'

import {
SUCCESS_CREATE_JOB
} from '../actions/createJob.js'

import {
SUCCESS_FETCH_JOB
} from '../actions/fetchJob.js'

import {
requestFetchComments
} from '../actions/fetchComments.js'

export function* transactionOfJobAndComments() {
yield call(takeEvery, [SUCCESS_CREATE_JOB, SUCCESS_FETCH_JOB], sendRequestToFetchComments)
}

export function* sendRequestToFetchComments(action) {
yield put(requestFetchComments(action.job.id))
}
24 changes: 17 additions & 7 deletions front-end/content.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import providers from './app/providers'
import getProvider, {
getProviderName,
getJobQuery
} from './app/providers'
import renderApp from './app'

console.log('load content script');

const jobTitle = providers['104'].getJobTitile()
const companyName = providers['104'].getCompanyName()
const providerName = getProviderName()
console.log('providerName:', providerName);
const provider = getProvider()
console.log('provider:', provider);
const jobObject = getJobQuery(provider)
console.log('jobObject:',jobObject);
const {job_name,company_name} = jobObject

const node = document.createElement('div')
const nodeId = `${jobTitle}Cla`
const nodeId = `${job_name}Cla`

console.log(jobTitle,companyName);
console.log(job_name, company_name);
node.id = nodeId
document.body.appendChild(node)

renderApp(nodeId)
renderApp(nodeId, {
job_name,
company_name
})
22 changes: 21 additions & 1 deletion front-end/dev.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
import Loading from './app/components/Loading'
import fetchJob from './app/API/fetchJob.js'
import createJob from './app/API/createJob.js'
const handler = (e) => {
const company_name = '中文'
const job_name = 'test'
const e04_job_no = 'e0444'

ReactDOM.render(<Loading/>, document.querySelector('#container'))
fetchJob({
company_name,
job_name,
e04_job_no
}).then(result => {
console.log('result=>',result);
})
}
const App = () => (
<div>
<Loading/>
<button onClick={handler}>fetch job</button>
</div>
)
ReactDOM.render(<App/>, document.querySelector('#container'))
Loading

0 comments on commit 68095d8

Please sign in to comment.