Skip to content

Commit 2f06da6

Browse files
author
Danilo Otavio Lima Salve
committed
fix(signals-sample): bloqueia requsição quando o CEP é vazio ou inválido
1 parent d842451 commit 2f06da6

3 files changed

Lines changed: 12 additions & 15 deletions

File tree

src/app/pages/signals/signals-sample/effect/effect.component.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
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" />
@@ -33,7 +34,7 @@
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
}

src/app/pages/signals/signals-sample/effect/effect.component.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff 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() {

src/app/pages/signals/signals-sample/shared/interface/address.interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ export interface Address {
1616
gia: string;
1717
ddd: string;
1818
siafi: string;
19+
erro?: string;
1920
}

0 commit comments

Comments
 (0)