diff --git a/client/src/actions/index.js b/client/src/actions/index.js index e6fa0d9..937b9b1 100644 --- a/client/src/actions/index.js +++ b/client/src/actions/index.js @@ -1,15 +1,13 @@ import axios from 'axios'; -import { Subject } from 'rxjs'; +import {Subject} from 'rxjs'; const ApiMap = { - 'development': 'http://127.0.0.1:4900', - 'production': '', -} + development: 'http://127.0.0.1:4900', + production: '', +}; // console.log('process.env.NODE_ENV:', process.env.NODE_ENV); export const ApiService = ApiMap[process.env.NODE_ENV] || ''; -// console.log('api:', ApiService) - export const TypeMap = { LOADING: 'COMMON_LOADING', LOADING_HIDE: 'COMMON_LOADING_HIDE', @@ -40,6 +38,7 @@ export const TypeMap = { BORROW_REQUEST_MSG: 'BORROW_REQUEST_MSG', BORROW_REPLY_MSG: 'BORROW_REPLY_MSG', BORROW_REFUSE: 'BORROW_REFUSE', + BORROW_RETURN: 'BORROW_RETURN', }; export class Utility { @@ -102,9 +101,9 @@ export class Utility { return null; } - static Toast() { } + static Toast() {} - static LoadingHide() { } + static LoadingHide() {} static IsArray(obj) { if (!obj || !Array.isArray(obj) || obj.length === 0) { @@ -115,7 +114,7 @@ export class Utility { } export class Httphelper { - static async __request({ method = 'get', url, headers, params, data, dispatch }) { + static async __request({method = 'get', url, headers, params, data, dispatch}) { try { if (!url) { Utility.Toast('url not found'); @@ -127,24 +126,23 @@ export class Httphelper { if (token) { _headers.token = token; } - const opt = { method, url: _url, headers: _headers, params, data }; + const opt = {method, url: _url, headers: _headers, params, data}; const response = await axios(opt); - const { data: body } = response; + const {data: body} = response; return body.data || body; } catch (ex) { - const { response } = ex; - const { data: errData, status, statusText } = response || {}; - const { msg } = errData || {}; + const {response} = ex; + const {data: errData, status, statusText} = response || {}; + const {msg} = errData || {}; if (status === 401) { Utility.Token = null; Utility.UserInfo = null; alert(msg || statusText); - if (dispatch) { - dispatch({ type: TypeMap.USER_401, payload: '' }); + dispatch({type: TypeMap.USER_401, payload: ''}); } - Utility.NotifyLogout.next({ isLogin: true }); + Utility.NotifyLogout.next({isLogin: true}); console.log(statusText); return Promise.reject(msg); } @@ -163,8 +161,8 @@ export class Httphelper { * @returns * @memberof Httphelper */ - static async onPost(url, { headers, params, data }, dispatch) { - return this.__request({ method: 'post', url, headers, params, data, dispatch }); + static async onPost(url, {headers, params, data}, dispatch) { + return this.__request({method: 'post', url, headers, params, data, dispatch}); } /** @@ -174,8 +172,8 @@ export class Httphelper { * @returns * @memberof Httphelper */ - static async onPut(url, { headers, params, data }, dispatch) { - return this.__request({ method: 'put', url, headers, params, data, dispatch }); + static async onPut(url, {headers, params, data}, dispatch) { + return this.__request({method: 'put', url, headers, params, data, dispatch}); } /** @@ -185,8 +183,8 @@ export class Httphelper { * @returns * @memberof Httphelper */ - static async onDelete(url, { headers, params, data }, dispatch) { - return this.__request({ method: 'delete', url, headers, params, data, dispatch }); + static async onDelete(url, {headers, params, data}, dispatch) { + return this.__request({method: 'delete', url, headers, params, data, dispatch}); } /** @@ -196,14 +194,14 @@ export class Httphelper { * @returns * @memberof Httphelper */ - static async onGet(url, { headers, params }, dispatch) { - return this.__request({ method: 'get', url, headers, params, dispatch }); + static async onGet(url, {headers, params}, dispatch) { + return this.__request({method: 'get', url, headers, params, dispatch}); } } // update user info export const userUpdate = (userInfo) => { - return { type: TypeMap.USER_UPDATE, payload: userInfo }; + return {type: TypeMap.USER_UPDATE, payload: userInfo}; }; /** * user login @@ -211,10 +209,10 @@ export const userUpdate = (userInfo) => { */ export const userLogin = (info) => { return async (dispatch) => { - const item = await Httphelper.onPost('/api/user/signin', { data: info }, dispatch); + const item = await Httphelper.onPost('/api/user/signin', {data: info}, dispatch); Utility.Token = item.token; Utility.UserInfo = item; - dispatch({ type: TypeMap.USER_LOGIN, payload: item }); + dispatch({type: TypeMap.USER_LOGIN, payload: item}); }; }; @@ -224,13 +222,13 @@ export const userLogin = (info) => { */ export const userSignUp = (data) => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { - const item = await Httphelper.onPost('/api/user/signup', { data }, dispatch); - dispatch({ type: TypeMap.USER_SIGNUP, payload: item }); + const item = await Httphelper.onPost('/api/user/signup', {data}, dispatch); + dispatch({type: TypeMap.USER_SIGNUP, payload: item}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; @@ -238,26 +236,26 @@ export const userSignUp = (data) => { // get category list export const categoryList = (params) => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { - const item = await Httphelper.onGet('/api/category', { params }, dispatch); - dispatch({ type: TypeMap.CATEGORY_LIST, payload: item }); + const item = await Httphelper.onGet('/api/category', {params}, dispatch); + dispatch({type: TypeMap.CATEGORY_LIST, payload: item}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; // get category list export const categoryListAll = (params) => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { - const item = await Httphelper.onGet('/api/category/all', { params }, dispatch); - dispatch({ type: TypeMap.CATEGORY_LIST_ALL, payload: item }); + const item = await Httphelper.onGet('/api/category/all', {params}, dispatch); + dispatch({type: TypeMap.CATEGORY_LIST_ALL, payload: item}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; @@ -265,13 +263,13 @@ export const categoryListAll = (params) => { // edit category by id export const categoryEdit = (id, data) => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { - const item = await Httphelper.onPut('/api/category/' + id, { data }, dispatch); - dispatch({ type: TypeMap.CATEGORY_DELETE, payload: item }); + const item = await Httphelper.onPut('/api/category/' + id, {data}, dispatch); + dispatch({type: TypeMap.CATEGORY_DELETE, payload: item}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; @@ -279,15 +277,15 @@ export const categoryEdit = (id, data) => { // search book by keyword export const bookSearch = (keyword, page = 1, size = 20) => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { - dispatch({ type: TypeMap.BOOK_SEARCH_KEYWORD, payload: { keyword, page, size } }); - const item = await Httphelper.onGet('/api/book/search', { params: { page, size, keyword } }, dispatch); + dispatch({type: TypeMap.BOOK_SEARCH_KEYWORD, payload: {keyword, page, size}}); + const item = await Httphelper.onGet('/api/book/search', {params: {page, size, keyword}}, dispatch); - dispatch({ type: TypeMap.BOOK_SEARCH, payload: item }); + dispatch({type: TypeMap.BOOK_SEARCH, payload: item}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; @@ -295,13 +293,13 @@ export const bookSearch = (keyword, page = 1, size = 20) => { // search book by keyword export const bookListByMy = () => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { - const item = await Httphelper.onGet('/api/book/my', { params: {} }, dispatch); - dispatch({ type: TypeMap.BOOK_MY_LIST, payload: item }); + const item = await Httphelper.onGet('/api/book/my', {params: {}}, dispatch); + dispatch({type: TypeMap.BOOK_MY_LIST, payload: item}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; @@ -309,13 +307,13 @@ export const bookListByMy = () => { // get book detail info by id export const bookDetail = (id) => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { - const item = await Httphelper.onGet('/api/book/' + id, { params: {} }, dispatch); - dispatch({ type: TypeMap.BOOK_DETAIL, payload: item }); + const item = await Httphelper.onGet('/api/book/' + id, {params: {}}, dispatch); + dispatch({type: TypeMap.BOOK_DETAIL, payload: item}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; @@ -323,13 +321,13 @@ export const bookDetail = (id) => { // delete book by id export const bookDelete = (id, index) => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { - const item = await Httphelper.onDelete('/api/book/' + id, { params: {} }, dispatch); - dispatch({ type: TypeMap.BOOK_MY_DELETE, payload: { id, index } }); + const item = await Httphelper.onDelete('/api/book/' + id, {params: {}}, dispatch); + dispatch({type: TypeMap.BOOK_MY_DELETE, payload: {id, index}}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; @@ -337,17 +335,17 @@ export const bookDelete = (id, index) => { // add book export const bookAdd = (data) => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { let formData = new FormData(); for (let key in data) { formData.append(key, data[key]); } - const item = await Httphelper.onPost('/api/book/', { data: formData }); - dispatch({ type: TypeMap.BOOK_MY_RELEASE, payload: item }, dispatch); + const item = await Httphelper.onPost('/api/book/', {data: formData}); + dispatch({type: TypeMap.BOOK_MY_RELEASE, payload: item}, dispatch); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; @@ -355,86 +353,86 @@ export const bookAdd = (data) => { // edit book export const bookEdit = (id, data) => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { let formData = new FormData(); for (let key in data) { formData.append(key, data[key]); } - const item = await Httphelper.onPut('/api/book/' + id, { data: formData }); - dispatch({ type: TypeMap.BOOK_MY_UPDATE, payload: item }, dispatch); + const item = await Httphelper.onPut('/api/book/' + id, {data: formData}); + dispatch({type: TypeMap.BOOK_MY_UPDATE, payload: item}, dispatch); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; // book search keyword export const bookSearchKeyword = (keyword, page = 1, size = 20) => { - return { type: TypeMap.BOOK_SEARCH_KEYWORD, payload: { keyword, page, size } }; + return {type: TypeMap.BOOK_SEARCH_KEYWORD, payload: {keyword, page, size}}; }; export const borrowAdd = (data) => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { - const item = await Httphelper.onPost('/api/borrow/', { data }, dispatch); - dispatch({ type: TypeMap.BORROW_ADD, payload: item }); + const item = await Httphelper.onPost('/api/borrow/', {data}, dispatch); + dispatch({type: TypeMap.BORROW_ADD, payload: item}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; export const borrowDelete = (id, index) => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { const item = await Httphelper.onDelete('/api/borrow/' + id, {}, dispatch); - dispatch({ type: TypeMap.BORROW_DELETE, payload: { id, index } }); + dispatch({type: TypeMap.BORROW_DELETE, payload: {id, index}}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; export const borrowDetail = (id) => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { const detail = await Httphelper.onGet('/api/borrow/' + id, {}, dispatch); - dispatch({ type: TypeMap.BORROW_DETAIL, payload: detail }); + dispatch({type: TypeMap.BORROW_DETAIL, payload: detail}); return detail; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; export const borrowMyList = () => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { - const item = await Httphelper.onGet('/api/borrow/my', { params: {} }, dispatch); - dispatch({ type: TypeMap.BORROW_MY_LIST, payload: item }); + const item = await Httphelper.onGet('/api/borrow/my', {params: {}}, dispatch); + dispatch({type: TypeMap.BORROW_MY_LIST, payload: item}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; export const borrowRequest = () => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { const item = await Httphelper.onGet('/api/borrow/request', {}, dispatch); - dispatch({ type: TypeMap.BORROW_REQUEST, payload: item }); + dispatch({type: TypeMap.BORROW_REQUEST, payload: item}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; @@ -442,13 +440,13 @@ export const borrowRequest = () => { export const borrowRequestMsg = (id, content) => { return async (dispatch) => { //BORROW_REPLY_TO - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { - const item = await Httphelper.onPut('/api/borrow/request/msg/' + id, { data: { content } }, dispatch); - dispatch({ type: TypeMap.BORROW_REQUEST_MSG, payload: item }); + const item = await Httphelper.onPut('/api/borrow/request/msg/' + id, {data: {content}}, dispatch); + dispatch({type: TypeMap.BORROW_REQUEST_MSG, payload: item}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; @@ -456,39 +454,52 @@ export const borrowRequestMsg = (id, content) => { export const borrowReplyMsg = (id, content) => { return async (dispatch) => { //BORROW_REPLY_TO - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { - const item = await Httphelper.onPut('/api/borrow/reply/msg/' + id, { data: { content } }, dispatch); - dispatch({ type: TypeMap.BORROW_REPLY_MSG, payload: item }); + const item = await Httphelper.onPut('/api/borrow/reply/msg/' + id, {data: {content}}, dispatch); + dispatch({type: TypeMap.BORROW_REPLY_MSG, payload: item}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; - +// refuse borrow book export const borrowRefuse = (id, content) => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { - const item = await Httphelper.onPut('/api/borrow/refuse/' + id, { data: { content } }, dispatch); - dispatch({ type: TypeMap.BORROW_REFUSE, payload: item }); + const item = await Httphelper.onPut('/api/borrow/refuse/' + id, {data: {content}}, dispatch); + dispatch({type: TypeMap.BORROW_REFUSE, payload: item}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; - +// confirm borrow book export const borrowConfirm = (id, content) => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); + try { + const item = await Httphelper.onPut('/api/borrow/confirm/' + id, {data: {content}}, dispatch); + dispatch({type: TypeMap.BORROW_CONFIRM, payload: item}); + return item; + } finally { + dispatch({type: TypeMap.LOADING_HIDE}); + } + }; +}; +// return borrow book +export const borrowReturn = (id, index) => { + return async (dispatch) => { + dispatch({type: TypeMap.LOADING}); try { - const item = await Httphelper.onPut('/api/borrow/confirm/' + id, { data: { content } }, dispatch); - dispatch({ type: TypeMap.BORROW_CONFIRM, payload: item }); + const item = await Httphelper.onPut('/api/borrow/return/' + id, {data: {}}, dispatch); + dispatch({type: TypeMap.BORROW_RETURN, payload: {id, index}}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; diff --git a/client/src/components/component.header.js b/client/src/components/component.header.js index 003d965..f776a16 100644 --- a/client/src/components/component.header.js +++ b/client/src/components/component.header.js @@ -94,7 +94,7 @@ class Header extends React.Component { return (