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

Add persist form functionality #24

Closed
NetanelBasal opened this issue Jul 6, 2020 · 2 comments · Fixed by #33
Closed

Add persist form functionality #24

NetanelBasal opened this issue Jul 6, 2020 · 2 comments · Fixed by #33
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@NetanelBasal
Copy link
Member

It'll be great to add a built-in method to persist the form value to the storage. For example:

const profileForm = new FormGroup<Profile>({
  firstName: new FormControl(''),
  lastName: new FormControl(''),
  skills: new FormArray([]),
  address: new FormGroup({
    street: new FormControl(''),
    city: new FormControl('')
  })
});

profileForm.persist(key, storage);

Where the key is the storage key, and the storage is local or session.

@NetanelBasal NetanelBasal changed the title Add persist form option Add persist form functionality Jul 6, 2020
@NetanelBasal NetanelBasal added enhancement New feature or request good first issue Good for newcomers labels Jul 6, 2020
@caroso1222
Copy link
Contributor

I wonder if you'd like to follow a more functional approach for the API on this one. Something along the lines of

import { LocalStorageManager } from '@ngneat/reactive-forms';

const profileForm = new FormGroup<Profile>({
  firstName: new FormControl(''),
  lastName: new FormControl(''),
});

LocalStorageManager.serialize(key, profileForm)
LocalStorageManager.deserialize(key)

It would allow the feature to scale with more managers like SessionStorageManager, IndexedDBManager, all of them implementing a common IPersistManager interface.

Not sure about this claim, but I suppose this could be easier to tree shake rather than having a persist method with a switch-case statement for local, session, etc.

I don't have any problem with .persist, though. Just picking brains on this one.

@NetanelBasal
Copy link
Member Author

We can combine the ideas and create something like:

profileForm.persist(key: string, storage: IPersistManager)

I think we still need to provide at least the default behavior and save it to localstorage if storage isn't provided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants