Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Approval history UI #746

Merged
merged 7 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions backend/clubs/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2975,6 +2975,37 @@
)


class ApprovalHistorySerializer(serializers.ModelSerializer):
approved = serializers.BooleanField()
approved_on = serializers.DateTimeField()
approved_by = serializers.SerializerMethodField("get_approved_by")
approved_comment = serializers.CharField()
history_date = serializers.DateTimeField()

def get_approved_by(self, obj):
user = self.context["request"].user
julianweng marked this conversation as resolved.
Show resolved Hide resolved
if not user.is_authenticated:
return None

Check warning on line 2988 in backend/clubs/serializers.py

View check run for this annotation

Codecov / codecov/patch

backend/clubs/serializers.py#L2988

Added line #L2988 was not covered by tests
if not user.has_perm("clubs.see_pending_clubs"):
club = Club.objects.get(code=obj.code)
membership = Membership.objects.filter(person=user, club=club).first()
if membership is None or membership.role < Membership.ROLE_OFFICER:
return None

Check warning on line 2993 in backend/clubs/serializers.py

View check run for this annotation

Codecov / codecov/patch

backend/clubs/serializers.py#L2993

Added line #L2993 was not covered by tests
if obj.approved_by is None:
return "Unknown"
return obj.approved_by.get_full_name()

Check warning on line 2996 in backend/clubs/serializers.py

View check run for this annotation

Codecov / codecov/patch

backend/clubs/serializers.py#L2996

Added line #L2996 was not covered by tests

class Meta:
model = Club
fields = (
"approved",
"approved_on",
"approved_by",
"approved_comment",
"history_date",
)


class AdminNoteSerializer(ClubRouteMixin, serializers.ModelSerializer):
creator = serializers.SerializerMethodField("get_creator")
title = serializers.CharField(max_length=255, default="Note")
Expand Down
46 changes: 46 additions & 0 deletions backend/clubs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
ApplicationSubmissionCSVSerializer,
ApplicationSubmissionSerializer,
ApplicationSubmissionUserSerializer,
ApprovalHistorySerializer,
AssetSerializer,
AuthenticatedClubSerializer,
AuthenticatedMembershipSerializer,
Expand Down Expand Up @@ -1276,6 +1277,49 @@ def upload_file(self, request, *args, **kwargs):

return file_upload_endpoint_helper(request, code=club.code)

@action(detail=True, methods=["get"])
def history(self, request, *args, **kwargs):
julianweng marked this conversation as resolved.
Show resolved Hide resolved
"""
Return a simplified approval history for the club.
---
responses:
"200":
content:
application/json:
schema:
type: array
items:
type: object
properties:
approved:
type: boolean
approved_on:
type: string
format: date-time
approved_by:
type: string
description: >
The full name of the user who approved
the club.
approved_comment:
type: string
history_date:
type: string
format: date-time
description: >
The time in which the specific version
of the club was saved at.
---
"""
club = self.get_object()
return Response(
ApprovalHistorySerializer(
club.history.order_by("history_date"),
julianweng marked this conversation as resolved.
Show resolved Hide resolved
many=True,
context={"request": request},
).data
)

@action(detail=True, methods=["get"])
def owned_badges(self, request, *args, **kwargs):
"""
Expand Down Expand Up @@ -2159,6 +2203,8 @@ def get_serializer_class(self):
return ClubConstitutionSerializer
if self.action == "notes_about":
return NoteSerializer
if self.action == "history":
return ApprovalHistorySerializer
if self.action in {"list", "fields"}:
if self.request is not None and (
self.request.accepted_renderer.format == "xlsx"
Expand Down
13 changes: 13 additions & 0 deletions backend/tests/clubs/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2182,6 +2182,12 @@ def test_club_sensitive_field_renew(self):
club.refresh_from_db()
self.assertTrue(club.approved)

# store result of approval history query
resp = self.client.get(reverse("clubs-history", args=(club.code,)))
self.assertIn(resp.status_code, [200], resp.content)
previous_history = json.loads(resp.content.decode("utf-8"))
self.assertTrue(previous_history[-1]["approved"])

with patch("django.conf.settings.REAPPROVAL_QUEUE_OPEN", True):
for field in {"name"}:
# edit sensitive field
Expand All @@ -2191,6 +2197,13 @@ def test_club_sensitive_field_renew(self):
content_type="application/json",
)
self.assertIn(resp.status_code, [200, 201], resp.content)
resp = self.client.get(reverse("clubs-history", args=(club.code,)))
# find the approval history
resp = self.client.get(reverse("clubs-history", args=(club.code,)))
self.assertIn(resp.status_code, [200], resp.content)
history = json.loads(resp.content.decode("utf-8"))
self.assertEqual(len(history), len(previous_history) + 1)
self.assertFalse(history[-1]["approved"])

# ensure club is marked as not approved
club.refresh_from_db()
Expand Down
123 changes: 123 additions & 0 deletions frontend/components/ClubPage/ClubApprovalDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { EmotionJSX } from '@emotion/react/types/jsx-namespace'
import moment from 'moment-timezone'
import { useRouter } from 'next/router'
import { ReactElement, useEffect, useState } from 'react'
import Select from 'react-select'
Expand All @@ -18,6 +20,7 @@ import {
SITE_NAME,
} from '../../utils/branding'
import { Contact, Icon, Modal, Text, TextQuote } from '../common'
import { Chevron } from '../DropdownFilter'
import { ModalContent } from './Actions'

type Props = {
Expand All @@ -30,9 +33,108 @@ type ConfirmParams = {
message: ReactElement | string
}

type HistoricItem = {
approved: boolean | null
approved_on: string | null
approved_by: string | null
approved_comment: string | null
history_date: string
}

const ClubHistoryDropdown = ({ history }: { history: HistoricItem[] }) => {
const [active, setActive] = useState<boolean>(false)
const [reason, setReason] = useState<string | null>(null)
const getReason = (item: HistoricItem): EmotionJSX.Element | string => {
return item.approved_comment ? (
item.approved_comment.length > 100 ? (
<span
style={{
cursor: 'pointer',
textDecoration: 'underline',
}}
onClick={() => setReason(item.approved_comment)}
>
View Reason
</span>
) : (
item.approved_comment
)
) : (
'No reason provided'
)
}
return (
<>
<div
style={{
cursor: 'pointer',
}}
className="mt-2"
onClick={() => setActive(!active)}
>
{active ? 'Hide' : 'Show'} History
<Chevron
name="chevron-down"
alt="toggle dropdown"
open={active}
color="inherit"
className="ml-1"
/>
</div>
<Modal
show={reason !== null}
closeModal={() => setReason(null)}
marginBottom={false}
width="80%"
>
<ModalContent>{reason}</ModalContent>
</Modal>
{active && (
<div style={{ maxHeight: '300px', overflowY: 'auto' }}>
{history.map((item, i) => (
<div
key={i}
className="mt-2"
style={{
fontSize: '70%',
}}
>
{item.approved === true ? (
<TextQuote className="py-0">
<b>Approved</b> by <b>{item.approved_by}</b> on{' '}
{moment(item.history_date)
.tz('America/New_York')
.format('LLL')}{' '}
- {getReason(item)}
</TextQuote>
) : item.approved === false ? (
<TextQuote className="py-0">
<b>Rejected</b> by <b>{item.approved_by}</b> on{' '}
{moment(item.history_date)
.tz('America/New_York')
.format('LLL')}{' '}
- {getReason(item)}
</TextQuote>
) : (
<TextQuote className="py-0">
<b>Submitted for re-approval</b> on{' '}
{moment(item.history_date)
.tz('America/New_York')
.format('LLL')}
</TextQuote>
)}
</div>
))}
</div>
)}
</>
)
}

const ClubApprovalDialog = ({ club }: Props): ReactElement | null => {
const router = useRouter()
const year = getCurrentSchoolYear()
const [history, setHistory] = useState<HistoricItem[]>([])
const [comment, setComment] = useState<string>(club.approved_comment || '')
const [loading, setLoading] = useState<boolean>(false)
const [confirmModal, setConfirmModal] = useState<ConfirmParams | null>(null)
Expand Down Expand Up @@ -64,6 +166,25 @@ const ClubApprovalDialog = ({ club }: Props): ReactElement | null => {
.then(setTemplates)
}

if (isOfficer || canApprove) {
doApiRequest(`/clubs/${club.code}/history/?format=json`)
.then((resp) => resp.json())
.then((data) => {
// Get last version of club for each change in approved status
const lastVersions: HistoricItem[] = []

for (let i = data.length - 1; i >= 0; i--) {
const item = data[i]
const lastItem = lastVersions[lastVersions.length - 1]

if (item.approved !== lastItem?.approved || !lastItem) {
lastVersions.push(item)
}
}
setHistory(lastVersions)
})
}

setComment(
selectedTemplates.map((template) => template.content).join('\n\n'),
)
Expand Down Expand Up @@ -130,6 +251,7 @@ const ClubApprovalDialog = ({ club }: Props): ReactElement | null => {
>
<Icon name="x" /> Revoke Approval
</button>
<ClubHistoryDropdown history={history} />
</div>
)}
{(club.active || canDeleteClub) && club.approved !== true ? (
Expand Down Expand Up @@ -366,6 +488,7 @@ const ClubApprovalDialog = ({ club }: Props): ReactElement | null => {
</button>
</>
)}
<ClubHistoryDropdown history={history} />
</div>
) : null}
{(seeFairStatus || isOfficer) && fairs.length > 0 && (
Expand Down
8 changes: 4 additions & 4 deletions frontend/components/DropdownFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,17 @@ const TableContainer = styled.div`
}
`

const Chevron = styled(Icon)<{ open?: boolean }>`
export const Chevron = styled(Icon)<{ open?: boolean; color?: string }>`
cursor: pointer;
color: ${CLUBS_GREY};
color: ${({ color }) => color ?? CLUBS_GREY};
transform: rotate(0deg) translateY(0);
transition: transform ${ANIMATION_DURATION}ms ease;
${({ open }) => open && 'transform: rotate(180deg) translateY(-4px);'}
${({ open }) => open && 'transform: rotate(180deg);'}

${mediaMaxWidth(MD)} {
margin-top: 0.1em !important;
margin-left: 0.1em !important;
color: ${LIGHT_GRAY};
color: ${({ color }) => color ?? LIGHT_GRAY};
${({ open }) => open && 'transform: rotate(180deg)'}
}
`
Expand Down
Loading