This repository has been archived by the owner on Apr 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (82 loc) · 2.39 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const redux = require('redux')
const thunkMiddleware = require('redux-thunk').default
const axios = require('axios')
const createStore = redux.createStore
const applyMiddleware = redux.applyMiddleware
const initialState = {
loading: false,
job_posting: [],
error_msg: ''
}
const FETCH_JOBS_REQUEST = 'FETCH_JOBS_REQUEST'
const FETCH_JOBS_SUCCESS = 'FETCH_JOBS_SUCCESS'
const FETCH_JOBS_FAILURE = 'FETCH_JOBS_FAILURE'
const fetchJobsRequest = () => {
return {
type: FETCH_JOBS_REQUEST
}
}
const fetchJobsSuccess = jobs => {
return {
type: FETCH_JOBS_SUCCESS,
success_data: jobs
}
}
const fetchJobsFailure = error => {
return {
type: FETCH_JOBS_FAILURE,
error_data: error
}
}
const fetchJobs = () => {
return function (dispatch) {
dispatch(fetchJobsRequest())
axios
.get('https://remotive.io/api/remote-jobs?limit=3')
.then(response => {
const all_jobs = response.data
const jobs = all_jobs.jobs.map(function (job) {
return {
job_id: job.id,
job_title: `${job.title} [${job.category}]`,
company_hiring: job.company_name,
job_type: job.job_type,
location: job.candidate_required_location,
application_link: job.url
}
})
dispatch(fetchJobsSuccess(jobs))
})
.catch(error => {
dispatch(fetchJobsFailure(error.message))
})
}
}
const reducer = (state = initialState, action) => {
console.log(action.type)
switch (action.type) {
case FETCH_JOBS_REQUEST:
return {
...state,
loading: true
}
case FETCH_JOBS_SUCCESS:
return {
...state,
loading: false,
job_posting: action.success_data,
}
case FETCH_JOBS_FAILURE:
return {
...state,
loading: false,
error_msg: action.error_data
}
}
}
const store = createStore(reducer, initialState, applyMiddleware(thunkMiddleware))
store.dispatch(fetchJobs())
store.subscribe(() => {
const state = store.getState()
console.log(state)
})