Skip to content

Commit

Permalink
fix: 删除登陆hooks多余代码 & 文档增加目录结构
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenlingasMx committed Apr 27, 2023
1 parent 0b4abad commit 84c2c7d
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 60 deletions.
19 changes: 11 additions & 8 deletions HelloWorld/src/hooks/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,29 @@ import Global from '../global';
import conf from '../config';
import { useSelector, useDispatch } from 'react-redux'
// 登录
export const login = ({ config = {}, update, remember }) => {
export const login = ({ mutationKey }) => {
const dispatch = useDispatch()
const mutation = useMutation({
mutationKey,
mutationFn: userLogin,
onSuccess: async data => {
if (data?.token && data?.data) {
await AsyncStorage.setItem('token', data.token);
if (remember) {
await AsyncStorage.setItem('cachLoginName', formData.loginName);
await AsyncStorage.setItem('cachPassword', formData.password);
}
await AsyncStorage.setItem('userData', JSON.stringify(data.data));
update({ token: data.token, userData: data.data });
dispatch({
type: "global/update",
payload: {
token: data.token,
userData: JSON.stringify(data.data)
}
})
if (Global.navigation) {
Global.navigation.replace('Tab');
}
} else if (data && data.message) {
Alert.alert(`Login failed - ${data.error}`, data.message);
}
},
...config,
});
return mutation;
};
Expand Down Expand Up @@ -65,7 +68,7 @@ export const useAuthToken = () => {
dispatch({
type: "global/update",
payload: {
authState: true,
authState: true,
token: null
}
})
Expand Down
19 changes: 4 additions & 15 deletions HelloWorld/src/pages/SignIn/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,17 @@ import { login } from '../../hooks/users'

const SigninScreen = ({
navigation,
update,
}) => {
const [store, setStore] = useState({
hostType: '',
remember: false,
formData: {
username: 'admin',
password: 'admin!',
},
})
const { hostType, remember, formData } = store
const { hostType, formData } = store

const { mutate, isLoading } = login({
update,
formData,
remember
})
const { mutate, isLoading } = login({ mutationKey: ['userLogin', formData] })

useEffect(() => {
if (navigation && Global) {
Expand All @@ -46,7 +40,7 @@ const SigninScreen = ({
}
};

const loginIn = () => mutate?.({ ...formData })
const loginIn = () => mutate?.(formData)

return (
<SafeAreaView style={styles.block}>
Expand Down Expand Up @@ -104,12 +98,7 @@ const SigninScreen = ({
);
}

export default connect(
({}) => ({}),
({ global }) => ({
update: global.update
}),
)(SigninScreen);
export default SigninScreen

const styles = StyleSheet.create({
block: {
Expand Down
10 changes: 4 additions & 6 deletions HelloWorld/src/pages/TransportHome/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import React, { Component } from 'react';
import { SafeAreaView } from 'react-native';

export default class MyScreen extends Component {
render() {
return (
<SafeAreaView></SafeAreaView>
);
}
export default function MyScreen() {
return (
<SafeAreaView></SafeAreaView>
);
}
49 changes: 49 additions & 0 deletions template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,55 @@ git commit -m "Keep calm and commit"
└──@react-native-community/masked-view
```

## 目录结构
```
├── Gemfile
├── README.md
├── __tests__
│   └── App-test.js
├── _bundle
│   └── config
├── _node-version
├── android
│   ├── app
│   ├── build.gradle
│   ├── gradle
│   ├── gradle.properties
│   ├── gradlew
│   ├── gradlew.bat
│   └── settings.gradle
├── app.json
├── babel.config.js
├── index.js
├── ios
│   ├── HelloWorld
│   ├── HelloWorld.xcodeproj
│   ├── HelloWorld.xcworkspace
│   ├── HelloWorldTests
│   ├── Podfile
│   ├── Podfile.lock
│   ├── Pods
│   ├── _xcode.env
│   └── build
├── jsconfig.json
├── metro.config.js
├── mocker
│   ├── index.js
│   └── user.mock.js
├── package.json
└── src
├── App.js
├── components
├── config.js
├── global.js
├── hooks
├── models
├── pages
├── routes
├── services
└── utils
```

## Links

- [React Native upgrade helper](https://react-native-community.github.io/upgrade-helper/)
Expand Down
19 changes: 11 additions & 8 deletions template/template/src/hooks/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,29 @@ import Global from '../global';
import conf from '../config';
import { useSelector, useDispatch } from 'react-redux'
// 登录
export const login = ({ config = {}, update, remember }) => {
export const login = ({ mutationKey }) => {
const dispatch = useDispatch()
const mutation = useMutation({
mutationKey,
mutationFn: userLogin,
onSuccess: async data => {
if (data?.token && data?.data) {
await AsyncStorage.setItem('token', data.token);
if (remember) {
await AsyncStorage.setItem('cachLoginName', formData.loginName);
await AsyncStorage.setItem('cachPassword', formData.password);
}
await AsyncStorage.setItem('userData', JSON.stringify(data.data));
update({ token: data.token, userData: data.data });
dispatch({
type: "global/update",
payload: {
token: data.token,
userData: JSON.stringify(data.data)
}
})
if (Global.navigation) {
Global.navigation.replace('Tab');
}
} else if (data && data.message) {
Alert.alert(`Login failed - ${data.error}`, data.message);
}
},
...config,
});
return mutation;
};
Expand Down Expand Up @@ -65,7 +68,7 @@ export const useAuthToken = () => {
dispatch({
type: "global/update",
payload: {
authState: true,
authState: true,
token: null
}
})
Expand Down
21 changes: 5 additions & 16 deletions template/template/src/pages/SignIn/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,21 @@ import Global from '../../global';
import Footer from '../../components/Footer';
import { logoLight } from '../../components/icons/signin';
import conf from '../../config';
import { useLogin } from '../../hooks/users'
import { login } from '../../hooks/users'

const SigninScreen = ({
navigation,
update,
}) => {
const [store, setStore] = useState({
hostType: '',
remember: false,
formData: {
username: 'admin',
password: 'admin!',
},
})
const { hostType, remember, formData } = store
const { hostType, formData } = store

const { mutate, isLoading } = useLogin({
update,
formData,
remember
})
const { mutate, isLoading } = login({ mutationKey: ['userLogin', formData] })

useEffect(() => {
if (navigation && Global) {
Expand All @@ -46,7 +40,7 @@ const SigninScreen = ({
}
};

const loginIn = () => mutate?.({ ...formData })
const loginIn = () => mutate?.(formData)

return (
<SafeAreaView style={styles.block}>
Expand Down Expand Up @@ -104,12 +98,7 @@ const SigninScreen = ({
);
}

export default connect(
({ }) => ({}),
({ global }) => ({
update: global.update
}),
)(SigninScreen);
export default SigninScreen

const styles = StyleSheet.create({
block: {
Expand Down
12 changes: 5 additions & 7 deletions template/template/src/pages/TransportHome/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import React, { Component } from 'react';
import { View, SafeAreaView } from 'react-native';
import { SafeAreaView } from 'react-native';

export default class MyScreen extends Component {
render() {
return (
<SafeAreaView></SafeAreaView>
);
}
export default function MyScreen() {
return (
<SafeAreaView></SafeAreaView>
);
}

0 comments on commit 84c2c7d

Please sign in to comment.