Add UI for admins to view and edit member cards

This commit is contained in:
Tanner Collin 2020-01-15 00:12:50 +00:00
parent 4d44e2c3cb
commit 4e82f13f74
7 changed files with 349 additions and 156 deletions

View File

@ -55,7 +55,7 @@ class Card(models.Model):
user = models.ForeignKey(User, related_name='cards', blank=True, null=True, on_delete=models.SET_NULL) user = models.ForeignKey(User, related_name='cards', blank=True, null=True, on_delete=models.SET_NULL)
member_id = models.IntegerField(blank=True, null=True) member_id = models.IntegerField(blank=True, null=True)
card_number = models.CharField(max_length=16, blank=True, null=True) card_number = models.CharField(unique=True, max_length=16, blank=True, null=True)
notes = models.TextField(blank=True, null=True) notes = models.TextField(blank=True, null=True)
last_seen_at = models.DateField(default=date.today, blank=True, null=True) last_seen_at = models.DateField(default=date.today, blank=True, null=True)
active_status = models.CharField(max_length=32, blank=True, null=True) active_status = models.CharField(max_length=32, blank=True, null=True)

View File

@ -2,6 +2,7 @@ from django.contrib.auth.models import User, Group
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from rest_framework import serializers from rest_framework import serializers
from rest_framework.exceptions import ValidationError from rest_framework.exceptions import ValidationError
from rest_framework.validators import UniqueValidator
from rest_auth.registration.serializers import RegisterSerializer from rest_auth.registration.serializers import RegisterSerializer
from rest_auth.serializers import UserDetailsSerializer from rest_auth.serializers import UserDetailsSerializer
from uuid import uuid4 from uuid import uuid4
@ -51,15 +52,6 @@ class UserEmailField(serializers.ModelField):
class AdminCardSerializer(serializers.ModelSerializer):
card_number = serializers.CharField()
class Meta:
model = models.Card
fields = '__all__'
read_only_fields = ['last_seen_at']
# member viewing other members # member viewing other members
class OtherMemberSerializer(serializers.ModelSerializer): class OtherMemberSerializer(serializers.ModelSerializer):
@ -177,7 +169,10 @@ class CardSerializer(serializers.ModelSerializer):
# admin viewing member details # admin viewing member details
class AdminCardSerializer(CardSerializer): class AdminCardSerializer(CardSerializer):
card_number = serializers.CharField() card_number = serializers.CharField(validators=[UniqueValidator(
queryset=models.Card.objects.all(),
message='Card number already exists.'
)])
member_id = serializers.IntegerField() member_id = serializers.IntegerField()
active_status = serializers.ChoiceField(['card_blocked', 'card_inactive', 'card_member_blocked', 'card_active']) active_status = serializers.ChoiceField(['card_blocked', 'card_inactive', 'card_member_blocked', 'card_active'])
class Meta: class Meta:

View File

@ -98,27 +98,33 @@ export function AccountForm(props) {
onChange: handleChange, onChange: handleChange,
value: input[name] || '', value: input[name] || '',
error: error[name], error: error[name],
...(input[name] ? {} : {icon: 'edit'}),
}); });
return ( return (
<Form onSubmit={handleSubmit}> <Form onSubmit={handleSubmit}>
<Header size='medium'>Member Details</Header> <Header size='medium'>Member Details</Header>
<Form.Input <Form.Group widths='equal'>
label='First Name' <Form.Input
required label='First Name'
{...makeProps('first_name')} fluid
/> required
{...makeProps('first_name')}
/>
<Form.Input
label='Last Name'
fluid
required
{...makeProps('last_name')}
/>
</Form.Group>
<Form.Input <Form.Input
label='Preferred First Name' label='Preferred First Name'
required required
{...makeProps('preferred_name')} {...makeProps('preferred_name')}
/> />
<Form.Input
label='Last Name'
required
{...makeProps('last_name')}
/>
<Form.Input <Form.Input
label='Email Address' label='Email Address'

View File

@ -4,6 +4,195 @@ import './light.css';
import { Container, Checkbox, Divider, Dropdown, Form, Grid, Header, Icon, Image, Menu, Message, Segment, Table } from 'semantic-ui-react'; import { Container, Checkbox, Divider, Dropdown, Form, Grid, Header, Icon, Image, Menu, Message, Segment, Table } from 'semantic-ui-react';
import { BasicTable, staticUrl, requester } from './utils.js'; import { BasicTable, staticUrl, requester } from './utils.js';
function AdminCardDetail(props) {
const [input, setInput] = useState({ ...props.card });
const [error, setError] = useState(false);
const [loading, setLoading] = useState(false);
const [success, setSuccess] = useState(false);
const id = props.card.id;
const handleValues = (e, v) => setInput({ ...input, [v.name]: v.value });
const handleUpload = (e, v) => setInput({ ...input, [v.name]: e.target.files[0] });
const handleChange = (e) => handleValues(e, e.currentTarget);
const handleCheck = (e, v) => setInput({ ...input, [v.name]: v.checked });
const handleSubmit = (e) => {
setLoading(true);
setSuccess(false);
const data = { ...input, member_id: props.result.member.id };
requester('/cards/'+id+'/', 'PUT', props.token, data)
.then(res => {
setLoading(false);
setSuccess(true);
setError(false);
setInput(res);
})
.catch(err => {
setLoading(false);
console.log(err);
setError(err.data);
});
};
const handleDelete = (e) => {
e.preventDefault();
requester('/cards/'+id+'/', 'DELETE', props.token)
.then(res => {
setInput(false);
})
.catch(err => {
console.log(err);
});
};
const makeProps = (name) => ({
name: name,
onChange: handleChange,
value: input[name] || '',
error: error[name],
});
const statusOptions = [
{ key: '0', text: 'Card Active', value: 'card_active' },
{ key: '1', text: 'Card Blocked', value: 'card_blocked' },
{ key: '2', text: 'Card Inactive', value: 'card_inactive' },
{ key: '3', text: 'Card Member Blocked', value: 'card_member_blocked' },
];
return (
input ?
<Segment raised color={input.active_status === 'card_active' ? 'green' : 'red'}>
<Form onSubmit={handleSubmit}>
<Form.Group widths='equal'>
<Form.Input
fluid
{...makeProps('card_number')}
/>
<Form.Select
fluid
options={statusOptions}
{...makeProps('active_status')}
onChange={handleValues}
/>
<Form.Group widths='equal'>
<Form.Button
loading={loading}
error={error.non_field_errors}
>
{success ? 'Saved.' : 'Save'}
</Form.Button>
<Form.Button
color='red'
onClick={handleDelete}
>
Delete
</Form.Button>
</Form.Group>
</Form.Group>
Notes: {input.notes || 'None'}
</Form>
</Segment>
:
<Segment raised color='black'>
Deleted card: {props.card.card_number}
</Segment>
);
};
export function AdminMemberCards(props) {
const cards = props.result.cards;
const [input, setInput] = useState({ active_status: 'card_active' });
const [error, setError] = useState(false);
const [loading, setLoading] = useState(false);
const [success, setSuccess] = useState(false);
const { id } = useParams();
const handleValues = (e, v) => setInput({ ...input, [v.name]: v.value });
const handleUpload = (e, v) => setInput({ ...input, [v.name]: e.target.files[0] });
const handleChange = (e) => handleValues(e, e.currentTarget);
const handleCheck = (e, v) => setInput({ ...input, [v.name]: v.checked });
const handleSubmit = (e) => {
setLoading(true);
setSuccess(false);
const data = { ...input, member_id: props.result.member.id };
requester('/cards/', 'POST', props.token, data)
.then(res => {
setLoading(false);
setSuccess(true);
setError(false);
props.setResult({ ...props.result, cards: [...cards, res] });
})
.catch(err => {
setLoading(false);
console.log(err);
setError(err.data);
});
};
const makeProps = (name) => ({
name: name,
onChange: handleChange,
value: input[name] || '',
error: error[name],
});
const statusOptions = [
{ key: '0', text: 'Card Active', value: 'card_active' },
{ key: '1', text: 'Card Blocked', value: 'card_blocked' },
{ key: '2', text: 'Card Inactive', value: 'card_inactive' },
{ key: '3', text: 'Card Member Blocked', value: 'card_member_blocked' },
];
return (
<div>
<Header size='medium'>Edit Member Cards</Header>
<Form onSubmit={handleSubmit}>
<Header size='small'>Add a Card</Header>
<Form.Group widths='equal'>
<Form.Input
label='Card Number'
fluid
{...makeProps('card_number')}
/>
<Form.Select
label='Card Status'
options={statusOptions}
fluid
{...makeProps('active_status')}
onChange={handleValues}
/>
<Form.Input
label='Optional Note'
fluid
{...makeProps('notes')}
/>
</Form.Group>
{success && <p>Success!</p>}
<Form.Button loading={loading} error={error.non_field_errors}>
Submit
</Form.Button>
</Form>
<Header size='small'>Current Cards</Header>
{cards.length ?
cards.map((x, i) =>
<AdminCardDetail key={i} card={x} {...props} />
)
:
<p>None</p>
}
</div>
);
};
export function AdminMemberForm(props) { export function AdminMemberForm(props) {
const [input, setInput] = useState(props.result.member); const [input, setInput] = useState(props.result.member);
const [error, setError] = useState(false); const [error, setError] = useState(false);
@ -42,78 +231,74 @@ export function AdminMemberForm(props) {
return ( return (
<div> <div>
{input ? <Form onSubmit={handleSubmit}>
<Form onSubmit={handleSubmit}> <Header size='medium'>Edit Member Details</Header>
<Header size='medium'>Edit Member Details</Header>
<Form.Input <Form.Input
label='Email' label='Email'
{...makeProps('email')} {...makeProps('email')}
/>
<Form.Input
label='Application Date'
{...makeProps('application_date')}
/>
<Form.Input
label='Current Start Date'
{...makeProps('current_start_date')}
/>
<Form.Input
label='Vetted Date'
{...makeProps('vetted_date')}
/>
<Form.Input
label='Expire Date'
{...makeProps('expire_date')}
/>
<Form.Input
label='Membership Fee'
{...makeProps('monthly_fees')}
/>
<Form.Field>
<label>Is the member a director?</label>
<Checkbox
label='Yes'
name='is_director'
onChange={handleCheck}
checked={input.is_director}
/> />
</Form.Field>
<Form.Input <Form.Field>
label='Application Date' <label>Is the member portal staff?</label>
{...makeProps('application_date')} <Checkbox
label='Yes'
name='is_staff'
onChange={handleCheck}
checked={input.is_staff}
/> />
</Form.Field>
<Form.Input <Form.Field>
label='Current Start Date' <label>Is the member an instructor?</label>
{...makeProps('current_start_date')} <Checkbox
label='Yes'
name='is_instructor'
onChange={handleCheck}
checked={input.is_instructor}
/> />
</Form.Field>
<Form.Input {success && <p>Success!</p>}
label='Vetted Date' <Form.Button loading={loading} error={error.non_field_errors}>
{...makeProps('vetted_date')} Submit
/> </Form.Button>
</Form>
<Form.Input
label='Expire Date'
{...makeProps('expire_date')}
/>
<Form.Input
label='Membership Fee'
{...makeProps('monthly_fees')}
/>
<Form.Field>
<label>Is the member a director?</label>
<Checkbox
label='Yes'
name='is_director'
onChange={handleCheck}
checked={input.is_director}
/>
</Form.Field>
<Form.Field>
<label>Is the member portal staff?</label>
<Checkbox
label='Yes'
name='is_staff'
onChange={handleCheck}
checked={input.is_staff}
/>
</Form.Field>
<Form.Field>
<label>Is the member an instructor?</label>
<Checkbox
label='Yes'
name='is_instructor'
onChange={handleCheck}
checked={input.is_instructor}
/>
</Form.Field>
{success && <p>Success!</p>}
<Form.Button loading={loading} error={error.non_field_errors}>
Submit
</Form.Button>
</Form>
:
<p>Loading...</p>
}
</div> </div>
); );
}; };
@ -123,66 +308,60 @@ export function AdminMemberInfo(props) {
return ( return (
<div> <div>
{member ? <Header size='medium'>Admin Details</Header>
<div>
<Header size='medium'>Admin Details</Header>
<BasicTable> <BasicTable>
<Table.Body> <Table.Body>
<Table.Row> <Table.Row>
<Table.Cell>Name:</Table.Cell> <Table.Cell>Name:</Table.Cell>
<Table.Cell>{member.first_name} {member.last_name}</Table.Cell> <Table.Cell>{member.first_name} {member.last_name}</Table.Cell>
</Table.Row> </Table.Row>
<Table.Row> <Table.Row>
<Table.Cell>Status:</Table.Cell> <Table.Cell>Status:</Table.Cell>
<Table.Cell>{member.status}</Table.Cell> <Table.Cell>{member.status}</Table.Cell>
</Table.Row> </Table.Row>
<Table.Row> <Table.Row>
<Table.Cell>Phone:</Table.Cell> <Table.Cell>Phone:</Table.Cell>
<Table.Cell>{member.phone}</Table.Cell> <Table.Cell>{member.phone}</Table.Cell>
</Table.Row> </Table.Row>
<Table.Row> <Table.Row>
<Table.Cell>Address:</Table.Cell> <Table.Cell>Address:</Table.Cell>
<Table.Cell>{member.street_address}</Table.Cell> <Table.Cell>{member.street_address}</Table.Cell>
</Table.Row> </Table.Row>
<Table.Row> <Table.Row>
<Table.Cell>City:</Table.Cell> <Table.Cell>City:</Table.Cell>
<Table.Cell>{member.city}</Table.Cell> <Table.Cell>{member.city}</Table.Cell>
</Table.Row> </Table.Row>
<Table.Row> <Table.Row>
<Table.Cell>Postal:</Table.Cell> <Table.Cell>Postal:</Table.Cell>
<Table.Cell>{member.postal_code}</Table.Cell> <Table.Cell>{member.postal_code}</Table.Cell>
</Table.Row> </Table.Row>
<Table.Row> <Table.Row>
<Table.Cell>Minor:</Table.Cell> <Table.Cell>Minor:</Table.Cell>
<Table.Cell>{member.is_minor ? 'Yes' : 'No'}</Table.Cell> <Table.Cell>{member.is_minor ? 'Yes' : 'No'}</Table.Cell>
</Table.Row> </Table.Row>
{member.is_minor && <Table.Row> {member.is_minor && <Table.Row>
<Table.Cell>Birthdate:</Table.Cell> <Table.Cell>Birthdate:</Table.Cell>
<Table.Cell>{member.birthdate}</Table.Cell> <Table.Cell>{member.birthdate}</Table.Cell>
</Table.Row>} </Table.Row>}
{member.is_minor && <Table.Row> {member.is_minor && <Table.Row>
<Table.Cell>Guardian:</Table.Cell> <Table.Cell>Guardian:</Table.Cell>
<Table.Cell>{member.guardian_name}</Table.Cell> <Table.Cell>{member.guardian_name}</Table.Cell>
</Table.Row>} </Table.Row>}
<Table.Row> <Table.Row>
<Table.Cell>Emergency Contact Name:</Table.Cell> <Table.Cell>Emergency Contact Name:</Table.Cell>
<Table.Cell>{member.emergency_contact_name || 'None'}</Table.Cell> <Table.Cell>{member.emergency_contact_name || 'None'}</Table.Cell>
</Table.Row> </Table.Row>
<Table.Row> <Table.Row>
<Table.Cell>Emergency Contact Phone:</Table.Cell> <Table.Cell>Emergency Contact Phone:</Table.Cell>
<Table.Cell>{member.emergency_contact_phone || 'None'}</Table.Cell> <Table.Cell>{member.emergency_contact_phone || 'None'}</Table.Cell>
</Table.Row> </Table.Row>
</Table.Body> </Table.Body>
</BasicTable> </BasicTable>
</div>
:
<p>Loading...</p>
}
</div> </div>
); );
}; };

View File

@ -84,18 +84,22 @@ export function SignupForm(props) {
<Form onSubmit={handleSubmit}> <Form onSubmit={handleSubmit}>
<Header size='medium'>Sign Up</Header> <Header size='medium'>Sign Up</Header>
<Form.Input <Form.Group widths='equal'>
label='First Name' <Form.Input
name='first_name' label='First Name'
onChange={handleChange} name='first_name'
error={error.first_name} fluid
/> onChange={handleChange}
<Form.Input error={error.first_name}
label='Last Name' />
name='last_name' <Form.Input
onChange={handleChange} label='Last Name'
error={error.last_name} name='last_name'
/> fluid
onChange={handleChange}
error={error.last_name}
/>
</Form.Group>
<Form.Input <Form.Input
label='Username' label='Username'

View File

@ -5,7 +5,7 @@ import { Button, Container, Divider, Dropdown, Form, Grid, Header, Icon, Image,
import moment from 'moment'; import moment from 'moment';
import { isAdmin, BasicTable, staticUrl, requester } from './utils.js'; import { isAdmin, BasicTable, staticUrl, requester } from './utils.js';
import { NotFound, PleaseLogin } from './Misc.js'; import { NotFound, PleaseLogin } from './Misc.js';
import { AdminMemberInfo, AdminMemberForm } from './Admin.js'; import { AdminMemberInfo, AdminMemberForm, AdminMemberCards } from './Admin.js';
export function Members(props) { export function Members(props) {
const [response, setResponse] = useState(false); const [response, setResponse] = useState(false);
@ -49,7 +49,7 @@ export function Members(props) {
</Header> </Header>
{response ? {response ?
<Item.Group divided> <Item.Group unstackable divided>
{response.results.length ? {response.results.length ?
response.results.map((x, i) => response.results.map((x, i) =>
<Item key={i} as={Link} to={'/members/'+x.member.id}> <Item key={i} as={Link} to={'/members/'+x.member.id}>
@ -130,6 +130,10 @@ export function MemberDetail(props) {
</Grid.Column> </Grid.Column>
</Grid> </Grid>
{isAdmin(user) && <Segment padded>
<AdminMemberCards result={result} setResult={setResult} {...props} />
</Segment>}
</div> </div>
: :
<p>Loading...</p> <p>Loading...</p>

View File

@ -33,6 +33,11 @@ export const requester = (route, method, token, data) => {
method: method, method: method,
body: formData, body: formData,
}; };
} else if (method == 'DELETE') {
options = {
...options,
method: method,
};
} else { } else {
throw new Error('Method not supported'); throw new Error('Method not supported');
} }
@ -48,7 +53,7 @@ export const requester = (route, method, token, data) => {
if (!response.ok) { if (!response.ok) {
throw customError(response); throw customError(response);
} }
return response.json(); return method === 'DELETE' ? {} : response.json();
}) })
.catch(error => { .catch(error => {
const code = error.data ? error.data.status : null; const code = error.data ? error.data.status : null;