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} ارسال شد
آن را در قسمت زیر وارد کنید.
- }
- {
- this.state.splash ?
+ )}
+ {this.state.splash ?