diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index 0eec1f1..a064295 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -8,20 +8,20 @@ assignees: '' --- **Describe the bug** -A clear and concise description of what the bug is. +[A clear and concise description of what the bug is.] **To Reproduce** -Steps to reproduce the behavior: +[Steps to reproduce the behavior: 1. Go to '...' 2. Click on '....' 3. Scroll down to '....' -4. See error +4. See error] **Expected behavior** -A clear and concise description of what you expected to happen. +[A clear and concise description of what you expected to happen.] **Screenshots** -If applicable, add screenshots to help explain your problem. +[If applicable, add screenshots to help explain your problem.] **Desktop (please complete the following information):** - OS: [e.g. iOS] diff --git a/src/components/Admin/Categories.js b/src/components/Admin/Categories.js index c78cf7a..d2164ed 100644 --- a/src/components/Admin/Categories.js +++ b/src/components/Admin/Categories.js @@ -1,29 +1,23 @@ 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 { Button,Modal,Input,Table, Divider,notification,Icon } from 'antd'; +import {fetchCategory,addCategory,deleteCategory,updateCategory} from '../../redux/actions/categoryActions' class Categories extends Component { - state = { visible: false } + state = { + visible: false , + visibleUpdateModal: false, + newCategoryName : '', + updateCategoryName : '', + } + + onChange = (e) => { + this.setState({ + [e.target.name]:e.target.value + }) + } showModal = () => { this.setState({ @@ -32,20 +26,69 @@ class Categories extends Component { } handleOk = (e) => { + this.props.addCategory(this.state.newCategoryName); this.setState({ visible: false, + newCategoryName: '' }); } handleCancel = (e) => { this.setState({ visible: false, + newCategoryName: '' + }); + } + + updateShowModal = () => { + this.setState({ + visibleUpdateModal: true, }); } + handleOkUpdate = (id) => { + this.props.updateCategory(this.state.updateCategoryName,id); + this.setState({ + visibleUpdateModal: false, + newCategoryName: '' + }); + } + + handleCancelUpdate = (e) => { + this.setState({ + visibleUpdateModal: false, + newCategoryName: '' + }); + } + + handleDelete = (id) => { + this.props.deleteCategory(id); + } + + sucessfulNotification=(type)=>{ + notification.open({ + message: type, + description: + '', + icon: , + }); + } + + componentDidUpdate(){ + this.props.fetchCategory(); + if(this.props.status===200&&this.props.statusType!=='fetchCategory'){ + this.sucessfulNotification(this.props.statusType); + } + } + componentDidMount(){ + this.props.fetchCategory(); + } + + render() { - const { isAuthenticated, user } = this.props; + const { isAuthenticated, user} = this.props; const { Column } = Table; + if (isAuthenticated && user.isVendor === false && user.isAdmin === true) { return ( @@ -60,20 +103,26 @@ class Categories extends Component { onOk={this.handleOk} onCancel={this.handleCancel} > - + - - - - +
+ ( - Update + Update + this.handleOkUpdate(record._id)} + onCancel={this.handleCancelUpdate} + > + + - Delete + {this.handleDelete(record._id)}} style={{cursor:'pointer',color:'red'}}>Delete )} /> @@ -91,10 +140,13 @@ class Categories extends Component { const mapStateToProps = state => ({ isAuthenticated: state.auth.isAuthenticated, - user: state.auth.user + user: state.auth.user, + category: state.category.categories, + status:state.category.status, + statusType:state.category.statusType, }); export default connect( mapStateToProps, - {} + {fetchCategory,addCategory,deleteCategory,updateCategory} )(Categories); \ No newline at end of file diff --git a/src/redux/actions/adminActions.js b/src/redux/actions/adminActions.js index adcfe2b..01dfd73 100644 --- a/src/redux/actions/adminActions.js +++ b/src/redux/actions/adminActions.js @@ -15,7 +15,7 @@ export const getAllUsers = () => (dispatch, getState) => { }); }) .catch(err => { - dispatch( returnErrors(err.response.data,err.response.status,"ADMIN_ACTIONS_ERRORS")); + dispatch(returnErrors(err.response.data,err.response.status,"ADMIN_ACTIONS_ERRORS")); dispatch({ type: GET_ALL_USERS_FAIL }) @@ -48,4 +48,4 @@ export const deleteUser = (userId) => (dispatch, getState) => { .catch(err => { dispatch( returnErrors(err.response.data,err.response.status,"ADMIN_ACTIONS_ERRORS")); }); - }; + }; \ No newline at end of file diff --git a/src/redux/actions/categoryActions.js b/src/redux/actions/categoryActions.js new file mode 100644 index 0000000..a9d37f8 --- /dev/null +++ b/src/redux/actions/categoryActions.js @@ -0,0 +1,51 @@ +import { FETCH_CATEGORY_SUCCESS,ADD_CATEGORY_SUCCESS,DELETE_CATEGORY_SUCCESS ,UPDATE_CATEGORY_SUCCESS} from "./type"; +import axios from "axios"; +import { tokenConfig } from "./authActions"; +import { url } from "../../helper/url"; + +export const fetchCategory = () => (dispatch,getState) => { + axios + .get(`${url}/categories`, tokenConfig(getState)) + .then(res =>{ + dispatch({ + type:FETCH_CATEGORY_SUCCESS, + payload:res.data + }) + }) + } + +export const addCategory = (name) => (dispatch,getState) => { + const body = JSON.stringify({ name }); + axios + .post(`${url}/categories`,body,tokenConfig(getState)) + .then((res) => { + dispatch({ + type:ADD_CATEGORY_SUCCESS, + payload:res.data + }) + }) +} + +export const deleteCategory = (id) => (dispatch,getState) => { + axios + .delete(`${url}/categories/`+id, tokenConfig(getState)) + .then(() => { + dispatch({ + type:DELETE_CATEGORY_SUCCESS, + payload:id + }) + }) +} + +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_SUCCESS, + payload:id, + packet:res.data + }) + }) +} \ No newline at end of file diff --git a/src/redux/actions/type.js b/src/redux/actions/type.js index 4535572..48f0a9d 100644 --- a/src/redux/actions/type.js +++ b/src/redux/actions/type.js @@ -35,4 +35,9 @@ export const DELETE_USER_FAIL = 'DELETE_USER_FAIL'; export const DELETED_ALL_SERVICES_OF_VENDOR = 'DELETED_ALL_SERVICES_OF_VENDOR'; export const POST_REVIEW_SUCCESS = 'POST_REVIEW_SUCCESS'; export const POST_REVIEW_FAIL = 'POST_REVIEW_FAIL'; -export const POST_REVIEW_LOADING = 'POST_REVIEW_LOADING'; \ No newline at end of file +export const POST_REVIEW_LOADING = 'POST_REVIEW_LOADING'; + +export const FETCH_CATEGORY_SUCCESS = 'FETCH_CATEGORY_SUCCESS'; +export const ADD_CATEGORY_SUCCESS = 'ADD_CATEGORY_SUCCESS'; +export const DELETE_CATEGORY_SUCCESS = 'DELETE_CATEGORY_SUCCESS'; +export const UPDATE_CATEGORY_SUCCESS = 'UPDATE_CATEGORY_SUCCESS'; \ No newline at end of file diff --git a/src/redux/reducers/adminReducers.js b/src/redux/reducers/adminReducers.js index 9b36eb6..11509a3 100644 --- a/src/redux/reducers/adminReducers.js +++ b/src/redux/reducers/adminReducers.js @@ -1,4 +1,4 @@ -import { GET_ALL_USERS, GET_ALL_USERS_FAIL, DELETE_USER, DELETE_USER_FAIL, DELETED_ALL_SERVICES_OF_VENDOR } from '../actions/type'; +import { GET_ALL_USERS, GET_ALL_USERS_FAIL, DELETE_USER, DELETE_USER_FAIL, DELETED_ALL_SERVICES_OF_VENDOR} from '../actions/type'; const initialState = { users: [], @@ -37,7 +37,7 @@ export default function (state = initialState, action) { ...state, status: 200, statusType: "deleteAllServices" - } + } default: return state; } diff --git a/src/redux/reducers/categoryReducer.js b/src/redux/reducers/categoryReducer.js new file mode 100644 index 0000000..1321b9b --- /dev/null +++ b/src/redux/reducers/categoryReducer.js @@ -0,0 +1,46 @@ +import { FETCH_CATEGORY_SUCCESS, ADD_CATEGORY_SUCCESS,DELETE_CATEGORY_SUCCESS,UPDATE_CATEGORY_SUCCESS } from '../actions/type'; + +const initialState = { + categories: [], + status: null, + statusType: null +} + +export default function (state = initialState, action) { + switch (action.type) { + case FETCH_CATEGORY_SUCCESS: + return { + ...state, + categories: action.payload, + status: 200, + statusType: "fetchCategory" + } + case ADD_CATEGORY_SUCCESS: + return { + categories: [...state.categories.categories,action.payload], + status:200, + statusType: "addCategory" + } + case DELETE_CATEGORY_SUCCESS: + return { + categories: state.categories.categories.filter((category)=>{ + return category._id!==action.payload + }), + status:200, + statusType:"deleteCategory" + } + case UPDATE_CATEGORY_SUCCESS: + return { + categories: state.categories.categories.map((category) => { + if(category._id===action.payload){ + category.name=action.packet + } + return category + }), + status:200, + statusType:"updateCategory" + } + default: + return state; + } +} \ No newline at end of file diff --git a/src/redux/reducers/index.js b/src/redux/reducers/index.js index 157d973..1410745 100644 --- a/src/redux/reducers/index.js +++ b/src/redux/reducers/index.js @@ -6,6 +6,7 @@ import UserReducer from './userReducer'; import CategoryServiceReducer from './categoryServiceReducer'; import AdminReducer from './adminReducers'; import ReviewReducer from './reviewReducer'; +import CategoryReducer from './categoryReducer'; export default combineReducers({ @@ -15,5 +16,6 @@ export default combineReducers({ vendor: vendorReducer, categoryService: CategoryServiceReducer, admin: AdminReducer, - review: ReviewReducer + review: ReviewReducer, + category:CategoryReducer }); \ No newline at end of file