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

feat: ability to add phone number in profile πŸ“² #482

Merged
Show file tree
Hide file tree
Changes from 5 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
9 changes: 9 additions & 0 deletions apps/member-profile/app/routes/_profile.profile.general.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
} from '@/shared/components/profile';
import {
CurrentLocationField,
PhoneNumberField,
PreferredNameField,
} from '@/shared/components/profile.general';
import { getMember } from '@/shared/queries/index';
Expand All @@ -53,6 +54,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
'headline',
'lastName',
'preferredName',
'phoneNumber',
])
.executeTakeFirstOrThrow();

Expand All @@ -73,6 +75,7 @@ const UpdateGeneralInformation = Student.pick({
headline: true,
lastName: true,
preferredName: true,
phoneNumber: true,
}).extend({
currentLocation: Student.shape.currentLocation.unwrap(),
currentLocationLatitude: Student.shape.currentLocationLatitude.unwrap(),
Expand Down Expand Up @@ -174,6 +177,12 @@ export default function UpdateGeneralInformationSection() {
longitudeName={keys.currentLocationLongitude}
/>

<PhoneNumberField
defaultValue={student.phoneNumber || undefined}
error={errors.phoneNumber}
name={keys.phoneNumber}
/>

ramiAbdou marked this conversation as resolved.
Show resolved Hide resolved
<Button.Group>
<Button.Submit>Save</Button.Submit>
</Button.Group>
Expand Down
43 changes: 42 additions & 1 deletion apps/member-profile/app/shared/components/profile.general.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from 'react';

import { CityCombobox, type CityComboboxProps } from '@oyster/core/location/ui';
import { type FieldProps, Form, Input, Text } from '@oyster/ui';
import { type FieldProps, Form, Input, InputField, Text } from '@oyster/ui';

export function CurrentLocationField({
defaultValue,
Expand Down Expand Up @@ -62,3 +62,44 @@ export function PreferredNameField({
</Form.Field>
);
}

const formatPhoneNumber = (input: string) => {
const cleaned = input.replace(/\D/g, '');

if (cleaned.length == 0) {
return '';
} else if (cleaned.length <= 3) {
return `(${cleaned}`;
} else if (cleaned.length <= 6) {
return `(${cleaned.slice(0, 3)})-${cleaned.slice(3)}`;
} else {
return `(${cleaned.slice(0, 3)})-${cleaned.slice(3, 6)}-${cleaned.slice(6, 10)}`;
}
};

export const PhoneNumberField = ({
defaultValue,
error,
name,
}: FieldProps<string>) => {
const [phoneNumber, setPhoneNumber] = useState(defaultValue || '');

const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const formatted = formatPhoneNumber(e.target.value);

setPhoneNumber(formatted);
};

return (
<InputField
value={phoneNumber}
onChange={handleChange}
error={error}
label="Phone Number"
name={name}
description="Enter your 10-digit phone number."
placeholder="(555)-123-4567"
type="tel"
/>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { type Kysely } from 'kysely';

export async function up(db: Kysely<any>) {
await db.schema
.alterTable('students')
.addColumn('phone_number', 'text')
.execute();
}

export async function down(db: Kysely<any>) {
await db.schema.alterTable('students').dropColumn('phone_number').execute();
}
8 changes: 8 additions & 0 deletions packages/types/src/domain/student.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ export const Student = Entity.merge(StudentSocialLinks)
joinedMemberDirectoryAt: z.coerce.date().nullable(),
joinedSlackAt: z.coerce.date().optional(),
lastName: z.string().trim().min(1),
phoneNumber: z
.string()
.trim()
.regex(
/^\(\d{3}\)-\d{3}-\d{4}$/,
'Phone Number must be in the format (555)-123-4567'
)
.optional(),

/**
* Enum that represents all of the accepted majors from the ColorStack
Expand Down
13 changes: 12 additions & 1 deletion packages/ui/src/components/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ Form.Field = function FormField({

type InputFieldProps = FieldProps<string> &
Pick<FormFieldProps, 'description' | 'label' | 'required'> &
Pick<InputProps, 'disabled' | 'placeholder'>;
Pick<
InputProps,
'disabled' | 'placeholder' | 'type' | 'pattern' | 'onChange' | 'value'
>;

export function InputField({
defaultValue,
Expand All @@ -82,6 +85,10 @@ export function InputField({
name,
placeholder,
required,
pattern,
type = 'text',
onChange,
value,
}: InputFieldProps) {
return (
<Form.Field
Expand All @@ -98,6 +105,10 @@ export function InputField({
name={name}
placeholder={placeholder}
required={required}
type={type}
pattern={pattern}
onChange={onChange}
value={value}
/>
</Form.Field>
);
Expand Down
3 changes: 2 additions & 1 deletion packages/ui/src/components/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ export type InputProps = Pick<
| 'required'
| 'type'
| 'value'
| 'pattern'
> & {
type?: Extract<HTMLInputTypeAttribute, 'email' | 'number' | 'text'>;
type?: Extract<HTMLInputTypeAttribute, 'email' | 'number' | 'text' | 'tel'>;
};

export const Input = React.forwardRef(
Expand Down