Comments (6)
Hi @sylvaingirardbe, it's always nice to see new people trying out the library.
I like your code, and there isn't much that I would to different. Here are a couple of points:
- I don't use classes but just plain objects (with an interface) for form values. While classes seem to work just fine, internally ngrx-forms has no logic to preserve the class identity of the object. Instead, it will always copy over the properties into new plain objects. That means
formState.value
is not an instance ofAsset
which may cause issues if you use e.g.instanceof
or expect the value to have some class method. However, as long as you are aware of these potential issues it's just fine to keep using classes. - I noticed that the address validation is not running on the current sub address all the time but only once it is saved as part of the asset. I am not sure if this is what you intended. In any case, you can remove a bit of redundancy by declaring some more (admittedly verbose) helper methods like this. It uses the (a bit unfortunately named)
StateUpdateFns
interface from ngrx-forms.
const assetAddressFormStateValidationUpdateFns: StateUpdateFns<AssetAddress> = {
countryCode: validate(required)
};
const validateAssetAddressFormState = updateGroup(assetAddressFormStateValidationUpdateFns);
const assetAddressFormStateValidationReducer = createFormGroupReducerWithUpdate(assetAddressFormStateValidationUpdateFns);
const assetFormStateValidationUpdateFns: StateUpdateFns<Asset> = {
location: updateGroup<AssetLocation>({
address: validateAssetAddressFormState
})
};
const validateAssetFormState = updateGroup(assetFormStateValidationUpdateFns);
const assetFormStateValidationReducer = createFormGroupReducerWithUpdate(assetFormStateValidationUpdateFns);
// and then in your reducer as you did before:
state = {
...state,
formState: assetFormStateValidationReducer(state.formState, action),
currentSubaddress: state.currentSubaddress != undefined ? assetAddressFormStateValidationReducer (state.currentSubaddress, action) : undefined
};
- Not related to ngrx-forms: In
addOrReplaceAddressInAssetIdentification
you can shorten the code quite a bit by usingsplice
instead ofslice
. This should do the same:
export const addOrReplaceAddressInAssetIdentification = (asset: Asset, address: AssetAddress): Asset => {
const addressIndex = asset.identification.addresses.findIndex(c => c.id == address.id);
const newAddresses = asset.identification.addresses.splice(addressIndex, 1, address);
return new Asset({
...asset,
identification: new AssetIdentification({
...asset.identification,
addresses: newAddresses
})
});
}
As I mentioned above, none of these points are mandatory and your code is fine without these changes. I'll keep this issue open in case you have more questions. Feel free to close it yourself once you are confident you have achieved what you want.
PS: I edited your post to include syntax highlighting which makes it easier to read.
from ngrx-forms.
Ok, thanks a bunch for the feedback. I was aware of No.1. No.2, not so much. I'll update my code a bit, same for No.3.
from ngrx-forms.
I've come across something else now. I can't seem to figure out how to disable a control. The examples about disabling controls are disabling entire control groups and not just specific controls in a group.
Doing something like this doesn't work since disable
expects a state.
const disableControls = updateGroup<Asset>({
identification: updateGroup<AssetIdentification>({
details: updateGroup({
fmisCode: disable()
})
})
});
from ngrx-forms.
Just leave away the parentheses on disable
. disable
is already a function of the correct form that can be used as an update function for the control.
from ngrx-forms.
@sylvaingirardbe did this resolve your issue? Can I close this?
from ngrx-forms.
Yep, thanks. I'll reopen when I come across something else.
from ngrx-forms.
Related Issues (20)
- Sure, just replace the players update with something like this:
- `NgrxFormControlDirective`: Update view when `ngrxValueConverter` changes HOT 1
- Pattern: Supply different "feature state" into the form reducer? HOT 1
- mat-radio checked not working? HOT 1
- Type inference is incorrect with readonly[]
- Question: Bind server-side validation to ngrx-forms forms
- [QUESTION] - Validation of a nested formState HOT 1
- Primitive unions are not supported
- Add compatibility with typescript 4.8 HOT 1
- New Angular Version 15 with breaking changes on Material to mat-list-options HOT 3
- No valid view adapter for Slider with Angular Material 15
- FormArrayState, the state doesn't update when SetValueActions are disptached
- Add compatibility with Typescript 5
- material 15+ (MCD based components) compatibility HOT 1
- Angular 16 support HOT 4
- Enable/Disable nested array properties HOT 3
- Programmatically Triggering Async Validation HOT 3
- Add sortArray function HOT 4
- Control validation order, run async after synchronous complete without error
- Question: Update parent form state based on child control states
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ngrx-forms.