-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdem-info.jsx
64 lines (54 loc) · 1.61 KB
/
dem-info.jsx
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
'use strict'
import React from 'react'
import cx from 'classnames'
import { createUseStyles } from 'react-jss'
export default function DemInfo(props) {
const { state, dob, party, className, ...otherProps } = props
const classes = useStylesFromThemeFunction()
if (!(state || dob || party)) return null // if no data, render not
const userState = state || ''
const userAge = dob ? calculateAge(dob) : ''
const userPoliticalParty = party || ''
let contentText = ''
if (userPoliticalParty && (userAge || userState)) {
contentText += `${userPoliticalParty} | `
} else if (userPoliticalParty) {
contentText += `${userPoliticalParty}`
}
if (userAge && userState) {
contentText += `${userAge}, ${userState}`
} else if (userAge || userState) {
contentText += userAge || userState
}
return (
<span className={cx(classes.infoText, className)} {...otherProps}>
{contentText}
</span>
)
}
/**
* Calculate user age based on birthdate from User Schema
* @param {String} birthdayStr
* @return {Number} age (in years)
*/
function calculateAge(birthdayStr) {
const birthday = new Date(birthdayStr)
const today = new Date()
let age = today.getFullYear() - birthday.getFullYear()
const month = today.getMonth() - birthday.getMonth()
if (month < 0 || (month === 0 && today.getDate() < birthday.getDate())) {
age--
}
return age
}
const useStylesFromThemeFunction = createUseStyles(theme => ({
infoText: {
fontFamily: 'Inter',
fontSize: '1rem',
fontWeight: '400',
lineHeight: '1.5rem',
letterSpacing: '0rem',
textAlign: 'left',
color: '#5D5D5C',
},
}))