Skip to content

Commit

Permalink
Merge pull request #30 from andela/ft-update-article-169621164
Browse files Browse the repository at this point in the history
#169621164 Enable user update their article
  • Loading branch information
bdushimi committed Nov 11, 2019
2 parents e49d71b + 1a48ab8 commit 36b9c76
Show file tree
Hide file tree
Showing 14 changed files with 523 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/app/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Profile from '../feature/profile/view_profile/ViewProfileComponent';
import UpdateProfile from '../feature/profile/update_profile/UpdateProfileComponent';
import ForgotPassword from '../feature/Reset Password/forgot password/ForgotPasswordComponent';
import ResetPassword from '../feature/Reset Password/reset password/ResetPasswordComponent';
import UpdateArticle from '../feature/articles/updateArticle/UpdateArticleComponent';
import '../../node_modules/font-awesome/css/font-awesome.min.css';

toast.configure();
Expand All @@ -39,6 +40,7 @@ function App() {
<Route path="/update-profile" component={UpdateProfile} />
<Route path="/articles/:slug" component={GetSingleArticle} />
<ProtectedRoutes path="/create" component={CreateArticle} />
<ProtectedRoutes path="/article/:slug/update" component={UpdateArticle} />
</section>
</Switch>
</BrowserRouter>
Expand Down
5 changes: 4 additions & 1 deletion src/app/common/articleMenu/ArticleDropdownMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ export class ArticleDropdownMenu extends Component {
};

render() {
const { slug } = this.props;
return (
<div className="main-wrapper">
<ul className="dropdown__menu">
<li>Update</li>
<a href={`/article/${slug}/update`}>
<li>Update</li>
</a>
<li onClick={this.checkUser}>Delete</li>
</ul>
</div>
Expand Down
5 changes: 5 additions & 0 deletions src/app/common/articleMenu/ArticleDropdownMenu.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
list-style-type: none;
padding: 0;
margin: 0;
a {
text-decoration: none;
color: black;
font-size: 14px;
}

li {
text-transform: capitalize;
Expand Down
2 changes: 2 additions & 0 deletions src/feature/articles/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ export const DISLIKE_ARTICLE_SUCCESS = 'DISLIKE_ARTICLE_SUCCESS';
export const LOADING = 'LOADING';
export const DELETE_ARTICLE_SUCCESS = 'DELETE_ARTICLE_SUCCESS';
export const DELETE_ARTICLE_FAIL = 'DELETE_ARTICLE_FAIL';
export const UPDATE_ARTICLE_SUCCESS = 'UPDATE_ARTICLE_SUCCESS';
export const UPDATE_ARTICLE_FAIL = 'UPDATE_ARTICLE_FAIL';
15 changes: 6 additions & 9 deletions src/feature/articles/createArticle/createArticleAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@ const createArticle = (
{ title, description, tags, category, body },
props
) => async dispatch => {
let formData = {};
formData.tags = tags || '';
formData.category = category || '';

formData = {
title,
description,
body
};
const formData = {};
formData.tags = tags || ' ';
formData.category = category || ' ';
formData.title = title;
formData.description = description;
formData.body = body;

try {
const res = await axios.post(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,4 @@
text-align: center;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ export class ViewSingleArticle extends Component {
.fromNow()}
</span>
{' '}
<span className="heading__munite">{readTime}.</span>
<span className="heading__munite">
{readTime}
.
</span>
<span>
<div className="heading__avarageRating">
<AverageRating avarageRatings={averageRatings} />
Expand Down
15 changes: 13 additions & 2 deletions src/feature/articles/getSingleArticle/GetSingleArticleReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import {
GET_SINGLE_ARTICLE_SUCCESS,
GET_SINGLE_ARTICLE_FAIL,
DELETE_ARTICLE_SUCCESS,
DELETE_ARTICLE_FAIL
DELETE_ARTICLE_FAIL,
UPDATE_ARTICLE_SUCCESS,
UPDATE_ARTICLE_FAIL
} from '../constants';

export const initialState = {
article: {},
deleted: false
deleted: false,
updated: false
};

export default (state = initialState, action) => {
Expand All @@ -29,12 +32,20 @@ export default (state = initialState, action) => {
...state,
deleted: true
};
case UPDATE_ARTICLE_SUCCESS:
return {
...state,
...payload,
updated: true
};
case LIKE_ARTICLE_FAIL:
case DISLIKE_ARTICLE_FAIL:
case GET_SINGLE_ARTICLE_FAIL:
return { ...state, ...payload };
case DELETE_ARTICLE_FAIL:
return { ...state, deleted: false };
case UPDATE_ARTICLE_FAIL:
return { ...state, ...payload, updated: false };
default:
return {
...state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ describe('Get Single Article Components tests', () => {
expect(wrapper.find('img').length).toBe(3);
expect(window.scrollTo).toBeCalledWith(0, 0);
});
it('should test eventListerner on menu display', () => {
const wrapper = renderViewSingleArticle();
const button = wrapper.find('.heading__menu');
button.simulate('click', {});
document.event({
name: 'click',
target: {
parentNode: { classList: { contains: jest.fn(() => false) } },
classList: { contains: jest.fn(() => false) }
}
});
expect(wrapper).toHaveLength(1);
});
});

describe('Reload after Delete article success ', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import {
GET_SINGLE_ARTICLE_SUCCESS,
GET_SINGLE_ARTICLE_FAIL,
DELETE_ARTICLE_SUCCESS,
DELETE_ARTICLE_FAIL
DELETE_ARTICLE_FAIL,
UPDATE_ARTICLE_SUCCESS,
UPDATE_ARTICLE_FAIL,
DISLIKE_ARTICLE_SUCCESS,
LIKE_ARTICLE_SUCCESS,
DISLIKE_ARTICLE_FAIL,
LIKE_ARTICLE_FAIL
} from '../../constants';
import GetSingleArticleReducer, {
initialState
Expand All @@ -20,6 +26,35 @@ describe('Get Single Article reducer', () => {
});
expect(reducer.payload).toEqual(payload);
});
it('Should test LIKE_ARTICLE_SUCCESS', () => {
const reducer = GetSingleArticleReducer(initialState, {
type: LIKE_ARTICLE_SUCCESS,
payload: {
payload
}
});
expect(reducer.payload).toEqual(payload);
});
it('Should test DISLIKE_ARTICLE_SUCCESS', () => {
const reducer = GetSingleArticleReducer(initialState, {
type: DISLIKE_ARTICLE_SUCCESS,
payload: {
payload
}
});
expect(reducer.payload).toEqual(payload);
});

it('Should test UPDATE_ARTICLE_SUCCESS', () => {
const reducer = GetSingleArticleReducer(initialState, {
type: UPDATE_ARTICLE_SUCCESS,
payload: {
payload,
updated: true
}
});
expect(reducer.payload).toEqual(payload);
});

it('Should test GET_SINGLE_ARTICLE_FAIL', () => {
const reducer = GetSingleArticleReducer(initialState, {
Expand All @@ -30,6 +65,42 @@ describe('Get Single Article reducer', () => {
});
expect(reducer.error).toEqual('No articles found at the moment');
});
it('Should test UPDATE_ARTICLE_FAIL', () => {
const reducer = GetSingleArticleReducer(initialState, {
type: UPDATE_ARTICLE_FAIL,
payload: {
error: 'could not update the article'
}
});
expect(reducer.error).toEqual('could not update the article');
});
it('Should test UPDATE_ARTICLE_FAIL', () => {
const reducer = GetSingleArticleReducer(initialState, {
type: UPDATE_ARTICLE_FAIL,
payload: {
error: 'could not update the article'
}
});
expect(reducer.error).toEqual('could not update the article');
});
it('Should test LIKE_ARTICLE_FAIL', () => {
const reducer = GetSingleArticleReducer(initialState, {
type: LIKE_ARTICLE_FAIL,
payload: {
error: 'could not like the article'
}
});
expect(reducer.error).toEqual('could not like the article');
});
it('Should test DISLIKE_ARTICLE_FAIL', () => {
const reducer = GetSingleArticleReducer(initialState, {
type: DISLIKE_ARTICLE_FAIL,
payload: {
error: 'could not dislike the article'
}
});
expect(reducer.error).toEqual('could not dislike the article');
});
});

describe('Delete Article tests', () => {
Expand Down
41 changes: 41 additions & 0 deletions src/feature/articles/updateArticle/UpdateArticleAction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { config } from 'dotenv';
import axios from 'axios';
import { toast } from 'react-toastify';
import { UPDATE_ARTICLE_SUCCESS, UPDATE_ARTICLE_FAIL } from '../constants';
import { BACKEND_URL } from '../../../app/common/config/appConfig';
import setAxiosConfig from '../../../app/common/config/axiosConfig';

config();

const updateArticle = (
{
title, description, tags, category, body
},
slug
) => async dispatch => {
const formData = {};
formData.tags = tags || ' ';
formData.category = category || ' ';
formData.title = title;
formData.description = description;
formData.body = body;

try {
const res = await axios.put(
`${BACKEND_URL}/articles/${slug}`,
formData,
setAxiosConfig()
);
dispatch({
type: UPDATE_ARTICLE_SUCCESS,
payload: res.data
});
} catch (err) {
const error = (await err.response)
? err.response.data.error
: 'SERVER ERROR! Please contact the administartor';
dispatch({ type: UPDATE_ARTICLE_FAIL, payload: error });
toast.error(error, { position: toast.POSITION.TOP_CENTER });
}
};
export default updateArticle;
Loading

0 comments on commit 36b9c76

Please sign in to comment.