Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetching data for admin page using redux #125

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
113 changes: 75 additions & 38 deletions src/components/Admin/Categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,77 @@ import React, { Component, Fragment } from 'react';
import "antd/dist/antd.css";
import { connect } from "react-redux";
import { Button,Modal,Input,Table, Divider } from 'antd';

const data = [
{
key: '1',
Name: 'Mechanic',
number: 32,
},
{
key: '2',
Name: 'Plumber',
number: 42,
},
{
key: '3',
Name: 'Electrician',
number: 32,
},
];
import {fetchCategory,deleteCategory,addCategory,updateCategory} from "../../redux/actions/adminActions";

class Categories extends Component {

state = { visible: false }
state = {
visibleForm: false,
newCategoryName:'',
visibleUpdate:false,
updatedCategoryName:'',
updatedCatId:'',
}

showModal = () => {
showModalForm = () => {
this.setState({
visible: true,
visibleForm: true,
});
}

handleOk = (e) => {
showModalUpdate = (e) => {
this.setState({
visible: false,
visibleUpdate: true,
updatedCatId:e
});
}

handleChangeForm=(e)=>{
this.setState({
newCategoryName:e.target.value
})
}

handleChangeUpdate=(e)=>{
this.setState({
updatedCategoryName:e.target.value
})
}

handleOkForm = () => {
this.props.addCategory(this.state.newCategoryName);
this.setState({
visibleForm: false,
newCategoryName:'' });
}

handleOkUpdate = () => {
this.props.updateCategory(this.state.updatedCategoryName,this.state.updatedCatId);
this.setState({
visibleUpdate: false,
updatedCategoryName:'',
updatedCatId:''
});
}

handleCancel = (e) => {
this.setState({
visible: false,
visibleForm: false,
newCategoryName:'',
visibleUpdate:false,
updatedCategoryName:'',
updatedCatId:''
});
}

handleDelete=(e)=>{
this.props.deleteCategory(e);
}

componentDidMount(){
this.props.fetchCategory();
}

render() {
const { isAuthenticated, user } = this.props;
const { Column } = Table;
Expand All @@ -51,29 +81,35 @@ class Categories extends Component {
<Fragment>
Categories
<br />
<Button type="primary" onClick={this.showModal}>
<Button type="primary" onClick={this.showModalForm}>
Add Category
</Button>
<Modal
title="Add a new category"
visible={this.state.visible}
onOk={this.handleOk}
visible={this.state.visibleForm}
onOk={this.handleOkForm}
onCancel={this.handleCancel}
>
<Input placeholder="Enter the name of the category" />
<Input onChange={(value)=>{this.handleChangeForm(value)}} placeholder="Enter the name of the category" />
</Modal>
<Table dataSource={data}>
<Column title="Category Name" dataIndex="Name" key="Name" />
<Column title="Number" dataIndex="number" key="number" />

<Table dataSource={this.props.cat} rowKey='_id'>
<Column title="Category Name" dataIndex="name" key="name" />
<Column
title="Action"
key="action"
render={(text, record) => (
key="_id"
render={(record) => (
<span>
<a href="/">Update</a>
<span onClick={() => this.showModalUpdate(record._id)}>Update</span>
<Modal
title="Add a new category"
visible={this.state.visibleUpdate}
onOk={this.handleOkUpdate}
onCancel={this.handleCancel}
>
<Input onChange={(value)=>{this.handleChangeUpdate(value)}} placeholder="Enter the name of the category" />
</Modal>
<Divider type="vertical" />
<a href="/">Delete</a>
<span onClick={()=>{this.handleDelete(record._id)}}>Delete</span>
</span>
)}
/>
Expand All @@ -91,10 +127,11 @@ class Categories extends Component {

const mapStateToProps = state => ({
isAuthenticated: state.auth.isAuthenticated,
user: state.auth.user
user: state.auth.user,
cat:state.admin.category,
});

export default connect(
mapStateToProps,
{}
{fetchCategory,deleteCategory,addCategory,updateCategory}
)(Categories);
57 changes: 57 additions & 0 deletions src/redux/actions/adminActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {FETCH_CATEGORY,DELETE_CATEGORY,ADD_CATEGORY,UPDATE_CATEGORY} from './type';
import axios from 'axios';
import {url} from '../../helper/url';
import { tokenConfig } from './authActions';

export const fetchCategory =()=>{
return dispatch =>{
axios.get(`${url}/categories`)
.then(res=>{
dispatch({
type:FETCH_CATEGORY,
payload: res.data.categories,
})
})
}
};

export const deleteCategory =(e)=> (dispatch,getState) =>{
const config = tokenConfig(getState);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to create a new variable config. directly use tokenConfig(getState) in the axios call

axios.delete(`${url}/categories/`+e,config)
.then(res=>{
dispatch({
type:DELETE_CATEGORY,
payload: e,
})
})

};

export const addCategory =(name)=>(dispatch,getState)=>{
const config = tokenConfig(getState);
Subhang23 marked this conversation as resolved.
Show resolved Hide resolved
const body = JSON.stringify({ name });
axios.post(`${url}/categories`,body,config)
.then(res=>{
dispatch({
type:ADD_CATEGORY,
payload: res.data,
})
})

};

export const updateCategory =(name,id)=>(dispatch,getState) =>{
const config = tokenConfig(getState);
Subhang23 marked this conversation as resolved.
Show resolved Hide resolved
const body = JSON.stringify({ name });
axios.patch(`${url}/categories/`+id,body,config)
.then(res=>{
dispatch({
type:UPDATE_CATEGORY,
payload: id,
newCategory:res.data,
})
})

};


7 changes: 6 additions & 1 deletion src/redux/actions/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ export const GET_CATEGORY_WISE_SERVICES_PENDING = 'GET_CATEGORY_WISE_SERVICES_PE
export const GET_CATEGORY_WISE_SERVICES_SUCCESS = 'GET_CATEGORY_WISE_SERVICES_SUCCESS';

export const GET_VENDOR_SERVICES = 'GET_VENDOR_SERVICES';
export const GET_VENDOR_SERVICES_FAIL = 'GET_VENDOR_SERVICES_FAIL';
export const GET_VENDOR_SERVICES_FAIL = 'GET_VENDOR_SERVICES_FAIL';

export const FETCH_CATEGORY='FETCH_CATEGORY';
export const DELETE_CATEGORY='DELETE_CATEGORY';
export const ADD_CATEGORY='ADD_CATEGORY';
export const UPDATE_CATEGORY='UPDATE_CATEGORY';
37 changes: 37 additions & 0 deletions src/redux/reducers/adminReducers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {FETCH_CATEGORY,DELETE_CATEGORY,ADD_CATEGORY,UPDATE_CATEGORY} from '../actions/type';

const initState={
category:[],
}

const fetchCategory=(state=initState,action)=>{
switch(action.type){
case FETCH_CATEGORY:
return {
...state,
category:action.payload
}
case ADD_CATEGORY:
return {category :state.category.concat(action.payload)}
case DELETE_CATEGORY :
return { category: state.category.filter(catname =>
catname._id !== action.payload
)}
case UPDATE_CATEGORY:
return{
category :state.category.map(item=>{
if(item._id===action.payload)
return {
_id:action.payload,
name:action.newCategory.name,
register_date:action.newCategory.register_date
}
else
return item
})
}
default: return state;
}

}
export default fetchCategory;
4 changes: 3 additions & 1 deletion src/redux/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import AuthReducer from './authReducer';
import ErrorReducer from './errorReducer';
import UserReducer from './userReducer';
import CategoryServiceReducer from './categoryServiceReducer';
import AdminReducer from './adminReducers';


export default combineReducers({
auth: AuthReducer,
error: ErrorReducer,
user: UserReducer,
vendor: vendorReducer,
categoryService: CategoryServiceReducer
categoryService: CategoryServiceReducer,
admin:AdminReducer,
});