From f414876aaee766589c944cd26e53e5b97ddd03f9 Mon Sep 17 00:00:00 2001 From: Reza_ashrafi Date: Thu, 22 Jul 2021 19:22:20 +0430 Subject: [PATCH] reza added new proxy --- src/Redux/actions/blog.js | 19 +++ src/Redux/actions/chat.js | 48 +++++-- src/Redux/actions/index.js | 4 +- src/Redux/actions/user.js | 44 ++++-- src/Redux/proxy.js | 132 ++++++++++++------ src/Redux/reducers/blog.js | 25 ++++ src/Redux/reducers/user.js | 33 ++++- src/constants/defaultValues.js | 9 ++ src/views/Auth/Login/Code/index.js | 2 +- src/views/Auth/Login/index.js | 45 +++--- src/views/ChatList/index.js | 4 +- src/views/ChatRoom/Body/index.js | 6 +- src/views/Home/HomeStatic/Identifier/index.js | 2 +- src/views/QR/VideoBox/index.js | 56 ++++---- 14 files changed, 301 insertions(+), 128 deletions(-) create mode 100644 src/Redux/actions/blog.js create mode 100644 src/Redux/reducers/blog.js create mode 100644 src/constants/defaultValues.js diff --git a/src/Redux/actions/blog.js b/src/Redux/actions/blog.js new file mode 100644 index 0000000..1068494 --- /dev/null +++ b/src/Redux/actions/blog.js @@ -0,0 +1,19 @@ +import proxy from '../proxy'; +const blog = { + list: (data = {}) => async (dispatch) => + await proxy.get('blog/list', data, { dispatch }), + update: (data = {}, data2 = {}) => async (dispatch) => { + await proxy.put('blog/update', data); + await proxy.get('blog/list', data2, { dispatch }); + }, + add: (data = {}, data2 = {}) => async (dispatch) => { + await proxy.post('blog/add', data); + await proxy.get('blog/list', data2, { dispatch }); + }, + del: (data = {}, data2 = {}) => async (dispatch) => { + await proxy.delete('blog/delete', data); + await proxy.get('blog/list', data2, { dispatch }); + }, +}; + +export default blog; \ No newline at end of file diff --git a/src/Redux/actions/chat.js b/src/Redux/actions/chat.js index 2ffa612..4abb5ee 100644 --- a/src/Redux/actions/chat.js +++ b/src/Redux/actions/chat.js @@ -1,13 +1,35 @@ -import proxy from '../proxy'; -export const list = - (data = {}) => - async (dispatch) => - (await proxy.get('chat/list', data)).dispatch(dispatch); -export const info = - (data = {}) => - async (dispatch) => - (await proxy.get('chat/info', data)).dispatch(dispatch); -export const bio = - (data = {}) => - async (dispatch) => - (await proxy.get('chat/bio', data)).dispatch(dispatch); +import proxy from "../proxy"; +const chat = { + list: + (data = {}) => + async (dispatch) => + await proxy.get("chat/list", data, { dispatch }), + info: + (data = {}) => + async (dispatch) => + await proxy.get("chat/info", data, { dispatch }), + bio: + (data = {}) => + async (dispatch) => + await proxy.get("chat/bio", data, { dispatch }), + update: + (data = {}, data2 = {}) => + async (dispatch) => { + await proxy.put("blog/update", data); + await proxy.get("blog/list", data2, { dispatch }); + }, + add: + (data = {}, data2 = {}) => + async (dispatch) => { + await proxy.post("blog/add", data); + await proxy.get("blog/list", data2, { dispatch }); + }, + del: + (data = {}, data2 = {}) => + async (dispatch) => { + await proxy.delete("blog/delete", data); + await proxy.get("blog/list", data2, { dispatch }); + }, +}; + +export default chat; diff --git a/src/Redux/actions/index.js b/src/Redux/actions/index.js index 8605f81..db5f287 100644 --- a/src/Redux/actions/index.js +++ b/src/Redux/actions/index.js @@ -1,7 +1,7 @@ // @create-index export { default as chat } from './chat.js'; -export { default as message } from './message.js'; -export { default as public } from './public.js'; +// export { default as message } from './message.js'; +// export { default as public } from './public.js'; export { default as user } from './user.js'; diff --git a/src/Redux/actions/user.js b/src/Redux/actions/user.js index 35fe2d0..1dc496e 100644 --- a/src/Redux/actions/user.js +++ b/src/Redux/actions/user.js @@ -1,9 +1,37 @@ import proxy from '../proxy'; -export const login = - (data = {}) => - async (dispatch) => - (await proxy.login(data)).dispatch(dispatch); -export const logout = - (data = {}) => - async (dispatch) => - (await proxy.logout(data)).dispatch(dispatch); +const user = { + otp: (data, history) => async (dispatch) => + await proxy.login('public/sendOTP', data, { history, dispatch }), + otp_login: (data, history) => async (dispatch) => + await proxy.login('user/otp/login', data, { history, dispatch }), + register: (data, history) => async (dispatch) => + await proxy.login('user/register', data, { history, dispatch }), + switchRole: (data, history) => async (dispatch) => + await proxy.login('user/switchRole', data, { history, dispatch }), + logout: (data, history) => async (dispatch) => + await proxy.logout('user/logout', data, { history, dispatch }), + getUserRole: (data) => async (dispatch) => + await proxy.get('user/getUserRole', data, { dispatch }), + list: (data = {}) => async (dispatch) => + await proxy.get('user/list', data, { dispatch }), + domains: (data = {}) => async (dispatch) => + await proxy.get('user/domains', data, { dispatch }), + getmothers: (data = {}) => async (dispatch) => + await proxy.get2('mothers/list', data, { + dispatch, + }), + update: (data = {}, data2 = {}) => async (dispatch) => { + await proxy.put('user/update', data); + await proxy.get('user/list', data2, { dispatch }); + }, + add: (data = {}, data2 = {}) => async (dispatch) => { + await proxy.post('user/add', data); + await proxy.get('user/list', data2, { dispatch }); + }, + del: (data = {}, data2 = {}) => async (dispatch) => { + await proxy.delete('user/delete', data); + await proxy.get('user/list', data2, { dispatch }); + }, +}; + +export default user; \ No newline at end of file diff --git a/src/Redux/proxy.js b/src/Redux/proxy.js index 3c566a8..7abfce2 100644 --- a/src/Redux/proxy.js +++ b/src/Redux/proxy.js @@ -1,63 +1,113 @@ +import { ApiConfig } from './../constants/defaultValues'; import axios from 'axios'; -import { toast } from 'react-toastify'; -const baseURL = 'https://chatstory.ir/api/v1'; //'https://chatstory.ir/api/v1'; -const token = ''; -const loginURL = 'user/otp/login'; -const logoutURL = 'user/logout'; -window.baseURL = baseURL; + +let { baseUrl, apiKey } = ApiConfig; +baseUrl = baseUrl + '/'; +const baseUrl2 = 'https://dnvn.ir/api/v1'; const Axios = axios.create({ withCredentials: true, validateStatus: null, - baseURL, + baseURL: baseUrl, +}); +const Axios2 = axios.create({ + withCredentials: true, + validateStatus: null, + baseURL: baseUrl2, }); -class dispatch {} class Proxy { - get = (url, params, opt = {}) => this.check(url, () => Axios.get(url, { params, ...opt })); - post = (url, params, opt = {}) => this.check(url, () => Axios.post(url, params, opt)); - put = (url, params, opt = {}) => this.check(url, () => Axios.put(url, params, opt)); - delete = (url, params, opt = {}) => this.check(url, () => Axios.delete(url, params, opt)); - result = (url, data) => ({ - ...data, - dispatch: (dispatch) => { - dispatch({ type: url, data }); - return data; - }, - }); - check = async (url, fetch) => { + get = async (url, params, opt = {}) => + await this.check( + url, + opt, + async () => await Axios.get(url, { params, ...opt }) + ); + get2 = async (url, params, opt = {}) => + await this.check2( + url, + opt, + async () => await Axios2.get(url, { params, ...opt }) + ); + post = async (url, params, opt = {}) => + await this.check(url, opt, async () => await Axios.post(url, params, opt)); + put = async (url, params, opt = {}) => + await this.check(url, opt, async () => await Axios.put(url, params, opt)); + delete = async (url, params, opt = {}) => { + await this.check( + url, + opt, + async () => await Axios.delete(url, { ...opt, data: params }) + ); + }; + + check = async (url, { dispatch }, fetch) => { + dispatch = dispatch || (() => {}); + dispatch({ type: 'loading' }); let response = await fetch(); switch (response.status) { case 200: - return this.result(url, response.data.data); + dispatch({ type: url, data: response.data.data }); + return response.data.data; case 401: - if (await this.refresh()) return this.result(url, (await fetch()).data.data); + if (await this.refresh()) { + let response = await fetch(); + dispatch({ type: url, data: response.data.data }); + return response.data.data; + } break; default: - toast.error(response.data.message); + dispatch({ type: 'error', data: response.data }); + } + return false; + }; + check2 = async (url, { dispatch }, fetch) => { + dispatch({ type: 'loading' }); + let response = await fetch(); + switch (response.status) { + case 200: + dispatch({ type: url, data: response.data }); + return response.data; + case 401: + if (await this.refresh()) { + let response = await fetch(); + dispatch({ type: url, data: response.data }); + return response.data; + } + break; + default: + dispatch({ type: 'error', data: response.data }); } return false; }; refresh = async () => { let refresh = localStorage.getItem('refresh'); if (!refresh) window.location.href = '/'; - let login = await this.login({}, { headers: { Authorization: `Bearer ${refresh}` } }); + let login = await this.login( + {}, + { headers: { Authorization: `Bearer ${refresh}` } } + ); return login ? true : false; }; - login = async (data, opt = { headers: { Authorization: `Bearer ${token}` } }) => { - const login = await this.post(loginURL, data, opt); - if (!login || !login.refreshToken) return this.result('login', false); - localStorage.setItem('refresh', login.refreshToken); - delete login.refreshToken; - localStorage.setItem('userData', JSON.stringify(login)); - window.location = "/"; - if (data != {}) window.router.setState({ islogin: true }); - return this.result('login', login); - }; - logout = async () => { - this.post(logoutURL); - localStorage.removeItem('refresh'); - localStorage.removeItem('userData'); - window.router.setState({ islogin: false }); - return this.result('logout', {}); + login = async (url, params, { dispatch }) => + this.post(url, params, { + dispatch: (obj) => { + let login = obj.data; + if (!login || !login.refreshToken) return false; + localStorage.setItem('refresh', login.refreshToken); + delete login.refreshToken; + delete login.accessToken; + localStorage.setItem('userData', JSON.stringify(login)); + dispatch(obj); + }, + }); + + logout = async (url, params, { dispatch }) => { + this.post(url, params, { + dispatch: (obj) => { + localStorage.removeItem('refresh'); + localStorage.removeItem('userData'); + dispatch(obj); + }, + }); }; status = () => { let refresh = localStorage.getItem('refresh'); @@ -73,4 +123,4 @@ class Proxy { } const _proxy = new Proxy(); -export default _proxy; +export default _proxy; \ No newline at end of file diff --git a/src/Redux/reducers/blog.js b/src/Redux/reducers/blog.js new file mode 100644 index 0000000..05b1d0e --- /dev/null +++ b/src/Redux/reducers/blog.js @@ -0,0 +1,25 @@ +const initialState = { + loading: false, + error: null, + list: [], +}; +export default function user(state = initialState, action) { + let { type, data } = action; + switch (type) { + case 'blog/list': + return { ...state, loading: false, list: data, error: null }; + case 'blog/update': + return { ...state, loading: false, error: null }; + case 'blog/add': + return { ...state, loading: false, error: null }; + case 'blog/delete': + return { ...state, loading: false, error: null }; + ///////////// + case 'loading': + return { ...state, loading: true }; + case 'error': + return { ...state, loading: false, error: data.message }; + default: + return state; + } +} \ No newline at end of file diff --git a/src/Redux/reducers/user.js b/src/Redux/reducers/user.js index 6dbe2cd..d9ddd9c 100644 --- a/src/Redux/reducers/user.js +++ b/src/Redux/reducers/user.js @@ -1,15 +1,36 @@ import proxy from '../proxy'; const initialState = { - isLogin: proxy.status(), + status: proxy.status(), + loading: false, + error: null, + list: null, + domains: [], + mothers: [], }; export default function user(state = initialState, action) { let { type, data } = action; switch (type) { - case 'login': - return { ...state, ...proxy.status(), islogin: proxy.status(), ...data }; - case 'logout': - return { ...state, islogin: proxy.status() }; + case 'user/login': + case 'user/register': + case 'user/otp/login': + case 'user/switchRole': + return { ...state, loading: false, status: data, error: null }; + case 'user/logout': + return { ...state, loading: false, status: proxy.status(), error: null }; + case 'user/getUserRole': + return { ...state, loading: false, userRoles: data, error: null }; + case 'user/list': + return { ...state, loading: false, list: data, error: null }; + case 'user/domains': + return { ...state, loading: false, domains: data, error: null }; + case 'mothers/list': + return { ...state, loading: false, mothers: data, error: null }; + ///////////// + case 'loading': + return { ...state, loading: true }; + case 'error': + return { ...state, loading: false, error: data.message }; default: return state; } -} +} \ No newline at end of file diff --git a/src/constants/defaultValues.js b/src/constants/defaultValues.js new file mode 100644 index 0000000..2591544 --- /dev/null +++ b/src/constants/defaultValues.js @@ -0,0 +1,9 @@ +export const ApiConfig = { + apiKey: 'AIzaSyBBksq-Asxq2M4Ot-75X19IyrEYJqNBPcg', + authDomain: 'dnvn.ir', + baseUrl: 'https://dnvn.ir/api/v1', + //baseUrl: 'http://localhost:3030/api/v1', + loginURL: 'user/login', + otploginURL: 'user/otp/login', + logoutURL: 'user/logout', +}; \ No newline at end of file diff --git a/src/views/Auth/Login/Code/index.js b/src/views/Auth/Login/Code/index.js index 37675dd..c6e80e0 100644 --- a/src/views/Auth/Login/Code/index.js +++ b/src/views/Auth/Login/Code/index.js @@ -210,7 +210,7 @@ export default class Code extends Component {

دانوین

- یک کد تائیدیه به شماره 09123190615 ارسال شد
+ یک کد تائیدیه به شماره {this.props.parent.state.cellphone} ارسال شد
آن را در قسمت زیر وارد کنید.

diff --git a/src/views/Auth/Login/index.js b/src/views/Auth/Login/index.js index 5023c99..e26eab8 100644 --- a/src/views/Auth/Login/index.js +++ b/src/views/Auth/Login/index.js @@ -4,53 +4,46 @@ import Cellphone from "./Cellphone/index"; import Code from "./Code/index"; import Splash from "../Splash/index"; -import { otp } from '../../../Redux/actions/public'; -import { login } from '../../../Redux/actions/user'; -import { connect } from 'react-redux'; +import { user } from "../../../Redux/actions"; +import { connect } from "react-redux"; class Login extends Component { constructor(props) { super(props); this.state = { page: 0, - splash : true, + splash: false, }; } async componentDidUpdate() { const { cellphone, otp, page } = this.state; if (cellphone && !otp) this.props.otp({ cellphone }); - if (page && cellphone && otp) this.props.login({ cellphone, otp }); + if (page && cellphone && otp) this.props.otp_login({ cellphone : cellphone, otp: Number(otp) }); } - componentDidMount() { - setTimeout(() =>{ - this.setState({ - splash: false, - }) - },6000); - } - - resend = () => { - const {cellphone} = this.state; - this.props.otp({cellphone}); - } + const { cellphone } = this.state; + this.props.otp({ cellphone }); + }; render() { return ( <> - { - this.state.page === 0 ? - ( !this.state.splash ? : null) : - - } - { - this.state.splash ? : null - } + {this.state.page === 0 ? ( + !this.state.splash ? ( + + ) : null + ) : ( + + )} + {this.state.splash ? : null} ); } } -export default connect((state) => ({ isLogin: state.user.isLogin }), { otp, login })(Login); +export default connect((state) => ({ isLogin: state.user.isLogin }), { + otp: user.otp, + otp_login: user.otp_login, +})(Login); diff --git a/src/views/ChatList/index.js b/src/views/ChatList/index.js index 8be54b6..77b08bf 100644 --- a/src/views/ChatList/index.js +++ b/src/views/ChatList/index.js @@ -1,6 +1,6 @@ import React, { Component } from 'react'; import {Link} from 'react-router-dom'; -import { list } from '../../Redux/actions/chat'; +import { chat } from '../../Redux/actions'; import { connect } from 'react-redux'; import ChatIcon from '@material-ui/icons/Chat'; import Chat from './Chat/index'; @@ -70,6 +70,6 @@ export default connect( (state) => ({ chat_list: state.chat.list || [], }), - { list } + { list : chat.list } )(ChatList); diff --git a/src/views/ChatRoom/Body/index.js b/src/views/ChatRoom/Body/index.js index 398df89..42ffd46 100644 --- a/src/views/ChatRoom/Body/index.js +++ b/src/views/ChatRoom/Body/index.js @@ -6,8 +6,8 @@ import { RecordState } from "audio-react-recorder"; import ReactPlayer from "react-player"; import ReactAudioPlayer from "react-audio-player"; import { ToastContainer } from "react-toastify"; -import { list, send, sendKeys } from "../../../Redux/actions/message"; -import { info, bio } from "../../../Redux/actions/chat"; +// import { list, send, sendKeys } from "../../../Redux/actions/message"; +import { chat } from "../../../Redux/actions"; import { connect } from "react-redux"; import Emoji from "./Emoji/index"; @@ -655,5 +655,5 @@ export default connect( last: state.message.list.slice(-1)[0] || {}, }; }, - { list, send, sendKeys, info, bio } + { info : chat.info , bio : chat.bio } )(List); diff --git a/src/views/Home/HomeStatic/Identifier/index.js b/src/views/Home/HomeStatic/Identifier/index.js index a1e4a6a..0d0be38 100644 --- a/src/views/Home/HomeStatic/Identifier/index.js +++ b/src/views/Home/HomeStatic/Identifier/index.js @@ -22,7 +22,7 @@ export default function Identifier(props){ } { data.design === '3d' ? - : null + : null } ); diff --git a/src/views/QR/VideoBox/index.js b/src/views/QR/VideoBox/index.js index 5116017..2a3a1f1 100644 --- a/src/views/QR/VideoBox/index.js +++ b/src/views/QR/VideoBox/index.js @@ -1,27 +1,33 @@ -import React from 'react'; -import {VideoCard} from 'material-ui-player'; -import ReactVideoJsPlayer from './React-video-js-player/index'; +import React from "react"; +import { VideoCard } from "material-ui-player"; +import ReactVideoJsPlayer from "./React-video-js-player/index"; -import './index.scss'; +import "./index.scss"; - -export default function VideoBox(props){ - return( - <> - { - window.innerWidth > 1000 ? -
-

{props.data.title}

- -
: null - } - { - window.innerWidth < 1000 ? -
-

{props.data.title}

- -
: null - } - - ); -} \ No newline at end of file +export default function VideoBox(props) { + return ( + <> + {window.innerWidth > 1000 ? ( +
+

+ + {props.data.title} +

+ +
+ ) : null} + {window.innerWidth < 1000 ? ( +
+

+ + {props.data.title} +

+ +
+ ) : null} + + ); +}