Novinaldi

Formmahasiswa-React Native

Jan 9th, 2022
1,218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import {
  3.     StyleSheet,
  4.     Text, TextInput, Touchable, TouchableOpacity, View, Alert
  5. } from 'react-native';
  6. import LinearGradient from 'react-native-linear-gradient';
  7. import Icon from 'react-native-vector-icons/FontAwesome5';
  8.  
  9.  
  10. class Formmahasiswa extends Component {
  11.     constructor(props) {
  12.         super(props);
  13.         this.state = {
  14.             nobp: '', nama: '', alamat: '', telp: '',
  15.         };
  16.     }
  17.     saveDataMhs = () => {
  18.         fetch('http://192.168.229.65/api-server/public/mahasiswa', {
  19.             method: 'POST',
  20.             headers: {
  21.                 Accept: 'application/json',
  22.                 'Content-Type': 'application/json'
  23.             },
  24.             body: JSON.stringify({
  25.                 mhsnobp: this.state.nobp,
  26.                 mhsnama: this.state.nama,
  27.                 mhsalamat: this.state.alamat,
  28.                 mhstelp: this.state.telp,
  29.             })
  30.         })
  31.             .then((response) => response.json())
  32.             .then((json) => {
  33.                 (json.status == 201) ? Alert.alert('Sukses', 'data mahasiswa berhasil disimpan') : '';
  34.             })
  35.             .catch((err) => console.log(err))
  36.             .finally(() => {
  37.                 this.setState({ nobp: '' });
  38.                 this.setState({ nama: '' });
  39.                 this.setState({ alamat: '' });
  40.                 this.setState({ telp: '' });
  41.             })
  42.     }
  43.     render() {
  44.         return (
  45.             <View style={style.container}>
  46.                 <LinearGradient colors={['#200122', '#6f0000']} style={style.header} start={{ x: 0, y: 0 }} end={{ x: 1, y: 0 }}>
  47.  
  48.                     <TouchableOpacity
  49.                         onPress={() => this.props.navigation.goBack()}
  50.                     >
  51.                         <Text style={{ paddingLeft: 15 }}>
  52.                             <Icon name="arrow-circle-left" size={30} color="#fff" />
  53.                         </Text>
  54.                     </TouchableOpacity>
  55.  
  56.                     <Text style={style.textHeader}>
  57.                         Form Mahasiswa Mahasiswa
  58.                     </Text>
  59.                     <Text></Text>
  60.                 </LinearGradient>
  61.  
  62.                 <View style={style.isi}>
  63.                     <View style={{
  64.                         flex: 1, paddingHorizontal: 10,
  65.                         marginTop: 20
  66.                     }}>
  67.                         <TextInput
  68.                             value={this.state.nobp}
  69.                             onChangeText={(value) => this.setState({ nobp: value })}
  70.                             placeholder='Inputkan No.BP'
  71.                             placeholderTextColor='#200122'
  72.                             style={style.textInput}
  73.                             keyboardType="number-pad"
  74.                         />
  75.                         <TextInput
  76.                             value={this.state.nama}
  77.                             onChangeText={(value) => this.setState({ nama: value })}
  78.                             placeholder='Inputkan Nama Lengkap'
  79.                             placeholderTextColor='#200122'
  80.                             style={style.textInput}
  81.                         />
  82.                         <TextInput
  83.                             value={this.state.alamat}
  84.                             onChangeText={(value) => this.setState({ alamat: value })}
  85.                             placeholder='Inputkan Alamat'
  86.                             placeholderTextColor='#200122'
  87.                             style={style.textInput}
  88.                         />
  89.                         <TextInput
  90.                             value={this.state.telp}
  91.                             onChangeText={(value) => this.setState({ telp: value })}
  92.                             placeholder='Inputkan No.Telp'
  93.                             placeholderTextColor='#200122'
  94.                             style={style.textInput}
  95.                             keyboardType="number-pad"
  96.                         />
  97.  
  98.                         <TouchableOpacity style={style.tombolSimpan}
  99.                             onPress={() => this.saveDataMhs()}
  100.                         >
  101.                             <Text>
  102.                                 <Icon name="save" size={30} color="#fff" />
  103.                             </Text>
  104.                             <Text
  105.                                 style={{ color: '#fff', fontSize: 18, fontWeight: 'bold', paddingLeft: 10 }}
  106.                             >Simpan Data</Text>
  107.                         </TouchableOpacity>
  108.                     </View>
  109.  
  110.                 </View>
  111.             </View>
  112.         );
  113.     }
  114. }
  115.  
  116. export default Formmahasiswa;
  117.  
  118. const style = StyleSheet.create({
  119.     container: { flex: 1, backgroundColor: '#fff' },
  120.     header: {
  121.         flex: 0.5,
  122.         backgroundColor: 'red',
  123.         borderBottomLeftRadius: 60,
  124.         borderBottomRightRadius: 60,
  125.         justifyContent: 'space-between',
  126.         alignItems: 'center',
  127.         flexDirection: 'row',
  128.         elevation: 10
  129.     },
  130.     textHeader: {
  131.         color: '#fff',
  132.         fontSize: 22,
  133.         fontWeight: 'bold'
  134.     },
  135.     isi: {
  136.         backgroundColor: '#fff',
  137.         flex: 3,
  138.     },
  139.     textInput: {
  140.         borderWidth: 2,
  141.         borderColor: '#200122',
  142.         paddingHorizontal: 20,
  143.         borderRadius: 10,
  144.         backgroundColor: '#fff',
  145.         elevation: 5,
  146.         marginVertical: 10
  147.     },
  148.     tombolSimpan: {
  149.         backgroundColor: '#200122',
  150.         flexDirection: 'row',
  151.         borderRadius: 10,
  152.         paddingVertical: 15,
  153.         justifyContent: 'center',
  154.         alignItems: 'center',
  155.  
  156.     }
  157. })
Advertisement
Add Comment
Please, Sign In to add comment