Skip to content

Commit

Permalink
tap
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonmanRolls committed Oct 27, 2024
1 parent 4d29b33 commit 554e4c7
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/pages/legacyfavourites.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable max-classes-per-file */
import { Context, Effect, pipe } from 'effect'
import { Effect, pipe } from 'effect'
import { Dispatch, ReactElement, SetStateAction, useEffect, useState } from 'react'
import styled, { css } from 'styled-components'
import { useChainId } from 'wagmi'
Expand All @@ -13,7 +13,7 @@ import { Outlink } from '@app/components/Outlink'
import { Content } from '@app/layouts/Content'
import { ContentGrid } from '@app/layouts/ContentGrid'

const { succeed, flatMap, map, match, runSync, sync, tap } = Effect
const { try: EffectTry, flatMap, map, match, runSync, sync } = Effect

const Container = styled.div(
({ theme }) => css`
Expand Down Expand Up @@ -63,6 +63,9 @@ type SimpleFavorite = { name: string; expiry: Date }

class JsonParseError extends SyntaxError {}

const invalidDateCheck = (favorite: SimpleFavorite) =>
favorite?.expiry?.toString() === 'Invalid Date' ? console.log('Invalid date') : null

export const getLegacyFavorites = (): string =>
globalThis?.localStorage?.getItem('ensFavourites') || '{}'

Expand All @@ -77,7 +80,7 @@ export const simplifyLegacyFavorites = (legacyFavorites: any): SimpleFavorite[]
}

const jsonParseEffect = (input: string): Effect.Effect<LegacyFavorite[], JsonParseError> =>
Effect.try({
EffectTry({
try: () => JSON.parse(input),
catch: (error) => new JsonParseError(error as string),
})
Expand All @@ -87,6 +90,8 @@ const setFavoritesProgram = (setState: Dispatch<SetStateAction<SimpleFavorite[]
sync(getLegacyFavorites),
flatMap(jsonParseEffect),
map(simplifyLegacyFavorites),
// Easy to 'interrupt' the computation at any point and do stuff
Effect.tap((simpleLegacyFavorites) => simpleLegacyFavorites.map(invalidDateCheck)),
match({
onFailure: console.error,
onSuccess: setState,
Expand Down

0 comments on commit 554e4c7

Please sign in to comment.