Skip to content

Commit

Permalink
changes from martin review
Browse files Browse the repository at this point in the history
  • Loading branch information
alasdairwilson committed Oct 27, 2023
1 parent 0cd6df9 commit 7c40c4e
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 46 deletions.
71 changes: 43 additions & 28 deletions components/EnrolDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { HiMail, HiX } from 'react-icons/hi'
import postUserOnEvent from 'lib/actions/postUserOnEvent'
import putUserOnEvent from 'lib/actions/putUserOnEvent'
import useEvent from 'lib/hooks/useEvent'
import { useForm } from 'react-hook-form'
import Stack from './ui/Stack'
import TextField from './forms/Textfield'


type Props = {
Expand All @@ -16,7 +19,12 @@ type Props = {
onEnrol: (userOnEvent: UserOnEvent | undefined) => void
}

interface Enrol {
enrolKey: string
}

const EnrolDialog: React.FC<Props> = ({ event, show, onEnrol}) => {
const { control, handleSubmit, reset } = useForm<Enrol>();
const [error, setError] = React.useState<string | undefined>(undefined)
const [enrolError, setEnrolError] = React.useState<string | undefined>(undefined)
const [success, setSuccess] = React.useState<string | null>(null)
Expand Down Expand Up @@ -59,9 +67,8 @@ const EnrolDialog: React.FC<Props> = ({ event, show, onEnrol}) => {
})
};

const enrolWithKey = () => {
const enrolKey = (document.getElementById('enrol-key') as HTMLInputElement).value;
const status = checkKey(enrolKey)
const enrolWithKey = (data: Enrol) => {
const status = checkKey(data.enrolKey)
if (status === null) {
setEnrolError("error")
} else {
Expand Down Expand Up @@ -103,33 +110,41 @@ const EnrolDialog: React.FC<Props> = ({ event, show, onEnrol}) => {
{event.name}
</Modal.Header>
<Modal.Body>
<Content markdown={event.enrol} />
You should have received an enrolment key from the course organiser:
<div className="flex">
<TextInput
className="w-[80%]"
id="enrol-key"
sizing="sm"
type="text"
placeholder='Enrolment Key'
/>
<Button className="w-[15%] h-8" onClick={enrolWithKey}>
Enrol
</Button>
</div>
{ enrolError && (
<Toast className=''>
<div className="inline-flex h-8 w-8 shrink-0 items-center justify-center rounded-lg bg-red-100 text-red-500 dark:bg-red-800 dark:text-red-200">
<HiX className="h-5 w-5" />
</div>
<div className="ml-3 text-sm font-normal">
Invalid Enrolment Key
</div>
</Toast>
)}
<Stack>
<Content markdown={event.enrol} />
<p>
You should have received an enrolment key from the course organiser.
</p>
<form onSubmit={handleSubmit(enrolWithKey)}>
<TextField
textfieldProps={{className: "w-[75%]",
autoComplete: "off",
placeholder: "Enrolment Key",
autoFocus: true,
}}
name={"enrolKey"}
control={control}
/>
<Button type="submit" className="m-0 h-10" style={{verticalAlign: "middle"}}>
Enrol
</Button>
</form>
{ enrolError && (
<Toast className=''>
<div className="inline-flex h-8 w-8 shrink-0 items-center justify-center rounded-lg bg-red-100 text-red-500 dark:bg-red-800 dark:text-red-200">
<HiX className="h-5 w-5" />
</div>
<div className="ml-3 text-sm font-normal">
Invalid Enrolment Key
</div>
</Toast>
)}
</Stack>
</Modal.Body>
<Modal.Footer>
If you have not received an enrolment key, you can request enrolment:
<p>
If you have not received an enrolment key, you can request enrolment:
</p>
<Button onClick={onClick}>
Request Enrollment
</Button>
Expand Down
4 changes: 3 additions & 1 deletion components/forms/Textfield.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ type Props<T extends FieldValues> = {
name: FieldPath<T>;
control: Control<T>;
rules?: Object;
textfieldProps?: Object;
};

function Textfield<T extends FieldValues>({ label, name, control, rules }: Props<T>): React.ReactElement {
function Textfield<T extends FieldValues>({ label, name, control, rules, textfieldProps }: Props<T>): React.ReactElement {
return (
<div data-cy={`textfield-${name}`}>
<Controller
Expand All @@ -31,6 +32,7 @@ function Textfield<T extends FieldValues>({ label, name, control, rules }: Props
value={value === undefined || value === null ? '' : value}
onChange={onChange}
onBlur={onBlur}
{...textfieldProps}
/>
</>
);
Expand Down
1 change: 0 additions & 1 deletion lib/actions/putEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Data } from 'pages/api/event/[eventId]'

// function that returns a promise that does a PUT request for this endpoint
export const putEvent = async (event: Event): Promise<Data> => {
console.log('insideputeventt', event)
const apiPath = `${basePath}/api/event/${event.id}`
const requestOptions = {
method: 'PUT',
Expand Down
1 change: 0 additions & 1 deletion pages/api/eventGroup/[eventGroupId].ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ const eventHandler = async (
eventItem.order = parseInt(eventItem.order);
}
});
console.log('req.body', req.body)
const updatedEventGroup = await prisma.eventGroup.update({
where: { id: parseInt(eventGroupId) },
data: {
Expand Down
13 changes: 0 additions & 13 deletions pages/api/userOnEvent/[eventId].ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,6 @@ const UserOnEvent = async (req: NextApiRequest, res: NextApiResponse<Data>) => {

const isAdmin = currentUser?.admin === true;

switch (method) {
case 'GET':
break;
case 'POST':
break;
case 'PUT':
break;
default:
res.setHeader('Allow', ['GET', 'POST', 'PUT']);
res.status(404).end(`Method ${method} Not Allowed`);
break;
}

let updatedUserOnEvent = null;
let userOnEvent = null;
switch (method) {
Expand Down
2 changes: 0 additions & 2 deletions pages/event/[eventId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ const Event: NextPage<EventProps> = ({ material, event }) => {
const isAdmin = userProfile?.admin;

const onSubmit = (data: EventWithUsers) => {
console.log('onSubmit', data)
putEvent(data).then((data) => {
console.log('putEvent', data)
data.event && mutateEvent(data.event)
});
}
Expand Down

0 comments on commit 7c40c4e

Please sign in to comment.