forked from opencollective/opencollective-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGiftCardDetails.js
229 lines (211 loc) · 7.56 KB
/
GiftCardDetails.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import React from 'react';
import PropTypes from 'prop-types';
import themeGet from '@styled-system/theme-get';
import dayjs from 'dayjs';
import { get } from 'lodash';
import { FormattedDate, FormattedMessage, injectIntl } from 'react-intl';
import styled, { withTheme } from 'styled-components';
import { formatCurrency } from '../lib/currency-utils';
import GiftCard from './icons/GiftCard';
import Avatar from './Avatar';
import Container from './Container';
import { Box, Flex } from './Grid';
import Link from './Link';
import StyledButton from './StyledButton';
import { Span } from './Text';
const DetailsColumnHeader = styled.span`
text-transform: uppercase;
color: ${themeGet('colors.black.500')};
font-weight: 300;
white-space: nowrap;
`;
/** Render a text status to indicate if gift card is claimed, and by whom */
const GiftCardStatus = ({ isConfirmed, collective, data }) => {
if (isConfirmed) {
return (
<FormattedMessage
id="giftCards.claimedBy"
defaultMessage="claimed by {user}"
values={{
user: <Link href={`/${collective.slug}`}>{collective.name}</Link>,
}}
/>
);
} else if (get(data, 'email')) {
return (
<FormattedMessage
id="giftCards.sentTo"
defaultMessage="sent to {email}"
values={{ email: <a href={`mailto:${data.email}`}>{data.email}</a> }}
/>
);
} else {
return <FormattedMessage id="giftCards.notYetClaimed" defaultMessage="not yet claimed" />;
}
};
GiftCardStatus.propTypes = {
isConfirmed: PropTypes.bool,
collective: PropTypes.shape({
slug: PropTypes.string,
name: PropTypes.string,
}),
data: PropTypes.shape({
email: PropTypes.string,
}),
};
/**
* Render GiftCard details like its status (claimed or not), who claimed it,
* when was it created... It is not meant to be show to all users, but just to
* the organizations that create the gift cards.
*/
class GiftCardDetails extends React.Component {
static propTypes = {
/** The gift card, which is actually a PaymentMethod */
giftCard: PropTypes.object.isRequired,
/** Collective slug */
collectiveSlug: PropTypes.string.isRequired,
/** @ignore Provided by styled-component withTheme(...) */
theme: PropTypes.object,
/** @ignore Provided by injectIntl */
intl: PropTypes.object,
};
constructor(props) {
super(props);
this.state = { expended: false };
}
toggleExpended() {
this.setState(state => ({ expended: !state.expended }));
}
getStatusColor(isConfirmed, balance, isExpired) {
const { colors } = this.props.theme;
if (balance === 0 || isExpired) {
return colors.black[200];
}
return isConfirmed ? colors.green[500] : colors.yellow[500];
}
renderDetails() {
const { giftCard, collectiveSlug } = this.props;
const redeemCode = giftCard.uuid.split('-')[0];
const email = get(giftCard, 'data.email');
return (
<Flex mt="0.75em" fontSize="0.8em">
{!giftCard.isConfirmed && (
<Flex flexDirection="column" mr="2em">
<DetailsColumnHeader>
<FormattedMessage id="giftCards.redeemCode" defaultMessage="REDEEM CODE" />
</DetailsColumnHeader>
<Link href={{ pathname: `/${collectiveSlug}/redeem/${redeemCode}`, query: { email } }}>{redeemCode}</Link>
</Flex>
)}
<Flex flexDirection="column" mr="2em">
<DetailsColumnHeader>
<FormattedMessage id="giftCards.emmited" defaultMessage="Emitted" />
</DetailsColumnHeader>
<FormattedDate value={giftCard.createdAt} />
</Flex>
<Flex flexDirection="column" mr="2em">
<DetailsColumnHeader>
<FormattedMessage id="giftCards.expiryDate" defaultMessage="EXPIRY DATE" />
</DetailsColumnHeader>
<span>{dayjs(giftCard.expiryDate).format('MM/YYYY')}</span>
</Flex>
<Flex flexDirection="column" mr="2em">
<DetailsColumnHeader>
<FormattedMessage id="giftCards.batch" defaultMessage="Batch name" />
</DetailsColumnHeader>
<span>
{giftCard.batch || (
<Span fontStyle="italic" color="black.500">
<FormattedMessage id="giftCards.notBatched" defaultMessage="Not batched" />
</Span>
)}
</span>
</Flex>
<Flex flexDirection="column" mr="2em">
<DetailsColumnHeader>
<FormattedMessage id="giftCards.description" defaultMessage="DESCRIPTION" />
</DetailsColumnHeader>
<span>{giftCard.description}</span>
</Flex>
</Flex>
);
}
renderValue() {
const { initialBalance, currency, monthlyLimitPerMember } = this.props.giftCard;
const { locale } = this.props.intl;
return monthlyLimitPerMember ? (
<FormattedMessage
id="giftCards.monthlyValue"
defaultMessage="{value} monthly"
values={{ value: formatCurrency(monthlyLimitPerMember, currency, { locale }) }}
/>
) : (
formatCurrency(initialBalance, currency, { locale })
);
}
render() {
const { isConfirmed, collective, balance, currency, expiryDate, data } = this.props.giftCard;
const isExpired = Boolean(expiryDate && new Date(expiryDate) < new Date());
const { locale } = this.props.intl;
return (
<Flex data-cy="vc-details">
{/* Avatar column */}
<Box mr="20px">
{isConfirmed ? (
<Link href={`/${collective.slug}`} title={collective.name}>
<Container>
<GiftCard
alignSelf="center"
size="2.5em"
color={this.getStatusColor(isConfirmed, balance, isExpired)}
/>
<Avatar collective={collective} radius={24} mt="-1em" ml="1em" css={{ position: 'absolute' }} />
</Container>
</Link>
) : (
<GiftCard alignSelf="center" size="2.5em" color={this.getStatusColor(isConfirmed, balance, isExpired)} />
)}
</Box>
{/* Infos + details column */}
<Flex flexDirection="column" p="0.1em">
<Box>
<strong>{this.renderValue()}</strong>{' '}
<GiftCardStatus isConfirmed={isConfirmed} collective={collective} data={data} />
</Box>
<Box color={this.props.theme.colors.black[500]} fontSize="0.9em">
<Flex alignItems="center">
<FormattedMessage
id="giftCards.balance"
defaultMessage="Balance: {balance}"
values={{ balance: formatCurrency(balance, currency, { locale }) }}
/>
{isExpired && (
<React.Fragment>
<Box mx={1}>|</Box>
<FormattedMessage id="GiftCard.Expired" defaultMessage="Expired" />
</React.Fragment>
)}
<Box mx={1}>|</Box>
<StyledButton
isBorderless
buttonSize="tiny"
buttonStyle="secondary"
fontSize="11px"
onClick={() => this.toggleExpended()}
px={1}
>
{this.state.expended ? (
<FormattedMessage id="closeDetails" defaultMessage="Close Details" />
) : (
<FormattedMessage id="viewDetails" defaultMessage="View Details" />
)}
</StyledButton>
</Flex>
</Box>
{this.state.expended && this.renderDetails()}
</Flex>
</Flex>
);
}
}
export default withTheme(injectIntl(GiftCardDetails));