App.js 2.9 KB
import React,{ Component } from 'react'
import { StyleSheet, Text, View, AppRegistry,Linking } from 'react-native';
import App from './components/Navigation/Navigation'
import {post,all } from 'axios'
import withDeepLinking,{ setTopLevelNavigator } from './withDeepLinking';
import firebase from '@react-native-firebase/dynamic-links'
import AsyncStorage from '@react-native-community/async-storage';
import Listen from './components/ListenonBackground'
import { SERVER } from './common/servername';
import { addNavigationHelpers } from 'react-navigation';

class RootApp extends Component {
  constructor(props) {
    super(props);

    this.state = { 
      loaded: false,
      notification: null,
      messageText: ''
    };
    
  }
  async componentDidMount() {
        try {
        let url = await firebase().getInitialLink();
        //console.log(url.url)
            if(url && url.url.indexOf("sanggatalk.io/app/coupon") > 0) {
              AsyncStorage.getItem("user",(err,res) => {
                if(res) {
                  console.log({UserId:JSON.parse(res).id,id:url.url.split('?params=')[1]})
                  post(`http://${SERVER}/coupons/private/gift`,{UserId:JSON.parse(res).id,id:url.url.split('?params=')[1]}).then(Res => {
                    if(Res.data.success) {
                      alert("쿠폰이 성공적으로 지급되었습니다.")
                    }
                  })
                }
                else {
                  AsyncStorage.setItem("present",JSON.stringify(true))
                }
              })
            }
            if(url && url.url.indexOf("sanggatalk.io/app/stamp") > 0) {
              AsyncStorage.getItem("user",(err,res) => {
                if(res) {
                  const num = parseInt(url.url.split('?params=')[1].split(',')[2])
                  
                  all([...Array(num)].map((e,i) => post(`http://${SERVER}/stamps/users`,{UserId:JSON.parse(res).id,StampId:url.url.split('?params=')[1].split(',')[1],StoreId:url.url.split('?params=')[1].split(',')[0]}))).then(res => {
                    if(res[0].data.success) {
                      alert("스탬프가 성공적으로 지급되었습니다.")
                    }
                  })
                }
                else {
                  AsyncStorage.setItem("present",JSON.stringify(true))
                }
              })
            }
        }
        catch(err) {
            console.log(err)
        }
        
      }
  render() {
    const { loaded } = this.state;
    return (
      <>
        <App uriPrefix='sanggatalk://' ref={
            navigatorRef => {
                setTopLevelNavigator(navigatorRef);
            }}/> 
      </>
    );
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

AppRegistry.registerComponent('main', () => RootApp);


export default RootApp