import React from 'react'; import superagent from 'superagent'; import _ from 'lodash'; import { fromUnixTime, formatDistanceToNow, format } from "date-fns"; import { formatInTimeZone } from 'date-fns-tz' import MoviesTVWrapper from './MoviesTVWrapper.jsx'; import { Container, Segment, Header, Table, Label, Button, Form, Transition, List } from 'semantic-ui-react' import { DEFAULT_TIMEZONE } from '../CloudApi/Constants.js'; export default class Coupons extends React.Component { constructor(props) { super(props); this.state = { loading: false, entitlementClasses: [], coupons: [], newCoupons: null, quantity: 1 }; } async componentDidMount() { this.setState({ loading: true }); const res = await superagent.get(`/api/admin/media/coupons`).accept('json'); this.setState({ coupons: res.body }); const schemaRes = await superagent.get("/api/admin/media/schemas"); const entitlementClasses = Object.keys(schemaRes.body.EntitlementClass).map(k => { return { "key": k, "value": k, "text": k } }); const mediaTypes = Object.keys(schemaRes.body.MediaType).map(k => { return { "key": k, "value": k, "text": k } }); this.setState({ schemas: schemaRes.body, entitlementClasses, mediaTypes, loading: false }); } async onChange(e, { name, value }) { this.setState({ [name]: value }) } async onGenerateCoupons() { this.setState({ newCoupons: null, loading: true }); let res = await superagent.post(`/api/admin/media/coupons`).send({ entitlementClass: this.state.entitlementClass, quantity: this.state.quantity }); this.setState({ newCoupons: res.body, loading: false }); res = await superagent.get(`/api/admin/media/coupons`).accept('json'); this.setState({ coupons: res.body, loading: false }); } renderCoupon(coupon) { const timezone = DEFAULT_TIMEZONE; const created = (coupon.createdAt && coupon.createdAt._seconds) ? formatInTimeZone(fromUnixTime(coupon.createdAt._seconds), timezone, 'PPPp z') : "Error"; const activationTimestamp = (coupon.activationTimestamp && coupon.activationTimestamp._seconds) ? formatInTimeZone(fromUnixTime(coupon.activationTimestamp._seconds), timezone, 'PPPp z') : null; const expirationTimestamp = (coupon.expirationTimestamp && coupon.expirationTimestamp._seconds) ? formatInTimeZone(fromUnixTime(coupon.expirationTimestamp._seconds), timezone, 'PPPp z') : null; return (