Skip to content

Commit

Permalink
refactor(core-data-access): Refactoring updates to data access library
Browse files Browse the repository at this point in the history
Updates to folder structure of state management files

Moved form hooks
  • Loading branch information
sullivanpj committed Jun 16, 2023
1 parent b04d437 commit faacb9c
Show file tree
Hide file tree
Showing 69 changed files with 996 additions and 291 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DateTime } from "@open-system/core-utilities";
import { useAtomValue, useSetAtom } from "jotai";
import { useCallback } from "react";
import { Contact, INITIAL_CONTACT, contactAtom } from "../models/contact";
import { Contact, INITIAL_CONTACT, contactAtom } from "../state/contact";

export const useContactValue = (): Contact | undefined => {
const contact = useAtomValue(contactAtom);
Expand Down
4 changes: 2 additions & 2 deletions libs/contact/typescript/data-access/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from "./hooks";
export * from "./models";
export * from "./types";
export * from "./state";
export * from "./types";
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export interface Contact extends Partial<Address> {

export const INITIAL_CONTACT: Partial<Contact> = {
isSubscribed: true,
} ;
};

export const contactAtom = atomWithWebStorage<Partial<Contact>>(
"contact-draft",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

import { Contact } from "@open-system/contact-data-access";
import { PhoneNumberText } from "@open-system/core-components";
import { useFormValues } from "@open-system/core-feature-form";
import { DateTime } from "@open-system/core-utilities";
import { Accordion, FieldText } from "@open-system/design-system-components";
import { AddressText } from "@open-system/shared-feature-address";
import Envelope from "../../../../../../assets/images/envelope.svg";
import { BaseContactForm, BaseContactFormProps } from "../base-contact-form";
import { ContactFormStepReview } from "../contact-form-step-review";
import { ContactFormSegments } from "../types";
import {
useFormValues
} from "@open-system/core-data-access";

export type BaseContactReviewFormProps = Omit<
BaseContactFormProps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Link } from "@open-system/core-components";
import {
EmailInput,
PhoneNumberInput,
useFormValues,
} from "@open-system/core-feature-form";
import {
Accordion,
Expand All @@ -17,6 +16,9 @@ import {
} from "@open-system/shared-feature-address";
import { BaseContactForm } from "../base-contact-form";
import { SubscriptionCheckbox } from "../subscription-checkbox";
import {
useFormValues
} from "@open-system/core-data-access";

export function ContactBusinessPersonalInfoForm({
className,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import { Contact } from "@open-system/contact-data-access";
import { Link, PhoneNumberText } from "@open-system/core-components";
import { useFormValues } from "@open-system/core-feature-form";
import { formatBoolean } from "@open-system/core-utilities";
import {
BaseComponentProps,
Expand All @@ -13,6 +12,9 @@ import { AddressText } from "@open-system/shared-feature-address";
import { BaseContactReviewForm } from "../base-contact-review-form";
import { ContactFormStepReview } from "../contact-form-step-review";
import { ContactFormSegments } from "../types";
import {
useFormValues
} from "@open-system/core-data-access";

export function ContactBusinessReviewForm({
className,
Expand Down
17 changes: 12 additions & 5 deletions libs/core/typescript/data-access/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
export * from "./useIsDarkMode";
export * from "./useToastMessages";
export * from "./useTheme";
export * from "./useAtomList";
export * from "./useNotifications";
export * from "./useCurrentUserId";
export * from "./useFileUploadList";
export * from "./useFormId";
export * from "./useFormId";
export * from "./useIsDarkMode";
export * from "./useNotifications";
export * from "./useTheme";
export * from "./useToastMessages";
export * from "./useFieldValue";
export * from "./useFormValues";
export * from "./useIsSubmitting";
export * from "./useIsValid";
export * from "./useFieldErrors";
export * from "./useFieldRegistration";
17 changes: 10 additions & 7 deletions libs/core/typescript/data-access/src/hooks/useAtomList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,28 @@ export const useAtomList = <
): UseAtomListReturn<TValue> => {
const setAtomList = useSetAtom(atomList);

const add = useCallback((item: Omit<TValue, "id"> & Partial<Pick<TValue, "id">>) => {
setAtomList({ type: "add", item });
}, []);
const add = useCallback(
(item: Omit<TValue, "id"> & Partial<Pick<TValue, "id">>) => {
setAtomList({ type: "add", item });
},
[setAtomList]
);

const remove = useCallback((id: string) => {
setAtomList({ type: "remove", id });
}, []);
}, [setAtomList]);

const reset = useCallback((initialValue?: TValue[]) => {
setAtomList({ type: "reset", initialValue });
}, []);
}, [setAtomList]);

const process = useCallback((funct: (prev: TValue[]) => TValue[]) => {
setAtomList({ type: "process", funct });
}, []);
}, [setAtomList]);

const map = useCallback((funct: (prev: TValue) => TValue) => {
setAtomList({ type: "map", funct });
}, []);
}, [setAtomList]);

return { add, remove, reset, process, map };
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useAtomValue } from "jotai";
import { currentUserIdAtom } from "../state/current-user-id";

export const useCurrentUserId = (): string => {
return useAtomValue(currentUserIdAtom);
};
25 changes: 25 additions & 0 deletions libs/core/typescript/data-access/src/hooks/useFieldRegistration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useSetAtom } from "jotai";
import { useCallback } from "react";
import { useFormContext } from "react-hook-form";
import { useFormId } from "../hooks/useFormId";
import { formFieldConfigFamily } from "../state/form";
import { FormFieldConfig } from "../types";

export const useFieldRegistration = (field: string) => {
const { register } = useFormContext();

const formId = useFormId();
const setFormFieldConfig = useSetAtom(
formFieldConfigFamily({ field, formId })
);

return useCallback(
(config: Partial<FormFieldConfig>) => {
const props = register(field, config as any);
setFormFieldConfig(config);

return { ...props, name: field, config };
},
[field, register, setFormFieldConfig]
);
};
52 changes: 42 additions & 10 deletions libs/core/typescript/data-access/src/hooks/useFileUploadList.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import { useAtomValue, useSetAtom } from "jotai";
import { Atom, useAtomValue, useSetAtom } from "jotai";
import { useCallback } from "react";
import { FileUploadState, fileUploadFamily } from "../models/form";
import { FileUploadState, fileUploadFamily } from "../state/form";
import { useFormId } from "./useFormId";

export type UseFileUploadListReturn = {
include: (files: Array<File>) => void;
exclude: (fileId: string) => void;
exclude: (name: string) => void;
reset: () => void;
};

export const useFileUploadListValue = (params: {
field: string;
formId?: string;
}): FileUploadState[] => {
}): Atom<Promise<FileUploadState>>[] => {
const formId = useFormId();
return useAtomValue(
fileUploadFamily({ field: params.field, formId: params.formId ?? formId })
useAtomValue(
fileUploadFamily({
field: params.field,
formId: params.formId ?? formId,
})
)
);
};

Expand All @@ -29,15 +34,15 @@ export const useSetFileUploadList = (params: {
fileUploadFamily({ field: params.field, formId: params.formId ?? formId })
);
const include = useCallback(
async (files: Array<File>) => {
await setFileUploadList({ type: "include", files });
(files: Array<File>) => {
setFileUploadList({ type: "include", files });
},
[setFileUploadList]
);

const exclude = useCallback(
(fileId: string) => {
setFileUploadList({ type: "exclude", fileId });
(name: string) => {
setFileUploadList({ type: "exclude", name });
},
[setFileUploadList]
);
Expand All @@ -52,6 +57,33 @@ export const useSetFileUploadList = (params: {
export const useFileUploadList = (params: {
field: string;
formId?: string;
}): [FileUploadState[], UseFileUploadListReturn] => {
}): [Atom<Promise<FileUploadState>>[], UseFileUploadListReturn] => {
return [useFileUploadListValue(params), useSetFileUploadList(params)];
};

export const useFileUploadErrors = (params: {
field: string;
formId?: string;
}) => {
/*const formId = useFormId();
const { setError, clearErrors, trigger } = useFormContext();
const error = useFieldErrors(params.field);
const nextError = useAtomValue(
useMemo(() => fileUploadErrorMessageFamily({
field: params.field,
formId: params.formId ?? formId,
}), [params, formId])
);
useEffect(() => {
if (nextError !== error?.[params.field]) {
if (nextError) {
setError(params.field, { type: "file", message: nextError });
} else if (!isEmpty(error?.[params.field])) {
clearErrors(params.field);
}
trigger();
}
}, [error, nextError]);*/
};
2 changes: 1 addition & 1 deletion libs/core/typescript/data-access/src/hooks/useFormId.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useAtomValue } from "jotai";
import { useMolecule } from "jotai-molecules";
import { formIdMolecule } from "../models/form";
import { formIdMolecule } from "../state/form";

export const useFormId = () => {
const formIdAtom = useMolecule(formIdMolecule);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useAtom, useAtomValue, useSetAtom } from "jotai/react";
import { isDarkModeAtom } from "../models/theme";
import { isDarkModeAtom } from "../state/theme";

export const useIsDarkMode = () => useAtom(isDarkModeAtom);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useAtomValue } from "jotai/react";
import {
NotificationMolecule,
notificationsAtom,
} from "../models/notifications";
} from "../state/notifications";

export const useNotifications = (): Record<
string,
Expand Down
2 changes: 1 addition & 1 deletion libs/core/typescript/data-access/src/hooks/useTheme.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useAtom, useAtomValue, useSetAtom } from "jotai/react";
import { themeAtom } from "../models/theme";
import { themeAtom } from "../state/theme";

export const useTheme = () => useAtom(themeAtom);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useAtomValue } from "jotai";
import { useCallback } from "react";
import { ToastMessage, toastMessagesAtom } from "../models/toast-messages";
import { ToastMessage, toastMessagesAtom } from "../state/toast-messages";
import { UseAtomListReturn, useAtomList } from "./useAtomList";
import { ToastVariants } from "@open-system/design-system-components";

Expand Down
4 changes: 2 additions & 2 deletions libs/core/typescript/data-access/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from "./formatting";
export * from "./hooks";
export * from "./models";
export * from "./state";
export * from "./types";
export * from "./utilities";
export * from "./formatting";
Loading

0 comments on commit faacb9c

Please sign in to comment.