Skip to content

Commit

Permalink
change slice
Browse files Browse the repository at this point in the history
  • Loading branch information
ngocle2497 committed Jul 11, 2022
1 parent 5d014be commit 91226e9
Show file tree
Hide file tree
Showing 20 changed files with 68 additions and 74 deletions.
2 changes: 1 addition & 1 deletion template/src/app/common/method/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const execFunc = <Fn extends (...args: any[]) => any>(
export const isIos = Platform.OS === 'ios';

export const logout = () => {
dispatch(appActions.onLogout());
dispatch(appActions.logout());
remove(STORAGE_KEY_TOKEN);
};

Expand Down
16 changes: 8 additions & 8 deletions template/src/app/common/redux/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ export const takeLatestListeners =
listenerApi.cancelActiveListeners();
await listenerApi.delay(15);
if (withLoading) {
listenerApi.dispatch(appActions.onStartProcess());
listenerApi.dispatch(appActions.startProcess());
}
await startListeningOption.effect(action, listenerApi);
if (withLoading) {
listenerApi.dispatch(appActions.onEndProcess());
listenerApi.dispatch(appActions.endProcess());
}
},
});
Expand All @@ -32,11 +32,11 @@ export const takeLeadingListeners =
effect: async (action, listenerApi) => {
listenerApi.unsubscribe();
if (withLoading) {
listenerApi.dispatch(appActions.onStartProcess());
listenerApi.dispatch(appActions.startProcess());
}
await startListeningOption.effect(action, listenerApi);
if (withLoading) {
listenerApi.dispatch(appActions.onEndProcess());
listenerApi.dispatch(appActions.endProcess());
}
listenerApi.subscribe();
},
Expand All @@ -52,11 +52,11 @@ export const debounceListeners =
listenerApi.cancelActiveListeners();
await listenerApi.delay(msDuration);
if (withLoading) {
listenerApi.dispatch(appActions.onStartProcess());
listenerApi.dispatch(appActions.startProcess());
}
await startListeningOption.effect(action, listenerApi);
if (withLoading) {
listenerApi.dispatch(appActions.onEndProcess());
listenerApi.dispatch(appActions.endProcess());
}
},
});
Expand All @@ -70,11 +70,11 @@ export const throttleListeners =
effect: async (action, listenerApi) => {
listenerApi.unsubscribe();
if (withLoading) {
listenerApi.dispatch(appActions.onStartProcess());
listenerApi.dispatch(appActions.startProcess());
}
await startListeningOption.effect(action, listenerApi);
if (withLoading) {
listenerApi.dispatch(appActions.onEndProcess());
listenerApi.dispatch(appActions.endProcess());
}
await listenerApi.delay(msDuration);
listenerApi.subscribe();
Expand Down
2 changes: 1 addition & 1 deletion template/src/app/common/yup-validate/login.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FormLoginType } from '@model/login';
import { FormLoginType } from '@model/authentication';
import * as yup from 'yup';

export const loginValidation: yup.SchemaOf<FormLoginType> = yup.object().shape({
Expand Down
2 changes: 1 addition & 1 deletion template/src/app/config/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface ParamsNetwork {

export enum SLICE_NAME {
APP = 'APP_',
LOGIN = 'LOGIN_',
AUTHENTICATION = 'AUTHENTICATION_',
}

export type ValidateMessageObject = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Button } from 'react-native';
import { FormProvider, useForm } from 'react-hook-form';

import { yupResolver } from '@hookform/resolvers/yup';
import { FormLoginType } from '@model/login';
import { FormLoginType } from '@model/authentication';
import { loginValidation } from '@validate/login';

import { Input } from './input';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useController, useFormContext } from 'react-hook-form';
import { CustomOmit } from '@common';
import { HelperText, TextField } from '@components';
import { InputFlatProps } from '@components/text-field/components/flat/type';
import { FormLoginType } from '@model/login';
import { FormLoginType } from '@model/authentication';

interface InputProps<T extends Record<string, any>>
extends CustomOmit<InputFlatProps, 'nameTrigger'>,
Expand Down
4 changes: 2 additions & 2 deletions template/src/app/features/un-authentication/login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
Wallpaper,
} from '@components';
import { useAnimatedState } from '@hooks';
import { FormLoginType } from '@model/login';
import { FormLoginType } from '@model/authentication';
import { appActions } from '@redux-slice';

import { FormLogin } from './components/form-login';
Expand All @@ -45,7 +45,7 @@ const LoginComponent = () => {

// function
const onSubmit = (data: FormLoginType) => {
dispatch(appActions.onSetAppTheme('dark'));
dispatch(appActions.setAppTheme('dark'));
Alert.alert(JSON.stringify(data));
};

Expand Down
2 changes: 1 addition & 1 deletion template/src/app/features/un-authentication/login/type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FormLoginType } from '@model/login';
import { FormLoginType } from '@model/authentication';

export interface FormLoginProps {
onSubmit: (data: FormLoginType) => void;
Expand Down
2 changes: 1 addition & 1 deletion template/src/app/library/networking/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ AxiosInstance.interceptors.response.use(
if (newToken === null) {
return Promise.reject(error);
}
dispatch(appActions.onSetToken(newToken));
dispatch(appActions.setToken(newToken));
originalRequest.headers[tokenKeyHeader] = newToken;
return AxiosInstance(originalRequest);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export type FormLoginType = {
password: string;
};

export interface LoginState {
export interface AuthenticationState {
loading: boolean;
count: number;
}
2 changes: 1 addition & 1 deletion template/src/app/navigation/app-navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const AppContainer = () => {

// effect
useEffect(() => {
dispatch(appActions.onLoadApp());
dispatch(appActions.startLoadApp());
}, []);

useEffect(() => {
Expand Down
18 changes: 9 additions & 9 deletions template/src/app/redux/action-slice/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,31 @@ const appSlice = createSlice({
name: SLICE_NAME.APP,
initialState: initialAppState,
reducers: {
onSetInternet: (state, { payload }: PayloadAction<boolean>) => {
setInternetState: (state, { payload }: PayloadAction<boolean>) => {
state.internetState = payload;
},
onSetToken: (state, { payload }: PayloadAction<string>) => {
setToken: (state, { payload }: PayloadAction<string>) => {
state.token = payload;
},
onSetAppProfile: (state, { payload }: PayloadAction<unknown>) => {
setAppProfile: (state, { payload }: PayloadAction<unknown>) => {
state.profile = payload;
},
onSetAppTheme: (state, { payload }: PayloadAction<ThemeType>) => {
setAppTheme: (state, { payload }: PayloadAction<ThemeType>) => {
state.theme = payload;
},
onLoadApp: state => {
startLoadApp: state => {
state.loadingApp = true;
},
onLoadAppEnd: state => {
endLoadApp: state => {
state.loadingApp = false;
},
onStartProcess: state => {
startProcess: state => {
state.showDialog = true;
},
onEndProcess: state => {
endProcess: state => {
state.showDialog = false;
},
onLogout: state => {
logout: state => {
state.token = undefined;
state.profile = {};
},
Expand Down
29 changes: 29 additions & 0 deletions template/src/app/redux/action-slice/authentication.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { SLICE_NAME } from '@config/type';
import { AuthenticationState } from '@model/authentication';
import * as Action from '@redux-action-type/authentication';
import { createAction, createSlice } from '@reduxjs/toolkit';

const initialState: AuthenticationState = {
loading: false,
};
const authenticationSlice = createSlice({
name: SLICE_NAME.AUTHENTICATION,
initialState: initialState,
reducers: {
reset: () => initialState,
},
});

const login = createAction(
Action.LOGIN,
(body: any, onSucceeded: () => void, onFailure: (msg: string) => void) => ({
payload: {
body,
onSucceeded,
onFailure,
},
}),
);
export const authenticationActions = { ...authenticationSlice.actions, login };
export const authenticationReducer = authenticationSlice.reducer;
2 changes: 1 addition & 1 deletion template/src/app/redux/action-slice/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './app';
export * from './login';
export * from './authentication';
34 changes: 0 additions & 34 deletions template/src/app/redux/action-slice/login.ts

This file was deleted.

3 changes: 3 additions & 0 deletions template/src/app/redux/action-type/authentication.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { SLICE_NAME } from '@config/type';

export const LOGIN = SLICE_NAME.AUTHENTICATION + 'LOGIN';
3 changes: 0 additions & 3 deletions template/src/app/redux/action-type/login.ts

This file was deleted.

8 changes: 4 additions & 4 deletions template/src/app/redux/listener/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ import { loadString } from '@utils/storage';
import { appActions } from '../action-slice/app';

takeLatestListeners()({
actionCreator: appActions.onLoadApp,
actionCreator: appActions.startLoadApp,
effect: async (_, listenerApi) => {
const appTheme = loadString(STORAGE_KEY_APP_THEME);
const token = loadString(STORAGE_KEY_TOKEN);
if (typeof token === 'string') {
listenerApi.dispatch(appActions.onSetToken(token));
listenerApi.dispatch(appActions.setToken(token));
}

if (
typeof appTheme === 'string' &&
checkKeyInObject(MyAppTheme, appTheme)
) {
listenerApi.dispatch(appActions.onSetAppTheme(appTheme as ThemeType));
listenerApi.dispatch(appActions.setAppTheme(appTheme as ThemeType));
}
listenerApi.dispatch(appActions.onLoadAppEnd());
listenerApi.dispatch(appActions.endLoadApp());
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { handleErrorResponse } from '@common'
import { takeLatestListeners } from '@listener';
import { ApiConstants, NetWorkService } from '@networking';

import { loginActions } from '../action-slice/login';
import { authenticationActions } from '../action-slice/authentication';

takeLatestListeners(true)({
actionCreator: loginActions.onLogin,
actionCreator: authenticationActions.login,
effect: async (action, listenerApi) => {
const { body } = action.payload;
console.log({ body });
Expand Down
2 changes: 1 addition & 1 deletion template/src/app/redux/listener/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { listenerMiddleware } from '@listener';

export * from './login';
export * from './authentication';
export * from './app';

0 comments on commit 91226e9

Please sign in to comment.