Skip to content

Commit

Permalink
make use of the refactored Employer type
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonyray committed Mar 14, 2024
1 parent 28bc22b commit 4d1a9fa
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
5 changes: 2 additions & 3 deletions shared/src/types/employers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { DocumentReference, Timestamp } from 'firebase-admin/firestore';
export const EMPLOYERS_FIRESTORE_PATH = 'employers';

export type Employer = {
userId: DocumentReference;
isCurrent: boolean;
employerName: string;
is_current: boolean;
employer_name: string;
created: Timestamp;
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';

import { zodResolver } from '@hookform/resolvers/zod';
import { EMPLOYERS_FIRESTORE_PATH } from '@socialincome/shared/src/types/employers';
import { EMPLOYERS_FIRESTORE_PATH, Employer } from '@socialincome/shared/src/types/employers';
import { USER_FIRESTORE_PATH } from '@socialincome/shared/src/types/user';
import { Button, Form, FormControl, FormField, FormItem, FormMessage, Input } from '@socialincome/ui';
import { Timestamp, addDoc, collection, doc } from 'firebase/firestore';
Expand All @@ -23,27 +23,27 @@ export function AddEmployerForm({ onNewEmployerSubmitted, translations }: AddEmp
const { user } = useUserContext();

const formSchema = z.object({
employerName: z.string().trim().min(1), // TODO : security
employer_name: z.string().trim().min(1), // TODO : security
});
type FormSchema = z.infer<typeof formSchema>;

const form = useForm<FormSchema>({
resolver: zodResolver(formSchema),
defaultValues: {
employerName: '',
employer_name: '',
},
});

const onSubmit = async (values: FormSchema) => {
if (user) {
const userId = user!.id;

await addDoc(collection(firestore, USER_FIRESTORE_PATH, userId, EMPLOYERS_FIRESTORE_PATH), {
...values,
isCurrent: true,
userId: doc(firestore, USER_FIRESTORE_PATH, userId),
const user_id = user!.id;
let new_employer: Employer = {
employer_name: values.employer_name,
is_current: true,
created: Timestamp.now(),
}).then(() => {
};

await addDoc(collection(firestore, USER_FIRESTORE_PATH, user_id, EMPLOYERS_FIRESTORE_PATH), new_employer).then(() => {
form.reset();
onNewEmployerSubmitted();
});
Expand All @@ -55,7 +55,7 @@ export function AddEmployerForm({ onNewEmployerSubmitted, translations }: AddEmp
<form className="flex flex-col gap-x-4 md:flex-row" onSubmit={form.handleSubmit(onSubmit)}>
<FormField
control={form.control}
name="employerName"
name="employer_name"
render={({ field }) => (
<FormItem className="grow">
<FormControl>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';

import { UserContext } from '@/app/[lang]/[region]/(website)/me/user-context-provider';
import { EMPLOYERS_FIRESTORE_PATH } from '@socialincome/shared/src/types/employers';
import { EMPLOYERS_FIRESTORE_PATH, Employer } from '@socialincome/shared/src/types/employers';

import { USER_FIRESTORE_PATH } from '@socialincome/shared/src/types/user';
import { Table, TableBody, TableCell, TableRow, Typography } from '@socialincome/ui';
Expand All @@ -24,6 +24,10 @@ type EmployersListProps = {
};
} & DefaultParams;

type EmployerWithId = {
id: string
} & Employer;

export function EmployersList({ translations }: EmployersListProps) {
const firestore = useFirestore();
const { user } = useContext(UserContext);
Expand All @@ -47,9 +51,9 @@ export function EmployersList({ translations }: EmployersListProps) {
await deleteDoc(employerRef).then(() => onEmployersUpdated());
};

const onArchiveEmployer = async (employer_id: string) => {
const onArchiveEmployer = async (employer_id: string) => { // Not leveraging type system ....
const employerRef = doc(firestore, USER_FIRESTORE_PATH, user!.id, EMPLOYERS_FIRESTORE_PATH, employer_id);
await updateDoc(employerRef, { isCurrent: false }).then(() => onEmployersUpdated());
await updateDoc(employerRef, { is_current: false }).then(() => onEmployersUpdated());
};

const onEmployersUpdated = async () => {
Expand All @@ -60,8 +64,12 @@ export function EmployersList({ translations }: EmployersListProps) {
return <span>Loading ...</span>;
}

const currentEmployers = data!.docs.filter((e) => e.get('isCurrent'));
const pastEmployers = data!.docs.filter((e) => !e.get('isCurrent'));
const employers: EmployerWithId[] = data!.docs.map((e) => {
const employer: Employer = e.data() as Employer;
return { id: e.id, ...employer }
});
const currentEmployers: EmployerWithId[] = employers.filter((e) => e.is_current);
const pastEmployers: EmployerWithId[] = employers.filter((e) => !e.is_current);

return (
<>
Expand All @@ -74,7 +82,7 @@ export function EmployersList({ translations }: EmployersListProps) {
<TableCell>
<div className="flex flex-row">
<Typography size="lg" weight="medium" className="grow">
{employer.get('employerName')}
{employer.employer_name}
</Typography>
<div className="flex flex-col">
<button onClick={() => onArchiveEmployer(employer.id)}>
Expand Down Expand Up @@ -106,7 +114,7 @@ export function EmployersList({ translations }: EmployersListProps) {
<TableCell>
<div className="flex flex-row">
<Typography size="lg" weight="medium" className="grow">
{employer.get('employerName')}
{employer.employer_name}
</Typography>
<div className="flex flex-col">
<button onClick={() => onDeleteEmployer(employer.id)}>
Expand Down

0 comments on commit 4d1a9fa

Please sign in to comment.