Skip to content

Commit 4e5beb1

Browse files
committed
Added storage upload demo
1 parent 6bb2976 commit 4e5beb1

File tree

8 files changed

+164
-47
lines changed

8 files changed

+164
-47
lines changed

app/styles/app.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export const appStyles = StyleSheet.create({
1919
right: 0,
2020
top: 0,
2121
},
22+
description: {
23+
paddingHorizontal: 20,
24+
paddingVertical: 5
25+
},
2226
center: {
2327
justifyContent: 'center',
2428
alignItems: 'center',

app/views/Storage/Demos/Download.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React from 'react'
2+
3+
import {
4+
View,
5+
Text,
6+
} from 'react-native'
7+
8+
import env from '../../../../config/environment';
9+
import { Container, Header, Title, Content, Button } from 'native-base';
10+
import appStyles from '../../../styles/app';
11+
12+
export class DownloadDemo extends React.Component {
13+
14+
componentWillMount() {
15+
const {firestack} = this.props;
16+
}
17+
18+
componentWillUnmount() {
19+
const {firestack} = this.props;
20+
}
21+
22+
downloadByUrl() {
23+
const {firestack} = this.props;
24+
console.log('downloadByUrl called');
25+
}
26+
27+
render() {
28+
return (
29+
<Container>
30+
<Content>
31+
<Button
32+
block
33+
onPress={this.downloadByUrl.bind(this)}>
34+
Download with URL
35+
</Button>
36+
</Content>
37+
</Container>
38+
)
39+
}
40+
41+
}
42+
43+
export default DownloadDemo

app/views/Storage/Demos/Upload.js

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,108 @@ import React from 'react'
33
import {
44
View,
55
Text,
6+
Image
67
} from 'react-native'
78

9+
import {
10+
Container, Header, Title, Content, Button,
11+
Card, CardItem, Spinner
12+
} from 'native-base';
813
import appStyles from '../../../styles/app';
14+
const cat = require('image!cat');
15+
16+
const UploadedImage = ({image}) => {
17+
console.log('uploadedImage ->', image);
18+
return (
19+
<Card>
20+
<CardItem>
21+
<Text>Uploaded file</Text>
22+
</CardItem>
23+
24+
<CardItem>
25+
<Image
26+
style={{ resizeMode: 'cover' }}
27+
source={image} />
28+
</CardItem>
29+
</Card>
30+
)
31+
}
932

1033
export class UploadDemo extends React.Component {
1134

35+
constructor(props) {
36+
super(props);
37+
38+
this.state = {
39+
uploading: false,
40+
fileUploaded: null,
41+
error: null
42+
}
43+
}
44+
1245
componentWillMount() {
1346
const {firestack} = this.props;
47+
firestack.storage.setStorageUrl('firestack-example.appspot.com');
1448
}
1549

1650
componentWillUnmount() {
1751
const {firestack} = this.props;
1852
}
1953

20-
_sendHi() {
54+
_uploadFile() {
2155
const {firestack} = this.props;
56+
const storage = firestack.storage;
57+
this.setState({
58+
fileUploaded: null,
59+
uploading: true
60+
}, () => {
61+
storage.uploadFile(`/assets/images/cat.jpg`, cat.path, {
62+
contentType: 'image/jpg'
63+
}).then(res => {
64+
// And store it
65+
const storageRef = firestack.storage.ref(res.fullPath);
66+
storageRef.downloadUrl()
67+
.then(filepath => {
68+
this.setState({
69+
fileUploaded: filepath,
70+
error: null,
71+
uploading: false
72+
});
73+
});
74+
}).catch(err => {
75+
console.log('There was an error', err);
76+
this.setState({
77+
fileUploaded: null,
78+
uploading: false,
79+
error: err
80+
});
81+
});
82+
});
2283
}
2384

2485
render() {
86+
const { uploading, fileUploaded } = this.state;
2587
return (
26-
<View>
27-
<View style={appStyles.row}>
88+
<Container>
89+
<View style={[appStyles.description, appStyles.center]}>
2890
<Text>
29-
This is a test for dealing with Uploads.
91+
This demo uploads a local resource to the firestack storage
92+
and then uses `downloadUrl()` to download and display the image
3093
</Text>
3194
</View>
32-
</View>
33-
)
95+
96+
<Content>
97+
<Button
98+
block
99+
onPress={this._uploadFile.bind(this)}>
100+
Upload file
101+
</Button>
102+
{fileUploaded &&
103+
<UploadedImage image={fileUploaded} />}
104+
{uploading && <Spinner />}
105+
</Content>
106+
</Container>
107+
)
34108
}
35109

36110
}

app/views/Storage/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,29 @@ import DemoList from '../../components/DemoList';
1212

1313
// Demos
1414
import Upload from './Demos/Upload';
15+
import Download from './Demos/Download';
1516

1617
export const Routes = {
1718
'upload': {
1819
route: {
1920
title: 'Upload',
2021
Component: Upload
2122
}
23+
},
24+
'downloads': {
25+
route: {
26+
title: 'Downloads',
27+
Component: Download
28+
}
2229
}
2330
}
2431

2532
export class Storage extends React.Component {
2633
render() {
2734
return (
28-
<DemoList routes={Routes} routeKey='storage' />
35+
<DemoList {...this.props}
36+
routes={Routes}
37+
routeKey='storage' />
2938
);
3039
}
3140
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"info" : {
3+
"version" : 1,
4+
"author" : "xcode"
5+
}
6+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "universal",
5+
"filename" : "cat.jpg",
6+
"scale" : "1x"
7+
},
8+
{
9+
"idiom" : "universal",
10+
"scale" : "2x"
11+
},
12+
{
13+
"idiom" : "universal",
14+
"scale" : "3x"
15+
}
16+
],
17+
"info" : {
18+
"version" : 1,
19+
"author" : "xcode"
20+
}
21+
}
Loading

ios/GoogleService-Info (3).plist

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)