-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
104 lines (101 loc) · 3.61 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import React, { useEffect, useState } from 'react';
import { useForm, Controller } from 'react-hook-form';
import { setString as cb } from 'expo-clipboard';
import { Button, Box, Input, NativeBaseProvider, StatusBar, Text, VStack } from 'native-base';
import axios from 'axios';
export default function App() {
const [ urlList, setUrlList ] = useState([]);
const [ urlSubmitted, setUrlSubmitted ] = useState('');
const [ showBtn, setShowBtn ] = useState(false);
const { control, handleSubmit, formState: {errors, isValid}, reset } = useForm({mode: 'onBlur'});
const onSubmit = data => {
setUrlSubmitted(data.inputUrl);
};
useEffect(() => {
if (urlSubmitted !== '') {
const fetch = async () => {
try {
await axios.post('https://api.tinyurl.com/create',
{
'url': urlSubmitted
},
{
headers: {
Accept: 'application/json',
Authorization: 'Bearer <token>',
'Content-Type': 'application/json'
}
}
).then((response) => {
const newUrlList = urlList.slice();
newUrlList.push(response.data.data.tiny_url);
setUrlList(newUrlList);
setShowBtn(true);
})
} catch (e) {
console.error(e.message);
}
}
fetch();
} else {
setShowBtn(false);
}
}, [urlSubmitted]);
return (
<NativeBaseProvider>
<VStack height='full' alignItems='center'>
<Box width='100%' alignItems='center' bg='black'>
<Text marginTop='3%' fontSize='2xl' color='white' padding='2'>url-shortener.js</Text>
</Box>
<VStack flex={1} w='100%' alignItems='center' bg='#2C2F33' space={2}>
<Box w='90%' justifyContent='space-between' marginTop='2%'>
<VStack space={2}>
<Controller
control={control}
name='inputUrl'
render={({field: {onChange, value, onBlur}}) => (
<Input
size='2xl'
borderColor='coolGray.500'
color='white'
placeholder='Enter your url here'
value={value}
onblur={onBlur}
onChangeText={value => onChange(value)}
keyboardType='email-address'
onEndEditing={handleSubmit(onSubmit)}
/>
)}
/>
<VStack space={2}>
<Button size='lg' onPress={handleSubmit(onSubmit)}>Submit</Button>
{
showBtn ? (
<Button size='lg' colorScheme='red' onPress={() => {
setUrlSubmitted('')
setUrlList([]);
setShowBtn(false);
reset({
inputUrl: ''
});
}}>Clear</Button>
) : null
}
</VStack>
</VStack>
<StatusBar barStyle='light-content'/>
</Box>
<Box flex={1} width='90%' borderWidth='1' borderColor='coolGray.500' rounded='lg' alignItems='center' marginBottom='5%'>
<VStack width="98%" space={2} padding={2}>
{
urlList.map((url, i) => {
return <Button size='md' colorScheme='green' onPress={() => {cb(url)}} key={i}>{url}</Button>
})
}
</VStack>
</Box>
</VStack>
</VStack>
</NativeBaseProvider>
);
}