MyPage.js 4.26 KB
import React, {useEffect, useState, useCallback} from 'react';
import {ScrollView, Text, Alert} from 'react-native';
import {useDispatch, useSelector} from "react-redux";
import {BUTTON_COLOR} from "../constant";
import SquareButtonComponent from "../components/Base/SquareButtonComponent";
import {logoutSaga, updateUserInfoSaga} from "../reducers/user";
import TextInputComponent from "../components/Base/TextInputComponent";

const MyPage = (props) => {
    // 데이터
    const {navigation} = props;
    const {me} = useSelector(state => state.user);
    const [nickName, setNickName] = useState(me.nickName);

    // 함수
    const dispatch = useDispatch();
    const goToPay = () => {
        Alert.alert('테스트 앱은 결제 기능 미지원');
    };
    const onChangeNickName = useCallback((text) => {
        setNickName(text);
    }, []);
    const updateUserInfo = () => {
        dispatch({type: updateUserInfoSaga, data: {nickName}});
    };
    const logout = () => {
        dispatch({type: logoutSaga});
    };

    // 렌더링
    useEffect(() => {
        if (!me) {
            navigation.navigate('지도');
        }
    }, [me]);
    return (
        <ScrollView>
            <Text style={{
                marginTop: 80,
                marginBottom: 20,
                fontSize: 24,
                fontWeight: 'bold',
                textAlign: 'center',
                color: '#373737'
            }}>
                닉네임 {me.nickName}
            </Text>
            <TextInputComponent
                onChangeText={onChangeNickName}
                value={nickName}
                secureTextEntry={false}
                placeholder={'닉네임을 업데이트하세요'}
                rules={[
                    v => !!v || '닉네임 입력은 필수입니다',
                    v => (v && (v.trim() !== '')) || '닉네임 입력은 필수입니다',
                    v => (v && (v.length <= 10)) || '닉네임의 길이는 10자 이하입니다'
                ]}
                style1={{
                    marginLeft: 20,
                    marginRight: 20,
                    marginTop: 10,
                    marginBottom: 10,
                    padding: 15,
                }}
                style2={{
                    marginLeft: 35,
                    marginRight: 35,
                }}
            />
            <SquareButtonComponent
                onPress={updateUserInfo}
                text={'닉네임 업데이트'}
                style1={{
                    marginLeft: 20,
                    marginRight: 20,
                    marginTop: 10,
                    marginBottom: 10,
                    padding: 15,
                    borderRadius: 3,
                    backgroundColor: BUTTON_COLOR
                }}
                style2={{
                    fontSize: 16,
                    fontWeight: 'bold',
                    textAlign: 'center',
                    color: '#ffffff'
                }}
            />
            <SquareButtonComponent
                onPress={logout}
                text={'로그아웃'}
                style1={{
                    marginLeft: 20,
                    marginRight: 20,
                    marginTop: 10,
                    marginBottom: 10,
                    padding: 15,
                    borderRadius: 3,
                    backgroundColor: BUTTON_COLOR
                }}
                style2={{
                    fontSize: 16,
                    fontWeight: 'bold',
                    textAlign: 'center',
                    color: '#ffffff'
                }}
            />
            <SquareButtonComponent
                onPress={goToPay}
                text={'대여 및 결제 페이지'}
                style1={{
                    marginLeft: 20,
                    marginRight: 20,
                    marginTop: 10,
                    marginBottom: 10,
                    padding: 15,
                    borderRadius: 3,
                    backgroundColor: BUTTON_COLOR
                }}
                style2={{
                    fontSize: 16,
                    fontWeight: 'bold',
                    textAlign: 'center',
                    color: '#ffffff'
                }}
            />
        </ScrollView>
    )
};

export default MyPage;