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: global forms provider #250

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

feat: global forms provider #250

wants to merge 5 commits into from

Conversation

mic-web
Copy link
Contributor

@mic-web mic-web commented Dec 1, 2023

feat: global forms provider

@mic-web mic-web requested a review from spawnia December 1, 2023 12:49
@mic-web mic-web self-assigned this Dec 1, 2023
yarn.lock Outdated Show resolved Hide resolved
Comment on lines +18 to +20
/**
* Returns a combined boolean of all submitSuccessful states
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How are they combined?

Comment on lines +34 to +42
export function useGlobalForms() {
const { isSubmitting, reset, submit } = useContext(GlobalFormsContext);

return {
isSubmitting,
reset,
submit,
};
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this function necessary or useful? It seems to me like the indirection is not needed.

import React, { useCallback, useContext, useMemo } from 'react';

export type FormInfo = { name: string; order: number };
type FormInfoMixed = string | FormInfo;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type FormInfoMixed = string | FormInfo;
type FormInfoObjectOrString = FormInfo | string;

return form;
}

export function useGlobalForm(form: FormInfoMixed) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would I use an explicit order?

setSubmitSuccessful,
} = useContext(GlobalFormsContext);

const setResetCallbackWrapped = useCallback(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const setResetCallbackWrapped = useCallback(
const setResetCallbackMemoized = useCallback(

It is not just wrapped in some arbitrary way.

import { useGlobalForms, useGlobalForm } from './GlobalFormsContext';
import { GlobalFormsProvider } from './GlobalFormsProvider';

function createOptions() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Options for what? An explicit return type might help

Comment on lines +169 to +171
expect(submitFirst).toHaveBeenCalledTimes(1);
expect(submitError).toHaveBeenCalledTimes(1);
expect(submitLast).toHaveBeenCalledTimes(0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this does not prove the order was correct, I think you can swap the numbers around and still pass this test.

function findNextOrder(
callbacks: Record<string, CallbackWithOrder> | undefined,
): number {
const orders = values(callbacks).map((callback) => callback.order);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const orders = values(callbacks).map((callback) => callback.order);
const orders = Object.values(callbacks).map((callback) => callback.order);


const submit: GlobalFormsContextType['submit'] = useCallback(
async (additionalCallback?: CallbackWithOrder) => {
keys(submitCallbacks).forEach((formName) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
keys(submitCallbacks).forEach((formName) => {
Object.keys(submitCallbacks).forEach((formName) => {

keys(submitCallbacks).forEach((formName) => {
submitSuccessfulList.current[formName] = false;
});
const callbacks = values(submitCallbacks);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const callbacks = values(submitCallbacks);
const callbacks = Object.values(submitCallbacks);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and other uses

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants