Skip to content

Commit

Permalink
Change AuthorEmail TextBox to Select
Browse files Browse the repository at this point in the history
- Change authorEmail Input field to select
- This is much better as Author email must exist in users table
- fix imports
  • Loading branch information
mayank1513 committed Jul 21, 2023
1 parent 54f510d commit e485fa0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { NextApiRequest, NextApiResponse } from 'next'
import prisma from '@/@/lib/prisma'
import prisma from '@/lib/prisma'

export default async function handle(
req: NextApiRequest,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { NextApiRequest, NextApiResponse } from 'next'
import prisma from '@/@/lib/prisma'
import prisma from '@/lib/prisma'

// POST /api/post
// Required fields in body: title, authorEmail
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { NextApiRequest, NextApiResponse } from 'next'
import prisma from '@/@/lib/prisma'
import prisma from '@/lib/prisma'

// PUT /api/publish/:id
export default async function handle(
Expand Down
34 changes: 27 additions & 7 deletions typescript/rest-nextjs-api-routes/src/pages/create.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import React, { useState } from 'react'
import Router from 'next/router'
import styles from '@/styles/Draft.module.css'
import { GetServerSideProps } from 'next'
import prisma from '@/lib/prisma'

export default function Draft() {
type Props = {
users: { email: string }[]
}

export default function CreateDraft(props: Props) {
const [title, setTitle] = useState('')
const [content, setContent] = useState('')
const [authorEmail, setAuthorEmail] = useState('')
Expand Down Expand Up @@ -33,12 +39,19 @@ export default function Draft() {
type="text"
value={title}
/>
<input
onChange={(e) => setAuthorEmail(e.target.value)}
placeholder="Author (email address)"
type="text"
value={authorEmail}
/>
<label>
Author Email:{' '}
<select
value={authorEmail}
onChange={(e) => setAuthorEmail(e.target.value)}
>
{props.users.map(({ email }, i) => (
<option value={email} key={email + i}>
{email}
</option>
))}
</select>
</label>
<textarea
cols={50}
onChange={(e) => setContent(e.target.value)}
Expand All @@ -58,3 +71,10 @@ export default function Draft() {
</div>
)
}

export const getServerSideProps: GetServerSideProps = async () => {
const users = await prisma.user.findMany()
return {
props: { users },
}
}
2 changes: 2 additions & 0 deletions typescript/rest-nextjs-api-routes/src/styles/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ textarea {
font-size: 16px;
}

select,
input[type='text'],
textarea {
width: 100%;
padding: 0.5rem;
margin: 0.5rem 0;
border-radius: 0.25rem;
border: 0.125rem solid rgba(0, 0, 0, 0.2);
background: white;
}

input[type='submit'] {
Expand Down

0 comments on commit e485fa0

Please sign in to comment.