File tree Expand file tree Collapse file tree
src/app/pages/signals/signals-sample Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2020 @let isLoading = addressResource.isLoading();
2121 @let error = addressResource.error();
2222 @let hasValue = addressResource.hasValue();
23+ @let hasError = address?.erro === 'true';
2324
2425 @if (isLoading) {
2526 < po-loading-overlay />
2627 } @else {
27- @if (hasValue && address) {
28+ @if (hasValue && address && !hasError ) {
2829 < po-info p-label ="Logradouro " [p-value] ="address.logradouro " />
2930 < po-info p-label ="Bairro " [p-value] ="address.bairro " />
3031 < po-info p-label ="Cidade " [p-value] ="address.localidade " />
3334 < po-info p-label ="DDD " [p-value] ="address.ddd " />
3435 }
3536 }
36- @if (error && enabled()) {
37+ @if (( error || hasError) && enabled()) {
3738 < p class ="po-font-text-large po-mt-2 "> Ops...</ p >
3839 < p class ="po-font-text-large "> Ocorreu um erro ao buscar o Endereço :(</ p >
3940 }
Original file line number Diff line number Diff line change @@ -33,22 +33,17 @@ export class EffectComponent {
3333 readonly currentZipCode = computed ( ( ) => ( this . zipCode ( ) . length === 8 ? this . zipCode ( ) : this . lastZipCode ( ) ) ) ;
3434 readonly lastZipCode = signal < string > ( '' ) ;
3535
36- // addressResource = resource({
37- // request: this.currentZipCode,
38- // loader: ({ request: zipCode, abortSignal }): Promise<Address> => {
39- // return fetch(`${this.apiUrl}/${zipCode || '0'}/json/`, { signal: abortSignal }).then(
40- // address => address.json() as unknown as Address
41- // );
42- // // return Promise.resolve(ADDRESS_DEFAULT)
43- // }
44- // });
45-
4636 protected readonly addressResource = resource ( {
4737 params : ( ) => ( { code : this . currentZipCode ( ) } ) ,
48- loader : ( { params, abortSignal } ) =>
49- fetch ( `${ this . apiUrl } /${ params . code || '0' } /json` , { signal : abortSignal } ) . then (
38+ loader : ( { params, abortSignal } ) => {
39+ if ( ! params . code || params . code . length !== 8 ) {
40+ return Promise . resolve ( { } as Address ) ;
41+ }
42+
43+ return fetch ( `${ this . apiUrl } /${ params . code } /json` , { signal : abortSignal } ) . then (
5044 address => address . json ( ) as unknown as Address
51- )
45+ ) ;
46+ }
5247 } ) ;
5348
5449 constructor ( ) {
Original file line number Diff line number Diff line change @@ -16,4 +16,5 @@ export interface Address {
1616 gia : string ;
1717 ddd : string ;
1818 siafi : string ;
19+ erro ?: string ;
1920}
You can’t perform that action at this time.
0 commit comments