spaceport/webclient/src/Home.js

352 lines
13 KiB
JavaScript
Raw Normal View History

2020-05-17 00:40:48 +00:00
import React, { useState, useEffect, useReducer } from 'react';
import { BrowserRouter as Router, Switch, Route, Link, useParams, useLocation } from 'react-router-dom';
import moment from 'moment-timezone';
import QRCode from 'react-qr-code';
import './light.css';
2020-04-21 00:14:53 +00:00
import { Container, Divider, Dropdown, Form, Grid, Header, Icon, Image, Menu, Message, Popup, Segment, Table } from 'semantic-ui-react';
import { statusColor, BasicTable, siteUrl, staticUrl, requester, isAdmin } from './utils.js';
import { LoginForm, SignupForm } from './LoginSignup.js';
import { AccountForm } from './Account.js';
2021-12-08 22:02:30 +00:00
import { SignForm } from './Sign.js';
import { PayPalSubscribeDeal } from './PayPal.js';
function MemberInfo(props) {
const user = props.user;
const member = user.member;
const lastTrans = user.transactions?.slice(0,3);
const lastTrain = user.training?.sort((a, b) => a.session.datetime < b.session.datetime ? 1 : -1).slice(0,3);
const lastCard = user.cards?.sort((a, b) => a.last_seen < b.last_seen)[0];
2022-03-19 21:40:01 +00:00
const unpaidTraining = user.training?.filter(x => x.attendance_status === 'Waiting for payment');
return (
<div>
<Grid stackable>
<Grid.Column width={5}>
<Image
size='small'
src={member.photo_medium ? staticUrl + '/' + member.photo_medium : '/nophoto.png'}
2020-01-11 06:27:30 +00:00
/>
</Grid.Column>
<Grid.Column width={11}>
<Header size='large'>{member.preferred_name} {member.last_name}</Header>
{member.is_allowed_entry ?
2022-04-10 07:13:36 +00:00
<></>
:
2021-09-23 21:13:36 +00:00
<p>You are not allowed entry into Protospace </p>
}
<BasicTable>
<Table.Body>
<Table.Row>
<Table.Cell>Status:</Table.Cell>
<Table.Cell>
<Icon name='circle' color={statusColor[member.status]} />
{member.status || 'Unknown'}
</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>Expiry:</Table.Cell>
2022-01-17 21:46:53 +00:00
<Table.Cell>{member.expire_date ? moment(member.expire_date).format('ll') : 'Unknown'}</Table.Cell>
</Table.Row>
</Table.Body>
</BasicTable>
</Grid.Column>
</Grid>
2022-03-19 21:40:01 +00:00
{!lastTrans.length && <Message>
<Message.Header>Welcome, new member!</Message.Header>
<p>
<a href={staticUrl + '/' + member.member_forms} target='_blank'>
Click here
2020-02-20 05:51:29 +00:00
</a> to view your application forms.
</p>
</Message>}
{!!lastTrans.length && !member.photo_medium && <Message warning>
<Message.Header>Please set a member photo!</Message.Header>
<p>Visit the <Link to='/account'>account settings</Link> page to set one.</p>
</Message>}
2020-05-01 03:55:13 +00:00
{!lastTrans.length && <React.Fragment>
<Header size='medium'>PayPal</Header>
<p>Create a ${user.member.monthly_fees} / month subscription, get your first three months for the price of two:</p>
<PayPalSubscribeDeal
amount={user.member.monthly_fees}
name='Protospace Membership'
custom={JSON.stringify({ deal: 3, member: user.member.id })}
/>
<p>Click "Checkout as Guest" if you don't have a PayPal account.</p>
<QRCode value={siteUrl + '/subscribe?monthly_fees=' + user.member.monthly_fees + '&id=' + user.member.id} />
2020-05-01 03:55:13 +00:00
</React.Fragment>}
<Header size='medium'>Latest Training</Header>
2022-03-19 21:40:01 +00:00
{unpaidTraining.map(x =>
<Message warning>
<Message.Header>Please pay your course fee!</Message.Header>
<p>Pay ${x.session.cost} for <Link to={'/classes/'+x.session.id}>{x.session.course_data.name}</Link> to avoid losing your spot.</p>
</Message>
)}
<BasicTable>
<Table.Body>
{lastTrain.length ?
lastTrain.map(x =>
<Table.Row key={x.id}>
<Table.Cell style={{ minWidth: '8rem' }}>
<Link to={'/classes/'+x.session.id}>{moment(x.session.datetime).tz('America/Edmonton').format('ll')}</Link>
</Table.Cell>
<Table.Cell>{x.session.course_data.name}</Table.Cell>
</Table.Row>
)
:
<Table.Row><Table.Cell>None, please sign up for an <Link to={'/courses/249/'}>Orientation</Link></Table.Cell></Table.Row>
}
{user.training.length > 3 && <Link to='/training'>[more]</Link>}
</Table.Body>
</BasicTable>
<Header size='medium'>Latest Transactions</Header>
<BasicTable>
<Table.Body>
{lastTrans.length ?
lastTrans.map(x =>
<Table.Row key={x.id}>
<Table.Cell style={{ minWidth: '8rem' }}>
<Link to={'/transactions/'+x.id}>{moment(x.date).format('ll')}</Link>
</Table.Cell>
<Table.Cell>{x.account_type}</Table.Cell>
<Table.Cell>${x.amount}</Table.Cell>
</Table.Row>
)
:
<Table.Row><Table.Cell>None</Table.Cell></Table.Row>
}
{user.transactions.length > 3 && <Link to='/transactions'>[more]</Link>}
</Table.Body>
</BasicTable>
<Header size='medium'>Details</Header>
<BasicTable>
<Table.Body>
<Table.Row>
<Table.Cell>Application:</Table.Cell>
2022-01-17 21:46:53 +00:00
<Table.Cell>{member.application_date ? moment(member.application_date).format('ll') : 'Unknown'}</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>Start:</Table.Cell>
2022-01-17 21:46:53 +00:00
<Table.Cell>{member.current_start_date ? moment(member.current_start_date).format('ll') : 'Unknown'}</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>Vetted:</Table.Cell>
2022-01-17 21:46:53 +00:00
<Table.Cell>{member.vetted_date ? moment(member.vetted_date).format('ll') : 'Not vetted'}</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>Monthly dues:</Table.Cell>
<Table.Cell>${member.monthly_fees || 'Unknown'}.00</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>Last scan:</Table.Cell>
2020-02-28 09:30:22 +00:00
<Table.Cell>
{lastCard && lastCard.last_seen ?
lastCard.last_seen > '2021-11-14T02:01:35.415685Z' ?
moment.utc(lastCard.last_seen).tz('America/Edmonton').format('lll')
:
moment.utc(lastCard.last_seen).tz('America/Edmonton').format('ll')
:
'Unknown'
}
2020-02-28 09:30:22 +00:00
{user.cards.length > 1 && <Link to='/cards'> [more]</Link>}
</Table.Cell>
</Table.Row>
</Table.Body>
</BasicTable>
2022-03-19 21:40:01 +00:00
{!!lastTrans.length && <p>
<a href={staticUrl + '/' + member.member_forms} target='_blank'>
View application forms
</a>
</p>}
</div>
);
};
export function Home(props) {
2020-11-17 20:27:12 +00:00
const { user, token } = props;
const [stats, setStats] = useState(JSON.parse(localStorage.getItem('stats', 'false')));
2020-05-17 00:40:48 +00:00
const [refreshCount, refreshStats] = useReducer(x => x + 1, 0);
const location = useLocation();
useEffect(() => {
2020-11-17 20:27:12 +00:00
requester('/stats/', 'GET', token)
.then(res => {
setStats(res);
localStorage.setItem('stats', JSON.stringify(res));
})
.catch(err => {
console.log(err);
2020-02-10 09:15:14 +00:00
setStats(false);
});
2020-11-18 08:47:22 +00:00
}, [refreshCount, token]);
2022-01-17 21:46:53 +00:00
const getStat = (x) => stats && stats[x] ? stats[x] : 'Unknown';
2020-06-22 20:51:04 +00:00
const getZeroStat = (x) => stats && stats[x] ? stats[x] : '0';
2022-02-16 05:35:38 +00:00
const getDateStat = (x) => stats && stats[x] ? moment.utc(stats[x]).tz('America/Edmonton').format('MMM Do @ LT') : 'Unknown';
2022-03-17 19:50:57 +00:00
const getNextStat = (x) => {
if (stats && stats[x]) {
const datetime = moment.utc(stats[x].datetime).tz('America/Edmonton');
if (datetime.isSame(moment().tz('America/Edmonton'), 'day') ) {
return <>{datetime.format('LT')} | <Link to={'/classes/' + stats[x].id}>{stats[x].name}</Link></>;
} else {
return <>{datetime.format('MMM Do')} | <Link to={'/classes/' + stats[x].id}>{stats[x].name}</Link></>;
}
} else {
return 'Unknown';
}
};
2020-04-21 00:14:53 +00:00
const mcPlayers = stats && stats['minecraft_players'] ? stats['minecraft_players'] : [];
2020-10-31 22:54:05 +00:00
const mumbleUsers = stats && stats['mumble_users'] ? stats['mumble_users'] : [];
2020-04-21 00:14:53 +00:00
2022-01-17 21:46:53 +00:00
const getTrackStat = (x) => stats && stats.track && stats.track[x] ? moment().unix() - stats.track[x]['time'] > 60 ? 'Free' : 'In Use' : 'Unknown';
2020-09-20 00:23:27 +00:00
const getTrackLast = (x) => stats && stats.track && stats.track[x] ? moment.unix(stats.track[x]['time']).tz('America/Edmonton').format('llll') : 'Unknown';
const getTrackAgo = (x) => stats && stats.track && stats.track[x] ? moment.unix(stats.track[x]['time']).tz('America/Edmonton').fromNow() : '';
2022-02-04 22:13:07 +00:00
const getTrackName = (x) => stats && stats.track && stats.track[x] && stats.track[x]['first_name'] ? stats.track[x]['first_name'] : 'Unknown';
2020-07-26 00:04:58 +00:00
const alarmStat = () => stats && stats.alarm && moment().unix() - stats.alarm['time'] < 300 ? stats.alarm['data'] < 270 ? 'Armed' : 'Disarmed' : 'Unknown';
2020-11-17 20:27:12 +00:00
2021-09-08 03:34:12 +00:00
const doorOpenStat = () => alarmStat() == 'Disarmed' && stats.alarm['data'] > 360 ? ', door open' : '';
2021-11-15 05:57:18 +00:00
const show_signup = stats?.at_protospace;
return (
<Container>
<Grid stackable padded columns={2}>
<Grid.Column>
{user ?
user.member.set_details ?
<MemberInfo user={user} />
:
<AccountForm {...props} />
:
<div>
2021-11-15 05:57:18 +00:00
<LoginForm {...props} />
2021-11-15 05:57:18 +00:00
<Divider section horizontal>Or</Divider>
<SignupForm {...props} show_signup={show_signup} />
</div>
}
</Grid.Column>
<Grid.Column>
2022-02-05 21:15:03 +00:00
{user?.member?.set_details !== false &&
<Segment>
<Header size='medium'>Quick Links</Header>
<p><a href='http://protospace.ca/' target='_blank' rel='noopener noreferrer'>Main Website</a></p>
<p><a href='http://wiki.protospace.ca/Welcome_to_Protospace' target='_blank' rel='noopener noreferrer'>Protospace Wiki</a> <Link to='/auth/wiki'>[register]</Link></p>
2022-03-14 04:41:15 +00:00
<p><a href='https://forum.protospace.ca' target='_blank' rel='noopener noreferrer'>Forum (Spacebar)</a> <Link to='/auth/discourse'>[register]</Link></p>
{!!user && <p><a href='https://drive.google.com/drive/folders/0By-vvp6fxFekfmU1cmdxaVRlaldiYXVyTE9rRnNVNjhkc3FjdkFIbjBwQkZ3MVVQX2Ezc3M?resourcekey=0-qVLjcYr8ZCmLypdINk2svg' target='_blank' rel='noopener noreferrer'>Google Drive</a></p>}
{!!user && isAdmin(user) && <p><a href='https://estancia.hippocmms.ca/' target='_blank' rel='noopener noreferrer'>Property Management Portal</a></p>}
2020-02-10 06:29:13 +00:00
<img className='swordfish' src='/swordfish.png' onClick={() => refreshStats()} />
<div>
<Header size='medium'>Protospace Stats</Header>
<p>Next meeting: {getDateStat('next_meeting')}</p>
<p>Next clean: {getDateStat('next_clean')}</p>
2022-03-17 19:50:57 +00:00
<p className='nowrap-stat'>Next class: {getNextStat('next_class')}</p>
<p className='nowrap-stat'>Last class: {getNextStat('prev_class')}</p>
<p>Member count: {getStat('member_count')} <Link to='/charts'>[more]</Link></p>
<p>Card scans today: {getZeroStat('card_scans')}</p>
<p>
Minecraft players: {mcPlayers.length} {mcPlayers.length > 5 && '🔥'} <Popup content={
<React.Fragment>
<p>
Server IP:<br />
games.protospace.ca
</p>
<p>
Players:<br />
{mcPlayers.length ? mcPlayers.map(x => <React.Fragment>{x}<br /></React.Fragment>) : 'None'}
</p>
</React.Fragment>
} trigger={<a>[more]</a>} />
{' '}<a href='http://games.protospace.ca:8123/?worldname=world&mapname=flat&zoom=3&x=74&y=64&z=354' target='_blank'>[map]</a>
</p>
{stats && stats.hasOwnProperty('mumble_users') && <p>
Mumble users: {mumbleUsers.length} <Popup content={
<React.Fragment>
<p>
Server IP:<br />
mumble.protospace.ca
</p>
<p>
Users:<br />
{mumbleUsers.length ? mumbleUsers.map(x => <React.Fragment>{x}<br /></React.Fragment>) : 'None'}
</p>
</React.Fragment>
} trigger={<a>[more]</a>} />
</p>}
<p>
Trotec availability: {getTrackStat('TROTECS300')} <Popup content={
<React.Fragment>
<p>
Last use:<br />
{getTrackLast('TROTECS300')}<br />
{getTrackAgo('TROTECS300')}<br />
by {getTrackName('TROTECS300')}
</p>
</React.Fragment>
} trigger={<a>[more]</a>} />
</p>
<p>
Rabbit availability: {getTrackStat('FRICKIN-LASER')} <Popup content={
<React.Fragment>
<p>
Last use:<br />
{getTrackLast('FRICKIN-LASER')}<br />
{getTrackAgo('FRICKIN-LASER')}<br />
by {getTrackName('FRICKIN-LASER')}
</p>
</React.Fragment>
} trigger={<a>[more]</a>} />
</p>
<p>
Precix availability: {getTrackStat('CNC-PRECIX')} <Popup content={
<React.Fragment>
<p>
Last use:<br />
{getTrackLast('CNC-PRECIX')}<br />
{getTrackAgo('CNC-PRECIX')}<br />
by {getTrackName('CNC-PRECIX')}
</p>
</React.Fragment>
} trigger={<a>[more]</a>} />
</p>
{user && <p>Alarm status: {alarmStat()}{doorOpenStat()}</p>}
</div>
<SignForm token={token} />
</Segment>
}
</Grid.Column>
</Grid>
</Container>
);
};