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

Elegant approach for "Save as Active" #75

Open
stephenh opened this issue Feb 15, 2023 · 0 comments
Open

Elegant approach for "Save as Active" #75

stephenh opened this issue Feb 15, 2023 · 0 comments

Comments

@stephenh
Copy link
Contributor

stephenh commented Feb 15, 2023

Copy/pasted from https://github.com/homebound-team/internal-frontend/pull/3424#discussion_r1107253955

I couldn't think of a great way to handle this with like "first-class" support in Beam...

I believe the concept's we'd want would be:

  1. Validation rules that are conditional on rpc.status (which form-state does support today, and where I'd nudged Dean and Craig at first)
  2. "Dual valid-ness", to drive "only enable Save As Active if the conditional-on-rpc.status rules all pass", essentially separate form.validIfDraft / form.validIfActive flags
  3. The "conditional on rpc.status=active" error messages should only ever show up if the user comes in to an already-active RPC and starts deleting fields.

The 2nd one, which is basically the useComputed they're writing by hand, is the tough one--b/c we don't want to actually set rpc.status=active, b/c that would show a bunch of errors in the UI like (First Name is required).

But we want to ask if we happened to set rpc.status=active, then would all of the rules be true?

...I dunno, like maybe something like:

const canBeActive = useComputed(() => {
  const originalStatus = rpc.status
  // quickly switch to active
  rpc.status.value = active
  // run all the form rules
  const canBeActive = rpc.valid
  // okay, put it back
  rpc.status.value = originalStatus
  // and return our "probed" validness
  return canBeActive
});

That actually doesn't seem that bad, and maybe we could write a hook like:

const canBeActive = useMaybeValid(formState, formState.status, active);

That would internalize the "try flipping to active, record valid, put it back" dance.

But I'm not actually sure if the "flip to active, flip it back" would work in mobx...like, I'm 80% sure it "would technically work", but would be nuances around making sure the changes happened synchronously and ideally were not observed by anyone else.

...actually, I saw the phrase "transaction" in the mobx docs last night, so maybe that would be an approach...start a transaction, change the status, record the canBeActive = rpc.valid, and then rollback the transaction. Never used the mobx transactions before so not sure/don't know the API.

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

No branches or pull requests

1 participant