diff --git a/src/components/Admin/Categories.js b/src/components/Admin/Categories.js index c78cf7a..926b70c 100644 --- a/src/components/Admin/Categories.js +++ b/src/components/Admin/Categories.js @@ -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; @@ -51,29 +81,35 @@ class Categories extends Component { Categories
- - + {this.handleChangeForm(value)}} placeholder="Enter the name of the category" /> - - - - +
+ ( + key="_id" + render={(record) => ( - Update + this.showModalUpdate(record._id)}>Update + + {this.handleChangeUpdate(value)}} placeholder="Enter the name of the category" /> + - Delete + {this.handleDelete(record._id)}}>Delete )} /> @@ -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); \ No newline at end of file diff --git a/src/redux/actions/adminActions.js b/src/redux/actions/adminActions.js new file mode 100644 index 0000000..776443b --- /dev/null +++ b/src/redux/actions/adminActions.js @@ -0,0 +1,47 @@ +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 = () => (dispatch) => { + axios.get(`${url}/categories`) + .then( res => { + dispatch({ + type:FETCH_CATEGORY, + payload: res.data.categories, + }) + }) +}; + +export const deleteCategory = (id) => (dispatch,getState) =>{ + axios.delete(`${url}/categories/`+id,tokenConfig(getState)) + .then(( ) => { + dispatch({ + type:DELETE_CATEGORY, + payload: id, + }) + }) +}; + +export const addCategory = (name) => (dispatch,getState) => { + const body = JSON.stringify({ name }) ; + axios.post(`${url}/categories`,body,tokenConfig(getState)) + .then(res => { + dispatch({ + type:ADD_CATEGORY, + payload: res.data, + }) + }) +} + +export const updateCategory =(name,id)=>(dispatch,getState) =>{ + const body = JSON.stringify({ name }); + axios.patch(`${url}/categories/`+id,body,tokenConfig(getState)) + .then(res => { + dispatch({ + type:UPDATE_CATEGORY, + payload: id, + newCategory:res.data, + }) + }) +}; \ No newline at end of file diff --git a/src/redux/actions/type.js b/src/redux/actions/type.js index d74ea73..51ab0f2 100644 --- a/src/redux/actions/type.js +++ b/src/redux/actions/type.js @@ -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'; \ No newline at end of file +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'; \ No newline at end of file diff --git a/src/redux/reducers/adminReducers.js b/src/redux/reducers/adminReducers.js new file mode 100644 index 0000000..90a4296 --- /dev/null +++ b/src/redux/reducers/adminReducers.js @@ -0,0 +1,35 @@ +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; \ No newline at end of file diff --git a/src/redux/reducers/index.js b/src/redux/reducers/index.js index 6d57b25..10b5062 100644 --- a/src/redux/reducers/index.js +++ b/src/redux/reducers/index.js @@ -4,6 +4,7 @@ import AuthReducer from './authReducer'; import ErrorReducer from './errorReducer'; import UserReducer from './userReducer'; import CategoryServiceReducer from './categoryServiceReducer'; +import AdminReducer from './adminReducers'; export default combineReducers({ @@ -11,5 +12,6 @@ export default combineReducers({ error: ErrorReducer, user: UserReducer, vendor: vendorReducer, - categoryService: CategoryServiceReducer + categoryService: CategoryServiceReducer, + admin:AdminReducer, }); \ No newline at end of file