-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from andela/ft-create-articles-164047027
164047027 Users should be able to create articles
- Loading branch information
Showing
42 changed files
with
1,806 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,6 @@ yarn-error.log* | |
|
||
#idea | ||
.idea/ | ||
.css | ||
|
||
yarn.lock | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,25 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<link rel="shortcut icon" href="%PUBLIC_URL%/logo.png" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /> | ||
<meta name="theme-color" content="#000000" /> | ||
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" /> | ||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" | ||
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous" /> | ||
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" | ||
<head> | ||
<meta charset="utf-8" /> | ||
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /> | ||
<meta name="theme-color" content="#000000" /> | ||
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" | ||
integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous"> | ||
<title>Authors Haven</title> | ||
</head> | ||
|
||
<body> | ||
<noscript>You need to enable JavaScript to run this app.</noscript> | ||
<div class="root-content" id="root"></div> | ||
|
||
</body> | ||
|
||
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" /> | ||
<title>Authors Haven</title> | ||
</head> | ||
<body> | ||
<noscript>You need to enable JavaScript to run this app.</noscript> | ||
<div id="root" class="root-content"></div> | ||
<!-- | ||
This HTML file is a template. | ||
If you open it directly in the browser, you will see an empty page. | ||
You can add webfonts, meta tags, or analytics to this file. | ||
The build step will place the bundled scripts into the <body> tag. | ||
To begin the development, run `npm start` or `yarn start`. | ||
To create a production bundle, use `npm run build` or `yarn build`. | ||
--> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.App-header { | ||
background-color: #F6FAF9; | ||
min-height: 100vh; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: center; | ||
font-size: calc(10px + 2vmin); | ||
color: black; } | ||
.App-header label { | ||
color: black; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
.App-header { | ||
background-color: #F6FAF9; | ||
min-height: 100vh; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: center; | ||
font-size: calc(10px + 2vmin); | ||
color: black; | ||
label {color: black}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import axios from 'axios'; | ||
import { UPDATE_ARTICLE, ACTION_FAILED } from './types'; | ||
import axiosHeader from '../axios_config'; | ||
|
||
const apiURL2 = 'https://aholympian.herokuapp.com/api/articles/'; | ||
|
||
export const editArticle = (slug, postData) => async (dispatch) => { | ||
await axios | ||
.put(`${apiURL2}${slug}`, postData, axiosHeader) | ||
.then((result) => { | ||
dispatch({ type: UPDATE_ARTICLE, payload: result.data }); | ||
}) | ||
.catch((error) => { | ||
dispatch({ | ||
type: ACTION_FAILED, | ||
payload: error, | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import axios from 'axios'; | ||
import axiosHeader from '../axios_config'; | ||
import { FETCH_ARTICLES, ACTION_FAILED } from './types'; | ||
|
||
const apiURL = 'https://aholympian.herokuapp.com/api/'; | ||
|
||
export const getArticles = () => async (dispatch) => { | ||
await axios | ||
.get(`${apiURL}articles`, axiosHeader) | ||
.then((result) => { | ||
dispatch({ type: FETCH_ARTICLES, payload: result.data }); | ||
}) | ||
.catch((error) => { | ||
dispatch({ | ||
type: ACTION_FAILED, | ||
payload: error, | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import axios from 'axios'; | ||
import { FETCH_ARTICLE, NEW_ARTICLE, ACTION_FAILED } from './types'; | ||
import axiosHeader from '../axios_config'; | ||
|
||
const apiURL2 = 'https://aholympian.herokuapp.com/api/articles/'; | ||
|
||
export const createArticle = postData => async (dispatch) => { | ||
await axios | ||
.post(`${apiURL2}`, postData, axiosHeader) | ||
.then((result) => { | ||
dispatch({ type: NEW_ARTICLE, payload: result.data }); | ||
}) | ||
.catch((error) => { | ||
dispatch({ | ||
type: ACTION_FAILED, | ||
payload: error, | ||
}); | ||
}); | ||
}; | ||
|
||
export const fetchArticle = slug => async (dispatch) => { | ||
await axios | ||
.get(`${apiURL2}${slug}`, axiosHeader) | ||
.then((result) => { | ||
dispatch({ type: FETCH_ARTICLE, payload: result.data }); | ||
}) | ||
.catch((error) => { | ||
dispatch({ | ||
type: ACTION_FAILED, | ||
payload: error, | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import configureMockStore from 'redux-mock-store'; | ||
import thunk from 'redux-thunk'; | ||
import mockAxios from 'axios'; | ||
|
||
import { UPDATE_ARTICLE, ACTION_FAILED } from '../types'; | ||
import { editArticle } from '../editArticle'; | ||
|
||
|
||
describe('testing update article', () => { | ||
it('tests editing an existing article', () => { | ||
const testStore = configureMockStore([thunk]); | ||
const store = testStore({}); | ||
mockAxios.put.mockResolvedValue({ | ||
data: {}, | ||
}); | ||
|
||
const expectedAction = [ | ||
{ | ||
type: UPDATE_ARTICLE, | ||
payload: {}, | ||
}, | ||
]; | ||
|
||
return store.dispatch(editArticle({})).then(() => { | ||
expect(store.getActions()).toEqual(expectedAction); | ||
}); | ||
}); | ||
it("dispatches UPDATE_ARTICLE action and returns an error", async () => { | ||
const testStore = configureMockStore([thunk]); | ||
const store = testStore({}); | ||
mockAxios.put.mockImplementationOnce(() => | ||
Promise.reject({ | ||
error: "Something bad happened" | ||
}) | ||
); | ||
|
||
try { | ||
await store.dispatch(editArticle()); | ||
} catch { | ||
const actions = store.getActions(); | ||
|
||
expect.assertions(3); | ||
expect(actions[0].type).toEqual("UPDATE_ARTICLE"); | ||
expect(actions[1].type).toEqual("ACTION_FAILED"); | ||
expect(actions[1].payload.error).toEqual("Something bad happened"); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import configureMockStore from 'redux-mock-store'; | ||
import thunk from 'redux-thunk'; | ||
import moxios from 'moxios'; | ||
|
||
import { FETCH_ARTICLES } from '../types'; | ||
import { getArticles } from '../getArticles'; | ||
|
||
const middlewares = [thunk]; | ||
const mockStore = configureMockStore(middlewares); | ||
|
||
describe('getArticles actions', () => { | ||
it('creates FETCH_ARTICLES after successfuly fetching articles', () => { | ||
moxios.wait(() => { | ||
const request = moxios.requests.mostRecent(); | ||
request.respondWith({ | ||
status: 200, | ||
response: {}, | ||
}); | ||
}); | ||
|
||
const expectedActions = [{ | ||
type: FETCH_ARTICLES, | ||
payload: {}, | ||
}, | ||
]; | ||
|
||
const store = mockStore({ payload: {} }); | ||
|
||
return store.dispatch(getArticles()).then(() => { | ||
// return of async actions | ||
expect(store.getActions()).toEqual(expectedActions); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import configureMockStore from 'redux-mock-store'; | ||
import thunk from 'redux-thunk'; | ||
import mockAxios from 'axios'; | ||
|
||
import { NEW_ARTICLE, FETCH_ARTICLE } from '../types'; | ||
import { createArticle, fetchArticle } from '../postArticles'; | ||
|
||
jest.mock('axios'); | ||
|
||
describe('testing new article', () => { | ||
it('tests posting a new article', () => { | ||
const testStore = configureMockStore([thunk]); | ||
const store = testStore({}); | ||
mockAxios.post.mockResolvedValue({ | ||
data: {}, | ||
}); | ||
|
||
const expectedAction = [ | ||
{ | ||
type: NEW_ARTICLE, | ||
payload: {}, | ||
}, | ||
]; | ||
|
||
return store.dispatch(createArticle({})).then(() => { | ||
expect(store.getActions()).toEqual(expectedAction); | ||
}); | ||
}); | ||
|
||
it('tests fetching a single article', () => { | ||
const testStore = configureMockStore([thunk]); | ||
const store = testStore({}); | ||
|
||
mockAxios.get.mockResolvedValue({ | ||
response: { | ||
data: {}, | ||
}, | ||
}); | ||
|
||
const expectedAction = [{ | ||
type: FETCH_ARTICLE, | ||
}]; | ||
return store.dispatch(fetchArticle({})).then(() => { | ||
expect(store.getActions()).toEqual(expectedAction); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,22 @@ | ||
import axios from 'axios'; | ||
|
||
let user = JSON.parse(localStorage.getItem('user')); | ||
|
||
let userToken; | ||
try { | ||
const user = JSON.parse(localStorage.getItem('user')); | ||
user = JSON.parse(localStorage.getItem('user')); | ||
const { token } = user.user; | ||
userToken = token; | ||
} catch (err) { | ||
userToken = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MTQsImV4cCI6MTU2MDc5MDQwMX0.IiKq2Fzbuhu424D3xBQrdSkZKzVx46bkALCgE4B4NaA'; | ||
const error = err.message; | ||
} | ||
|
||
|
||
const axiosHeader = { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Token ${userToken}`, | ||
}, | ||
}; | ||
|
||
export default axiosHeader; | ||
export default axiosHeader; |
Oops, something went wrong.