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 from redux for admin categories #113

Closed
wants to merge 2 commits into from
Closed
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
34 changes: 11 additions & 23 deletions src/components/Admin/Categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,7 @@ 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} from "../../redux/actions/adminActions";

class Categories extends Component {

Expand All @@ -43,6 +26,10 @@ class Categories extends Component {
});
}

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

render() {
const { isAuthenticated, user } = this.props;
const { Column } = Table;
Expand All @@ -62,9 +49,9 @@ class Categories extends Component {
>
<Input 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}>
<Column title="Category Name" dataIndex="title" key="title" />
<Column title="Number" dataIndex="id" key="id" />

<Column
title="Action"
Expand All @@ -91,10 +78,11 @@ class Categories extends Component {

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

export default connect(
mapStateToProps,
{}
{fetchCategory}
)(Categories);
15 changes: 15 additions & 0 deletions src/redux/actions/adminActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {FETCH_CATEGORY} from './type';
import axios from 'axios';

export const fetchCategory =()=>{
return dispatch =>{
axios.get('https://jsonplaceholder.typicode.com/posts')
.then(res=>{
dispatch({
type:FETCH_CATEGORY,
payload: res.data,
})
})
}
};

4 changes: 3 additions & 1 deletion src/redux/actions/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ export const GET_ALL_CATEGORIES_PENDING = 'GET_ALL_CATEGORIES_PENDING';
export const GET_ALL_CATEGORIES_SUCCESS = 'GET_ALL_CATEGORIES_SUCCESS';

export const GET_CATEGORY_WISE_SERVICES_PENDING = 'GET_CATEGORY_WISE_SERVICES_PENDING';
export const GET_CATEGORY_WISE_SERVICES_SUCCESS = 'GET_CATEGORY_WISE_SERVICES_SUCCESS';
export const GET_CATEGORY_WISE_SERVICES_SUCCESS = 'GET_CATEGORY_WISE_SERVICES_SUCCESS';

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

const initState={
category:[],
}

const fetchCategory=(state=initState,action)=>{
switch(action.type){
case FETCH_CATEGORY:
return {
...state,
category:action.payload
}
default: return state;
}

}
export default fetchCategory;
8 changes: 4 additions & 4 deletions src/redux/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import vendorReducer from './vendorReducer';
import AuthReducer from './authReducer';
import ErrorReducer from './errorReducer';
import UserReducer from './userReducer';
import CategoryReducer from './categoryReducer';
import ServiceReducer from './serviceReducer';
import CategoryServiceReducer from './categoryServiceReducer';
import adminReducer from './adminReducer';


export default combineReducers({
auth: AuthReducer,
error: ErrorReducer,
user: UserReducer,
vendor: vendorReducer,
category: CategoryReducer,
service: ServiceReducer
categoryService: CategoryServiceReducer,
categorylist:adminReducer,
});