Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

433: Display event publisher in event preview #457

Merged
merged 6 commits into from
Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
},
"dependencies": {
"app-module-path": "^1.0.5",
"axios": "^0.19.0",
"babel-polyfill": "^6.23.0",
"babel-preset-stage-2": "^6.1.18",
"body-parser": "^1.14.1",
"bootstrap": "^4.1.3",
"classnames": "^2.2.6",
"codecov": "^3.0.0",
"connect-history-api-fallback": "^1.1.0",
"cookie-parser": "^1.4.0",
Expand Down
2 changes: 1 addition & 1 deletion src/actions/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function startFetchingEventDetails() {
}

export function fetchEventDetails(eventID, user = {}) {
let url = `${appSettings.api_base}/event/${eventID}/?include=keywords,location,audience,in_language,external_links,image`
let url = `${appSettings.api_base}/event/${eventID}/?include=keywords,location,audience,in_language,external_links`

if(appSettings.nocache) {
url += `&nocache=${Date.now()}`
Expand Down
134 changes: 134 additions & 0 deletions src/api/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import axios from 'axios'
import get from 'lodash/get'

import store from '../store'
import CONSTANTS from '../constants'

let authToken

const getToken = () => {
const state = store.getState()
return get(state, 'user.token')
}

store.subscribe(() => {
authToken = getToken()
})

export class ApiClient {
baseUrl

constructor(baseUrl) {
this.baseUrl = baseUrl
authToken = getToken()
}

getUrl = (endpoint) => {
// URLs in Django must have a trailing slash
const endsWithTrailingSlash = endpoint.substring(endpoint.length - 1) === '/'
return `${this.baseUrl}/${endpoint}${endsWithTrailingSlash ? '' : '/'}`
}

getHeaders = () => ({
...CONSTANTS.API_HEADERS,
...(authToken
? {Authorization: `JWT ${authToken}`}
: {}),
})

request = async ({
method,
endpoint,
data = {},
headers = {},
}) => {
const dataOrParams = ['GET', 'DELETE'].includes(method.toUpperCase()) ? 'params' : 'data'

return axios
.request({
method,
url: this.getUrl(endpoint),
headers: {
...this.getHeaders(),
...headers,
},
[dataOrParams]: data,
})
.then(response => ({
data: get(response, 'data'),
error: null,
}))
}

/**
* Make a GET request into the API.
* @param endpoint
* @param data
* @param config
* @returns {Promise}
*/
get = async (endpoint, data = {}, config = {}) => this.request({
method: 'GET',
endpoint,
data,
...config,
})

/**
* Make a POST request into the API.
* @param endpoint
* @param data
* @param config
* @returns {Promise}
*/
post = (endpoint, data = {}, config = {}) => this.request({
method: 'POST',
endpoint,
data,
...config,
})

/**
* Make a DELETE request into the API.
* @param endpoint
* @param data
* @param config
* @returns {Promise}
*/
delete = (endpoint, data = {}, config = {}) => this.request({
method: 'DELETE',
endpoint,
data,
...config,
})

/**
* Make a PUT request into the API.
* @param endpoint
* @param data
* @param config
* @returns {Promise}
*/
put = (endpoint, data = {}, config = {}) => this.request({
method: 'PUT',
endpoint,
data,
...config,
})

/**
* Make a PATCH request into the API.
* @param endpoint
* @param data
* @param config
* @returns {Promise}
*/
patch = (endpoint, data = {}, config = {}) => this.request({
method: 'PATCH',
endpoint,
data,
...config,
})
}

export default new ApiClient(appSettings.api_base)
Loading