-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
84 lines (71 loc) · 2.4 KB
/
index.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
import React, { Component } from 'react';
import {
StyleSheet,
View,
Image
} from 'react-native';
import PropTypes from "prop-types";
import md5 from 'blueimp-md5';
const GRAVATAR_URI = 'www.gravatar.com/avatar/';
/**
*@description Display gravatar for an email address
*/
class Gravatar extends React.Component {
_calculateStyle() {
const size = {width: this.props.size / 2, height: this.props.size / 2};
let border = {};
switch (this.props.mask) {
case 'circle':
border = {borderRadius: size.width / 2};
break;
case 'rounded':
border = {borderRadius: size.width / 20};
break;
case 'square':
break;
}
return {...size, ...border};
}
render() {
const protocol = this.props.secure ? 'https://' : 'http://';
const uri = protocol + GRAVATAR_URI + md5(this.props.emailAddress) + '?s=' + this.props.size + '&d=' + this.props.defaultImage;
const style = this._calculateStyle();
return (
<View style={[styles.overlay]}>
<Image resizeMode={this.props.resizeMode} source={{uri}} style={[styles.image, style]} />
</View>
);
}
}
Gravatar.propTypes = {
/**
* @param {string} emailAddress - email address for the gravatar
* @param {number} size - size of the gravatar
* @param {('circle'|'square'|'rounded' )} mask - mask type for the gravatar
* @param {('404'|'mm'|'identicon'|'monsterid'|'wavatar'|'retro'|'robohash'|'blank')} defaultImage - deafult image type if no image is supplied
* @param {('cover'|'contain'|'stretch'|'repeat'|'center')} resizeMode - resizeMode for the gravatar image.
*/
...View.PropTypes,
emailAddress: PropTypes.string,
size: PropTypes.number,
mask: PropTypes.oneOf(['circle', 'square', 'rounded']),
defaultImage: PropTypes.oneOf(['404', 'mm', 'identicon', 'monsterid', 'wavatar', 'retro', 'robohash', 'blank']),
resizeMode: PropTypes.oneOf(['cover', 'contain', 'stretch', 'repeat', 'center'])
};
Gravatar.defaultProps = {
emailAddress: '',
size: 600,
mask: 'circle',
secure: true,
defaultImage: 'retro',
resizeMode: 'contain'
};
const styles = StyleSheet.create({
overlay: {
overflow: 'hidden',
},
image: {
flex: 1,
},
});
export default Gravatar;