Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

feat: inserting a question in a table #138

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
NEXT_PUBLIC_SUPABASE_URL=YOUR_SUPABASE_URL
NEXT_PUBLIC_SUPABASE_ANON_KEY=YOUR_SUPABASE_ANON_KEY
NEXT_PRIVATE_SUPABASE_SERVICE_KEY=YOUR_SUPABASE_SERVICE_KEY
41 changes: 41 additions & 0 deletions src/pages/api/submit_question.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import { createClient, SupabaseClient } from '@supabase/supabase-js';

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { NextApiRequest, NextApiResponse } from 'next';

// TODO: move the following somewhere else.
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL || '';
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY || '';
const supabaseServiceKey = process.env.NEXT_PRIVATE_SUPABASE_SERVICE_KEY || '';
const supabase = createClient(supabaseUrl, supabaseAnonKey);
const supabaseSecret = createClient(supabaseUrl, supabaseServiceKey);

export default async function handler(req, res) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export default async function handler(req, res) {
export default async function handler(req: NextApiRequest, res: NextApiResponse) {

Always nice to have :)

// data format:
// {
// company: "a",
// location: "a",
// position: "b",
// question: "a",
// question_details: "",
// recency-weeks: "2",
// stay_anonymous: false
// }
const input_data = JSON.parse(req.body);
const jwt = req.headers.authentication;
const asked_date = new Date(new Date().setDate(new Date().getDate() - Number(input_data['recency-weeks']) * 7));

input_data['asked_date'] = asked_date;
delete input_data['recency-weeks'];

const { data: user, userError } = await supabase.auth.api.getUser(jwt);
const id = user.identities[0]['id'];

input_data['created_by'] = id;

const { data, error } = await supabaseSecret.from('questions').insert([input_data]);

if (error) {
res.status(500).json({ error });
} else {
res.status(200).json({ message: 'success!' });
}
}
25 changes: 0 additions & 25 deletions src/pages/api/submit_question.ts

This file was deleted.

31 changes: 16 additions & 15 deletions src/pages/submit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,22 @@ const Submit: NextPage<UserProps> = ({ session }) => {
</div>

<div className='sm:col-span-3'>
<label htmlFor='recency' className='block text-sm font-medium text-gray-700'>
<label htmlFor='recency-weeks' className='block text-sm font-medium text-gray-700'>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<label htmlFor='recency-weeks' className='block text-sm font-medium text-gray-700'>
<label htmlFor='recency_weeks' className='block text-sm font-medium text-gray-700'>

change here also as you have done in the below fields.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@debasishbsws thank you but this is an old PR so I'll close it

How recently was this asked?
</label>
<div className='mt-1'>
<select
id='recency'
{...register('recency', { required: true })}
id='recency-weeks'
{...register('recency-weeks', { required: true })}
Comment on lines +105 to +106
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
id='recency-weeks'
{...register('recency-weeks', { required: true })}
id='recency_weeks'
{...register('recency_weeks', { required: true })}

className='block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm'
>
<option>Within the past week</option>
<option>Within the past month</option>
<option>1-2 months ago</option>
<option>3-6 months ago</option>
<option>7-12 months ago</option>
<option>1+ year ago</option>
{/* value is estimated # weeks since the question was asked */}
<option value='1'>Within the past week</option>
<option value='2'>Within the past month</option>
<option value='6'>1-2 months ago</option>
<option value='18'>3-6 months ago</option>
<option value='38'>7-12 months ago</option>
<option value='60'>1+ year ago</option>
</select>
</div>
</div>
Expand All @@ -131,13 +132,13 @@ const Submit: NextPage<UserProps> = ({ session }) => {
</div>

<div className='sm:col-span-6'>
<label htmlFor='question-details' className='block text-sm font-medium text-gray-700'>
<label htmlFor='question_details' className='block text-sm font-medium text-gray-700'>
Question details
</label>
<div className='mt-1'>
<textarea
id='question-details'
{...register('question-details')}
id='question_details'
{...register('question_details')}
rows={3}
className='block w-full rounded-md border border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm'
defaultValue={''}
Expand All @@ -148,14 +149,14 @@ const Submit: NextPage<UserProps> = ({ session }) => {
<div className='relative mt-6 flex'>
<div className='flex h-5 items-center'>
<input
id='stay-anonymous'
{...register('stay-anonymous')}
id='stay_anonymous'
{...register('stay_anonymous')}
type='checkbox'
className='h-4 w-4 rounded border-gray-300 text-indigo-600 focus:ring-indigo-500'
/>
</div>
<div className='ml-3 text-sm'>
<label htmlFor='stay-anonymous' className='font-medium text-gray-700'>
<label htmlFor='stay_anonymous' className='font-medium text-gray-700'>
Stay anonymous
</label>
</div>
Expand Down