Skip to content

Commit

Permalink
Merge pull request #27 from tweag/typescript-effect
Browse files Browse the repository at this point in the history
Typescript effect: update
  • Loading branch information
aspiwack authored Nov 7, 2024
2 parents de8fd99 + a079aa6 commit a1d1e39
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions 2024-09-26-typescript-effect/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {

const CityResponse = Schema.Struct({
name: Schema.String,
country_code: Schema.String.pipe(Schema.length(2)),
country_code: pipe(Schema.String, Schema.length(2)),
latitude: Schema.Number,
longitude: Schema.Number,
});
Expand Down Expand Up @@ -61,7 +61,7 @@ const getRequest = (url: string): Effect.Effect<HttpClientResponse.HttpClientRes
Effect.provide(FetchHttpClient.layer),
);

const getCities = (search: string): Effect.Effect<Option.Option<void>, never, never> => {
const getCities = (search: string): Effect.Effect<Option.Option<void | Option.Option<void>>, never, never> => {
Option.map(citiesElement, (c) => (c.innerHTML = ""));

return pipe(
Expand Down Expand Up @@ -99,20 +99,14 @@ const getCity = (city: string): Effect.Effect<readonly CityResponse[], never, ne
Effect.scoped,
);

const renderCitySuggestions = (cities: readonly CityResponse[]): void => {
const renderCitySuggestions = (cities: readonly CityResponse[]): void | Option.Option<void> =>
// If there are multiple cities, populate the suggestions
if (cities.length > 1) {
populateSuggestions(cities);
return;
}

// We didn't get into the if statement above, so we have only one city or none
// Let's try to get the first city
// Otherwise, show a message that the city was not found
pipe(
Array.head(cities),
Option.match({
onSome: selectCity,
onNone: () => {
cities,
Array.match({
onNonEmpty: populateSuggestions,
onEmpty: () => {
const search = Option.match(cityElement, {
onSome: (cityEl) => cityEl.value,
onNone: () => "searched",
Expand All @@ -126,7 +120,7 @@ const renderCitySuggestions = (cities: readonly CityResponse[]): void => {
},
}),
);
};


const populateSuggestions = (results: readonly CityResponse[]): Option.Option<void> =>
Option.map(citiesElement, (citiesEl) =>
Expand Down

0 comments on commit a1d1e39

Please sign in to comment.