Skip to content

Commit

Permalink
feat: refreshing transaction & auth admin
Browse files Browse the repository at this point in the history
  • Loading branch information
Angga Maulana committed Apr 9, 2023
1 parent 4d7aa50 commit a43e1ff
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 35 deletions.
45 changes: 23 additions & 22 deletions components/Organizms/Table/Table.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from "react";
import PropTypes from "prop-types";
import { FiRefreshCcw } from "react-icons/fi";

// components
export default function Table(props) {
const { color, children, total, currentPage, lastPage, prev, next } = props
const { color, children, total, currentPage, lastPage, prev, next, refresh, refreshing } = props

return (
<>
Expand Down Expand Up @@ -43,28 +44,28 @@ export default function Table(props) {
</div>
<div className="flex items-center space-x-2">
<div className="relative">
<button className="relative z-0 inline-flex text-sm rounded-md shadow-sm focus:ring-accent-500 focus:border-accent-500 hover:bg-gray-50 focus:z-10 focus:outline-none focus:ring-1">
<span className="relative inline-flex items-center px-3 py-3 space-x-2 text-sm font-medium text-gray-600 bg-white border border-gray-300 rounded-md sm:py-2">
<div>
<svg
xmlns="http://www.w3.org/2000/svg"
className="w-3 h-3"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={2}
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M3 4a1 1 0 011-1h16a1 1 0 011 1v2.586a1 1 0 01-.293.707l-6.414 6.414a1 1 0 00-.293.707V17l-4 4v-6.586a1 1 0 00-.293-.707L3.293 7.293A1 1 0 013 6.586V4z"
/>
<button type="button" onClick={refresh} disabled={refreshing} className="relative z-0 inline-flex text-sm rounded-md shadow-sm focus:ring-accent-500 focus:border-accent-500 hover:bg-gray-50 focus:z-10 focus:outline-none focus:ring-1">
{
refreshing ? (
<span className="bg-blueGray-600 relative inline-flex items-center px-2 py-1 space-x-1 text-sm font-medium text-gray-600 border border-gray-300 rounded-md sm:py-2">
<div>
<svg xmlns="http://www.w3.org/2000/svg" className="animate-spin h-9 w-9" viewBox="0 0 25 25" fill="none"
stroke="currentColor">
<path d="M4.5 12.5C4.5 16.9183 8.08172 20.5 12.5 20.5C16.9183 20.5 20.5 16.9183 20.5 12.5C20.5 8.08172 16.9183 4.5 12.5 4.5" stroke="#fafafa" strokeWidth="1.2"/>
</svg>
</div>
<div className="hidden sm:block">
Filters
</div>
</span>
</div>
</span>
) : (
<span className="relative inline-flex items-center px-3 py-3 space-x-2 text-sm font-medium text-gray-600 bg-white border border-gray-300 rounded-md sm:py-2">
<div>
<FiRefreshCcw/>
</div>
<div className="hidden sm:block">
Refresh
</div>
</span>
)
}
</button>
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions pages/auth/adminlogin.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export default function AdminLogin() {
onSubmit: value => {
const { email, password } = value
let data = new FormData();
data.append("member_id",email)
data.append("username",email)
data.append("password",password)

dispatch(AuthActions.doLoginRequest({
dispatch(AuthActions.doLoginAdminRequest({
data: data,
message: (msg) => alert.success(msg),
error: (msg) => alert.error(msg),
Expand Down Expand Up @@ -138,7 +138,7 @@ export default function AdminLogin() {
</div>

<div className="text-center mt-6">
<Button label="Login" type="submit" disabled={auth?.login?.fetching} isFetching={auth?.login?.fetching} />
<Button label="Login" type="submit" disabled={auth?.admin_login?.fetching} isFetching={auth?.admin_login?.fetching} />
</div>
<div className="flex flex-row align-center">
<hr className="w-full mt-3 border-b-1 border-blueGray-300" />
Expand Down
9 changes: 8 additions & 1 deletion pages/member/bonus/level.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export default function Level() {
dispatch(BonusActions.doGetBonusLevelRequest())
},[])

const refreshBonusLevel = () => {
dispatch(BonusActions.doPostTrigerBonusLevelRequest());
}

return (
<div className="flex flex-wrap">
<div className="w-full lg:w-12/12 px-4">
Expand All @@ -50,7 +54,10 @@ export default function Level() {
</div>
</div>
<div className="flex px-4 lg:px-10 py-10 pt-0 bg-white">
<Table color="light">
<Table
refreshing={state?.bonus_level?.fetching}
refresh={refreshBonusLevel}
color="light">
<table className="border-collapse items-center w-full bg-transparent">
<thead>
<tr>
Expand Down
2 changes: 2 additions & 0 deletions services/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const api = axios.create({

const URL = {
LOGIN: 'auth/login',
LOGIN_ADMIN: 'auth/login/admin',
REGISTER: 'auth/register',
PROFILE: 'profile',
CHECK_POSITION: 'check-position',
Expand All @@ -31,6 +32,7 @@ const URL = {
UPDATE_PAKET: 'profile/upgrade',
HISTORY_ORDER: 'profile/history/orders',
BONUS_LEVEL: 'bonus/level',
BONUS_LEVEL_FRESH: 'bonus/level/refresh',
BONUS_SPONSOR: 'bonus/sponsor',
BONUS_PAIRING: 'bonus/pairing',
TREE: 'profile/tree',
Expand Down
8 changes: 8 additions & 0 deletions store/actionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ const Types = {
POST_LOGIN_SUCCESS: 'POST_LOGIN_SUCCESS',
POST_LOGIN_FAILURE: 'POST_LOGIN_FAILURE',

POST_LOGIN_ADMIN_REQUEST: 'POST_LOGIN_ADMIN_REQUEST',
POST_LOGIN_ADMIN_SUCCESS: 'POST_LOGIN_ADMIN_SUCCESS',
POST_LOGIN_ADMIN_FAILURE: 'POST_LOGIN_ADMIN_FAILURE',

POST_REGISTER_REQUEST: 'POST_REGISTER_REQUEST',
POST_REGISTER_SUCCESS: 'POST_REGISTER_SUCCESS',
POST_REGISTER_FAILURE: 'POST_REGISTER_FAILURE',
Expand Down Expand Up @@ -122,6 +126,10 @@ const Types = {
GET_BONUS_PAIRING_SUCCESS: 'GET_BONUS_PAIRING_SUCCESS',
GET_BONUS_PAIRING_FAILURE: 'GET_BONUS_PAIRING_FAILURE',

POST_TRIGER_BONUS_LEVEL_REQUEST: 'POST_TRIGER_BONUS_LEVEL_REQUEST',
POST_TRIGER_BONUS_LEVEL_SUCCESS: 'POST_TRIGER_BONUS_LEVEL_SUCCESS',
POST_TRIGER_BONUS_LEVEL_FAILURE: 'POST_TRIGER_BONUS_LEVEL_FAILURE',

GET_ADMIN_ORDER_LIST_REQUEST: 'GET_ADMIN_ORDER_LIST_REQUEST',
GET_ADMIN_ORDER_LIST_SUCCESS: 'GET_ADMIN_ORDER_LIST_SUCCESS',
GET_ADMIN_ORDER_LIST_FAILURE: 'GET_ADMIN_ORDER_LIST_FAILURE',
Expand Down
30 changes: 30 additions & 0 deletions store/redux/authReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ export const AuthActions = {
error,
}),

doLoginAdminRequest: data => ({
type: Types.POST_LOGIN_ADMIN_REQUEST,
payload: data,
}),
doLoginAdminSuccess: data => ({
type: Types.POST_LOGIN_ADMIN_SUCCESS,
payload: data,
}),
doLoginAdminFailure: error => ({
type: Types.POST_LOGIN_ADMIN_FAILURE,
error,
}),

// register
doRegisterRequest: data => ({
type: Types.POST_REGISTER_REQUEST,
Expand Down Expand Up @@ -104,6 +117,7 @@ export const AuthActions = {

const initialState = {
login: {data: [], fetching: false, error: null},
admin_login: {data: [], fetching: false, error: null},
register: {data: [], fetching: false, error: null},
members: {data: [], fetching: false, error: null},
verif: {data: [], fetching: false, error: null},
Expand Down Expand Up @@ -134,6 +148,22 @@ const authReducer = (state = initialState, action) => {
...state,
login: {data: [], fetching: false, error: action.error},
};

case Types.POST_LOGIN_ADMIN_REQUEST:
return {
...state,
admin_login: {data: [], fetching: true, error: null},
};
case Types.POST_LOGIN_ADMIN_SUCCESS:
return {
...state,
admin_login: {data: action.payload, fetching: false, error: null},
};
case Types.POST_LOGIN_ADMIN_FAILURE:
return {
...state,
admin_login: {data: [], fetching: false, error: action.error},
};

// Register
case Types.POST_REGISTER_REQUEST:
Expand Down
31 changes: 31 additions & 0 deletions store/redux/bonusReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,28 @@ export const BonusActions = {
doGetBonusPairingFailure: error => ({
type: Types.GET_BONUS_PAIRING_FAILURE,
error,
}),

doPostTrigerBonusLevelRequest: () => ({
type: Types.POST_TRIGER_BONUS_LEVEL_REQUEST,
}),
doPostTrigerBonusLevelSuccess: data => {
return ({
type: Types.POST_TRIGER_BONUS_LEVEL_SUCCESS,
payload: data,
})
},
doPostTrigerBonusLevelFailure: error => ({
type: Types.POST_TRIGER_BONUS_LEVEL_FAILURE,
error,
})
}

const initialState = {
sponsor: {data: [], fetching: false, error: null},
level: {data: [], fetching: false, error: null},
pairing: {data: [], fetching: false, error: null},
bonus_level: {data: [], fetching: false, error: null},
};

const bonusReducer = (state = initialState, action) => {
Expand Down Expand Up @@ -99,6 +114,22 @@ const bonusReducer = (state = initialState, action) => {
...state,
pairing: {data: [], fetching: false, error: action.error},
};

case Types.POST_TRIGER_BONUS_LEVEL_REQUEST:
return {
...state,
bonus_level: {data: [], fetching: true, error: null},
};
case Types.POST_TRIGER_BONUS_LEVEL_SUCCESS:
return {
...state,
bonus_level: {data: action.payload, fetching: false, error: null},
};
case Types.POST_TRIGER_BONUS_LEVEL_FAILURE:
return {
...state,
bonus_level: {data: [], fetching: false, error: action.error},
};
default:
return state;
}
Expand Down
40 changes: 35 additions & 5 deletions store/sagas/authSagas.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,36 @@ function* doLogin(data) {
}else{
data?.payload?.error("Username or password invalid!")
}
yield put(AuthActions.doLoginFailure(error));
yield put(AuthActions.doLoginFailure(error.message));
}
}

function* doLoginAdmin(data) {
try {
const {payload} = data;
const response = yield call(authorization.post, URL.LOGIN_ADMIN, payload?.data);

remove('logedin')
api.defaults.headers.common.Authorization = `Bearer ${response?.data?.data?.token}`;
yield all([
put(AuthActions.doLoginAdminSuccess(response.data)),
put(ProfileActions.doGetProfileRequest())
])

set('logedin',response?.data?.data?.token)
Cookies.set("logedin",response?.data?.data?.token)
payload?.message("Login SuccessFuly!")

setTimeout(() => {
payload?.navigate();
},700);
} catch (error) {
if(error?.response?.data?.message){
data?.payload?.error(error?.response?.data?.message)
}else{
data?.payload?.error("Username or password invalid!")
}
yield put(AuthActions.doLoginAdminFailure(error.message));
}
}

Expand Down Expand Up @@ -61,7 +90,7 @@ function* doRegister(data) {
data?.payload?.error("Register error")
}

yield put(AuthActions.doRegisterFailure(error));
yield put(AuthActions.doRegisterFailure(error.message));
}
}

Expand All @@ -87,7 +116,7 @@ function* doAddNewMembber(data) {
data?.payload?.error("Register error")
}

yield put(AuthActions.doAddNewMemberFailure(error));
yield put(AuthActions.doAddNewMemberFailure(error.message));
}
}

Expand All @@ -109,7 +138,7 @@ function* doCheckRef(data) {

yield put(AuthActions.doCheckRefSuccess(response.data));
} catch (error) {
yield put(AuthActions.doCheckRefFailure(error));
yield put(AuthActions.doCheckRefFailure(error.message));
}
}

Expand All @@ -121,12 +150,13 @@ function* doCheckPosition(data) {
data?.payload?.message("success",response?.data?.message)
} catch (error) {
data?.payload?.message("error", error?.response?.data?.message)
yield put(AuthActions.doCheckPositionFailure(error));
yield put(AuthActions.doCheckPositionFailure(error.message));
}
}

export default function* actionWatchAuth() {
yield takeLatest(Types.POST_LOGIN_REQUEST, doLogin);
yield takeLatest(Types.POST_LOGIN_ADMIN_REQUEST, doLoginAdmin);
yield takeLatest(Types.POST_REGISTER_REQUEST, doRegister);
yield takeLatest(Types.POST_NEW_MEMBER_REQUEST, doAddNewMembber);
yield takeLatest(Types.POST_VERIFICATION_REQUEST, doVerification);
Expand Down
23 changes: 19 additions & 4 deletions store/sagas/bonusSagas.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {call, put, takeLatest } from 'redux-saga/effects';
import {call, put, takeLatest, all } from 'redux-saga/effects';
import { api, URL } from "services/api";
import { Types } from "store/actionTypes";
import { BonusActions } from 'store/redux/bonusReducer';
Expand All @@ -10,7 +10,7 @@ function* doGetBonusLevel() {

yield put(BonusActions.doGetBonusLevelSuccess(data?.data));
} catch (error) {
yield put(BonusActions.doGetBonusLevelFailure(error));
yield put(BonusActions.doGetBonusLevelFailure(error.message));
}
}

Expand All @@ -20,7 +20,7 @@ function* doGetBonusSponsor() {

yield put(BonusActions.doGetBonusSponsorSuccess(data?.data));
} catch (error) {
yield put(BonusActions.doGetBonusSponsorFailure(error));
yield put(BonusActions.doGetBonusSponsorFailure(error.message));
}
}

Expand All @@ -30,12 +30,27 @@ function* doGetBonusPairing() {

yield put(BonusActions.doGetBonusPairingSuccess(data?.data));
} catch (error) {
yield put(BonusActions.doGetBonusPairingFailure(error));
yield put(BonusActions.doGetBonusPairingFailure(error.message));
}
}

function* doTrigerdBonusLevel() {
try {
const { data } = yield call(api.post, URL.BONUS_LEVEL_FRESH);

yield all([
put(BonusActions.doPostTrigerBonusLevelSuccess(data?.data)),
put(BonusActions.doGetBonusLevelRequest())
]);

} catch (error) {
yield put(BonusActions.doPostTrigerBonusLevelFailure(error.message));
}
}

export default function* actionWatchBonus() {
yield takeLatest(Types.GET_BONUS_SPONSOR_REQUEST, doGetBonusSponsor);
yield takeLatest(Types.GET_BONUS_LEVEL_REQUEST, doGetBonusLevel);
yield takeLatest(Types.GET_BONUS_PAIRING_REQUEST, doGetBonusPairing);
yield takeLatest(Types.POST_TRIGER_BONUS_LEVEL_REQUEST, doTrigerdBonusLevel);
}

0 comments on commit a43e1ff

Please sign in to comment.