Skip to content

Commit

Permalink
added page on onboarding, profile.ts for supabase queries
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin3656 committed Oct 13, 2024
1 parent 82dc058 commit 2b7378b
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 0 deletions.
13 changes: 13 additions & 0 deletions api/supabase/queries/profiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Profile } from '@/types/schema';
import supabase from '../createClient';

export async function upsertProfile(profile: Profile) {
const { data, error } = await supabase
.from('profiles')
.upsert(profile)
.select();

if (error) throw new Error(`Error upserting profile data: ${error.message}`);

return data[0];
}
179 changes: 179 additions & 0 deletions app/onboarding/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
'use client';

import React, { useState } from 'react';
import { upsertProfile } from '@/api/supabase/queries/profiles';
import { Profile } from '@/types/schema';

type UUID = `${string}-${string}-${string}-${string}-${string}`;
const generateUUID = (): UUID => {
return crypto.randomUUID() as UUID;
};
const id = generateUUID();

// Define the possible options for each question
const states = ['Tennessee', 'Missouri'];
const gardenTypes = ['Individual', 'Community', 'School'];
const plotOptions = [
{ label: 'yes', value: true },
{ label: 'no', value: false },
];
//Interfaces and props to avoid typ errors on Lint
interface StateSelectionProps {
selectedState: string;
setSelectedState: React.Dispatch<React.SetStateAction<string>>;
}

interface GardenTypeSelectionProps {
selectedGardenType: string;
setSelectedGardenType: React.Dispatch<React.SetStateAction<string>>;
}

interface PlotSelectionProps {
selectedPlot: boolean | null;
setSelectedPlot: React.Dispatch<React.SetStateAction<boolean>>;
}

// Select State
const StateSelection: React.FC<StateSelectionProps> = ({
selectedState,
setSelectedState,
}) => {
return (
<div>
<h2>Which state do you live in?</h2>
<select
value={selectedState}
onChange={e => setSelectedState(e.target.value)}
>
<option value="" disabled>
Select a state
</option>
{states.map(state => (
<option key={state} value={state}>
{state}
</option>
))}
</select>
</div>
);
};

// Step 2: Select garden type

const GardenTypeSelection: React.FC<GardenTypeSelectionProps> = ({
selectedGardenType,
setSelectedGardenType,
}) => {
return (
<div>
<h2>What type of garden do you want to create?</h2>
{gardenTypes.map(type => (
<label key={type}>
<input
type="radio"
name="gardenType"
value={type}
checked={selectedGardenType === type}
onChange={e => setSelectedGardenType(e.target.value)}
/>
{type}
</label>
))}
</div>
);
};

// Step 3: Do you have a plot?
const PlotSelection: React.FC<PlotSelectionProps> = ({
selectedPlot,
setSelectedPlot,
}) => {
return (
<div>
<h2>Do you already have a plot?</h2>
{plotOptions.map(option => (
<label key={option.label}>
<input
type="radio"
name="plot"
value={String(option.value)}
checked={selectedPlot === option.value}
onChange={() => setSelectedPlot(option.value)}
/>
{option.label}
</label>
))}
</div>
);
};

// Main Onboarding Component
const OnboardingFlow = () => {
const [step, setStep] = useState(1);
const [selectedState, setSelectedState] = useState<string>('');
const [selectedGardenType, setSelectedGardenType] = useState<string>('');
const [selectedPlot, setSelectedPlot] = useState<boolean>(false);

const handleNext = () => {
setStep(step + 1);
};

const handleBack = () => {
setStep(step - 1);
};

const handleSubmit = async () => {
const profile: Profile = {
user_id: id,
state: selectedState,
email: '',
phone_num: '',
user_type: selectedGardenType,
has_plot: selectedPlot,
};
try {
const updatedProfile = await upsertProfile(profile);
console.log('Profile successfully upserted:', updatedProfile);
} catch (err) {
console.error('Error upserting profile:', err);
} finally {
}
console.log('Submitted data: ', profile);
// Handle form submission, e.g., send to a server or display a confirmation
};

return (
<div>
{step === 1 && (
<StateSelection
selectedState={selectedState}
setSelectedState={setSelectedState}
/>
)}
{step === 2 && (
<GardenTypeSelection
selectedGardenType={selectedGardenType}
setSelectedGardenType={setSelectedGardenType}
/>
)}
{step === 3 && (
<PlotSelection
selectedPlot={selectedPlot}
setSelectedPlot={setSelectedPlot}
/>
)}

<div>
{step > 1 && <button onClick={handleBack}>Back</button>}
{step < 3 && (
<button onClick={handleNext} disabled={!selectedState && step === 1}>
Next
</button>
)}
{step === 3 && <button onClick={handleSubmit}>Submit</button>}
</div>
</div>
);
};

export default OnboardingFlow;
2 changes: 2 additions & 0 deletions types/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import type { UUID } from 'crypto';
export type Season = 'SPRING' | 'SUMMER' | 'FALL' | 'WINTER';

export interface Profile {
user_id: UUID;
state: string;
email: string;
phone_num: string;
user_type: string;
has_plot: boolean;
}

export interface Plant {
Expand Down

0 comments on commit 2b7378b

Please sign in to comment.