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

add combobox for locations #129

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
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 .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"version": "0.2.0",
"configurations": [

{
"name": "Next.js",
"type": "node-terminal",
Expand Down
25 changes: 22 additions & 3 deletions apps/web/src/app/_components/combo-box.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ interface ComboBoxProps {
currLabel: string;
onSelect: (option: string) => void;
triggerClassName?: string;
onChange?: (value: string) => void;
variant?: "default" | "form";
}

/**
Expand All @@ -41,9 +43,16 @@ export default function ComboBox({
currLabel,
onSelect,
triggerClassName,
onChange,
variant,
}: ComboBoxProps) {
const [isOpen, setIsOpen] = useState(false);

const styleVariant =
variant === "form"
? "flex h-16 w-full rounded-md border-[3px] border-cooper-blue-600 bg-white px-3 py-2 text-xl font-normal ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-0 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50"
: "";

return (
<Popover open={isOpen} onOpenChange={setIsOpen}>
<PopoverTrigger
Expand All @@ -57,15 +66,25 @@ export default function ComboBox({
variant="outline"
role="combobox"
aria-expanded={isOpen}
className="justify-between"
className={cn(
styleVariant,
"w-[400px] justify-between overflow-hidden text-ellipsis text-nowrap",
)}
>
{defaultLabel}
<span className="overflow-hidden text-ellipsis whitespace-nowrap">
{defaultLabel}
</span>
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</PopoverTrigger>
<PopoverContent className="w-[400px] p-0">
<Command>
<CommandInput placeholder={searchPlaceholder} />
<CommandInput
placeholder={searchPlaceholder}
onChangeCapture={(e) =>
onChange && onChange((e.target as HTMLInputElement).value)
}
/>
<CommandEmpty>{searchEmpty}</CommandEmpty>
<CommandGroup>
<CommandList>
Expand Down
63 changes: 59 additions & 4 deletions apps/web/src/app/_components/form/sections/review-section.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
"use client";

import { useEffect, useState } from "react";
import { useFormContext } from "react-hook-form";

import {
Expand All @@ -10,14 +13,46 @@ import {
import { Input } from "@cooper/ui/input";
import { Textarea } from "@cooper/ui/textarea";

import ComboBox from "~/app/_components/combo-box";
import { FormSection } from "~/app/_components/form/form-section";
import { api } from "~/trpc/react";

/**
* ReviewSection component renders form fields for writing a co-op review.
*/
export function ReviewSection({ textColor }: { textColor: string }) {
const form = useFormContext();

const [locationLabel, setLocationLabel] = useState<string>("");
const [searchTerm, setSearchTerm] = useState<string>("");
const [prefix, setPrefix] = useState<string>("");

useEffect(() => {
const newPrefix =
searchTerm.length === 3 ? searchTerm.slice(0, 3).toLowerCase() : null;
if (newPrefix && newPrefix !== prefix) {
setPrefix(newPrefix);
}
}, [searchTerm]);

const locationsToUpdate = api.location.getByPrefix.useQuery(
{ prefix },
{ enabled: searchTerm.length === 3 },
);

const locationValuesAndLabels = locationsToUpdate.data
? locationsToUpdate.data.map((location) => {
return {
value: location.id,
label:
location.city +
(location.state ? `, ${location.state}` : "") +
", " +
location.country,
};
})
: [];

return (
<FormSection title="Review" className={textColor}>
<FormField
Expand Down Expand Up @@ -49,17 +84,37 @@ export function ReviewSection({ textColor }: { textColor: string }) {
<div className="flex justify-between space-x-2">
<FormField
control={form.control}
name="location"
name="locationId"
render={({ field }) => (
<FormItem>
<FormLabel>Location</FormLabel>
<FormControl>
<Input {...field} />
<ComboBox
variant="form"
defaultLabel={locationLabel || "Select location..."}
searchPlaceholder="Search location..."
searchEmpty="No location found."
valuesAndLabels={locationValuesAndLabels}
currLabel={locationLabel}
onChange={(value) => {
setSearchTerm(value);
}}
onSelect={(currentValue) => {
setLocationLabel(
currentValue === locationLabel ? "" : currentValue,
);
field.onChange(
locationValuesAndLabels.find(
(location) => location.label === currentValue,
)?.value,
);
console.log(field.value);
}}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
></FormField>
<FormField
control={form.control}
name="hourlyPay"
Expand Down
20 changes: 18 additions & 2 deletions apps/web/src/app/_components/reviews/review-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import type { ReviewType } from "@cooper/db/schema";
import { Card, CardContent } from "@cooper/ui/card";

import { api } from "~/trpc/react";
import { ReviewCardStars } from "./review-card-stars";

// const InterviewDifficulty = [
Expand All @@ -19,6 +20,21 @@ interface ReviewCardProps {
}

export function ReviewCard({ reviewObj }: ReviewCardProps) {
// ===== LOCATION DATA ===== //
const locationName = (reviewObj: ReviewType) => {
if (reviewObj.locationId) {
const { data: location } = api.location.getById.useQuery({
id: reviewObj.locationId,
});
return location
? location.city +
(location.state ? `, ${location.state}` : "") +
", " +
location.country
: "N/A";
}
};

return (
<Card className="mx-auto w-[94%]">
<div className="flex pt-5">
Expand All @@ -29,10 +45,10 @@ export function ReviewCard({ reviewObj }: ReviewCardProps) {
</div>
<div className="align-center flex gap-2 pt-2">
<span
className={`${reviewObj.location ? "visibility: visible" : "visibility: hidden"}`}
className={`${locationName(reviewObj) ? "visibility: visible" : "visibility: hidden"}`}
>
{" "}
{reviewObj.location} •
{locationName(reviewObj)} •
</span>
<span>{reviewObj.workTerm}</span>
</div>
Expand Down
23 changes: 20 additions & 3 deletions apps/web/src/app/_components/reviews/role-card-preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import Image from "next/image";

import type { RoleType } from "@cooper/db/schema";
import type { ReviewType, RoleType } from "@cooper/db/schema";
import { cn } from "@cooper/ui";
import { Card, CardContent, CardHeader, CardTitle } from "@cooper/ui/card";

Expand All @@ -26,6 +26,21 @@ export function RoleCardPreview({
const role = api.role.getById.useQuery({ id: reviewObj.id });
const reviews = api.review.getByRole.useQuery({ id: reviewObj.id });

// ===== LOCATION DATA ===== //
const locationName = (reviewObj: ReviewType) => {
if (reviewObj.locationId) {
const { data: location } = api.location.getById.useQuery({
id: reviewObj.locationId,
});
return location
? location.city +
(location.state ? `, ${location.state}` : "") +
", " +
location.country
: "N/A";
}
};

return (
<Card
className={cn(
Expand All @@ -49,13 +64,15 @@ export function RoleCardPreview({
<span>{company.data?.name}</span>
{reviews.isSuccess && reviews.data.length > 0 && (
<span
className={`${reviews.data[0]?.location ? "visibility: visible" : "visibility: hidden"}`}
className={`${(reviews.data[0] ? locationName(reviews.data[0]) : "") ? "visibility: visible" : "visibility: hidden"}`}
>
</span>
)}
{reviews.isSuccess && reviews.data.length > 0 && (
<span>{reviews.data[0]?.location}</span>
<span>
{reviews.data[0] ? locationName(reviews.data[0]) : ""}
</span>
)}
</div>
</div>
Expand Down
23 changes: 20 additions & 3 deletions apps/web/src/app/_components/reviews/role-info.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import Image from "next/image";

import type { RoleType } from "@cooper/db/schema";
import type { ReviewType, RoleType } from "@cooper/db/schema";
import { cn } from "@cooper/ui";
import { Card, CardContent, CardHeader, CardTitle } from "@cooper/ui/card";

Expand Down Expand Up @@ -36,6 +36,21 @@ export function RoleInfo({ className, roleObj }: RoleCardProps) {

const companyData = companyQuery.data;

// ===== LOCATION DATA ===== //
const locationName = (reviewObj: ReviewType) => {
if (reviewObj.locationId) {
const { data: location } = api.location.getById.useQuery({
id: reviewObj.locationId,
});
return location
? location.city +
(location.state ? `, ${location.state}` : "") +
", " +
location.country
: "N/A";
}
};

return (
<Card
className={cn(
Expand Down Expand Up @@ -71,13 +86,15 @@ export function RoleInfo({ className, roleObj }: RoleCardProps) {
<span>{companyData?.name}</span>
{reviews.isSuccess && reviews.data.length > 0 && (
<span
className={`${reviews.data[0]?.location ? "visibility: visible" : "visibility: hidden"}`}
className={`${(reviews.data[0] ? locationName(reviews.data[0]) : "") ? "visibility: visible" : "visibility: hidden"}`}
>
</span>
)}
{reviews.isSuccess && reviews.data.length > 0 && (
<span>{reviews.data[0]?.location}</span>
<span>
{reviews.data[0] ? locationName(reviews.data[0]) : ""}
</span>
)}
</div>
</div>
Expand Down
17 changes: 16 additions & 1 deletion apps/web/src/app/_components/reviews/role-review-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ export async function RoleReviewCard({

if (!company) return null;

// ===== LOCATION DATA ===== //
const locationName = async (locationId: string) => {
if (locationId) {
const location = await api.location.getById({
id: locationId,
});
return location
? location.city +
(location.state ? `, ${location.state}` : "") +
", " +
location.country
: "N/A";
}
};

return (
<Card
className={cn(
Expand Down Expand Up @@ -79,7 +94,7 @@ export async function RoleReviewCard({
<div className="m-4 flex items-center space-x-8">
<div className="flex flex-col text-sm">
<h4 className="font-semibold">Location</h4>
<p>{company.location}</p>
<p>{locationName(company.location)}</p>
</div>
<div className="flex flex-col text-sm">
<h4 className="font-semibold">Work Model</h4>
Expand Down
2 changes: 2 additions & 0 deletions packages/api/src/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
profileRouter,
reviewRouter,
roleRouter,
locationRouter,
} from "./router";
import { createTRPCRouter } from "./trpc";

Expand All @@ -13,6 +14,7 @@ export const appRouter = createTRPCRouter({
role: roleRouter,
profile: profileRouter,
review: reviewRouter,
location: locationRouter,
});

// export type definition of API
Expand Down
3 changes: 2 additions & 1 deletion packages/api/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import { companyRouter } from "./company";
import { profileRouter } from "./profile";
import { reviewRouter } from "./review";
import { roleRouter } from "./role";
import { locationRouter } from "./location";

export { authRouter, companyRouter, profileRouter, reviewRouter, roleRouter };
export { authRouter, companyRouter, profileRouter, reviewRouter, roleRouter, locationRouter };
38 changes: 38 additions & 0 deletions packages/api/src/router/location.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { TRPCRouterRecord } from "@trpc/server";
import { z } from "zod";

import { asc, eq } from "@cooper/db";
import { CreateLocationSchema, Location } from "@cooper/db/schema";

import { protectedProcedure, publicProcedure } from "../trpc";

export const locationRouter = {
list: publicProcedure.query(({ ctx }) => {
return ctx.db.query.Location.findMany({
orderBy: asc(Location.city),
});
}),

getById: publicProcedure
.input(z.object({ id: z.string() }))
.query(({ ctx, input }) => {
return ctx.db.query.Location.findFirst({
where: eq(Location.id, input.id),
});
}),

getByPrefix: publicProcedure
.input(z.object({ prefix: z.string() }))
.query(({ ctx, input }) => {
return ctx.db.query.Location.findMany({
where: (loc, { ilike }) => ilike(loc.city, `${input.prefix}%`),
orderBy: asc(Location.city),
});
}),

create: protectedProcedure
.input(CreateLocationSchema)
.mutation(({ ctx, input }) => {
return ctx.db.insert(Location).values(input);
}),
} satisfies TRPCRouterRecord;
Loading