-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add dynamic component for events
- Loading branch information
Showing
9 changed files
with
171 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Event, EventType, Payload } from "types/events"; | ||
|
||
import { Card } from "react-daisyui"; | ||
|
||
import { motion } from "framer-motion"; | ||
const variants = { | ||
visible: { opacity: 1, transition: { duration: 1 } }, | ||
hidden: { opacity: 0, transition: { duration: 1 } }, | ||
}; | ||
|
||
const AllocationCreatedEvent = (event: { | ||
kind: Event.ALLOCATION_CREATED; | ||
payload: Payload[Event.ALLOCATION_CREATED]; | ||
}) => { | ||
return ( | ||
<Card bordered={true}> | ||
<Card.Body> | ||
<Card.Title>Allocation Created</Card.Title> | ||
<div> | ||
<div>Allocation ID: {event.payload.allocationId}</div> | ||
<div>Amount: {event.payload.amount}</div> | ||
<div>Recipient: {event.payload.validityTimestamp}</div> | ||
</div> | ||
</Card.Body> | ||
</Card> | ||
); | ||
}; | ||
|
||
const AllocationReleasedEvent = (event: { | ||
kind: Event.ALLOCATION_RELEASED; | ||
payload: Payload[Event.ALLOCATION_RELEASED]; | ||
}) => { | ||
return ( | ||
<Card bordered={true}> | ||
<Card.Body> | ||
<Card.Title>Allocation Released</Card.Title> | ||
<div> | ||
<div>Allocation ID: {event.payload.allocationId}</div> | ||
</div> | ||
</Card.Body> | ||
</Card> | ||
); | ||
}; | ||
|
||
export const EventCard = (event: EventType) => { | ||
return ( | ||
<motion.div variants={variants} initial="hidden" animate="visible"> | ||
{(() => { | ||
switch (event.kind) { | ||
case Event.ALLOCATION_CREATED: | ||
return <AllocationCreatedEvent {...event} />; | ||
case Event.ALLOCATION_RELEASED: | ||
return <AllocationReleasedEvent {...event} />; | ||
} | ||
})()} | ||
</motion.div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,42 @@ | ||
import { useState } from "react"; | ||
import { useEffect, useState } from "react"; | ||
import { EventType } from "types/events"; | ||
import { useAllocationEvents } from "hooks/events/useAllocationEvents"; | ||
import { EventCard } from "./event"; | ||
import { uniqBy } from "ramda"; | ||
import { use } from "i18next"; | ||
|
||
export const Events = () => { | ||
const [events, setEvents] = useState<EventType[]>([]); | ||
const [events, setEvents] = useState< | ||
(EventType & { | ||
id: number; | ||
timestamp: number; | ||
})[] | ||
>([]); | ||
const { events$: allocationEvents$ } = useAllocationEvents(); | ||
|
||
allocationEvents$.subscribe((event) => { | ||
setEvents((prevEvents) => [...prevEvents, event]); | ||
}); | ||
useEffect(() => { | ||
console.log("subscribing"); | ||
const sub = allocationEvents$.subscribe((event) => { | ||
console.log("new event", event); | ||
setEvents((prevEvents) => { | ||
return uniqBy( | ||
(e) => { | ||
return `${e.kind}-${e.id}`; | ||
}, | ||
[...prevEvents, event] | ||
); | ||
}); | ||
}); | ||
return () => { | ||
console.log("unsubscribing"); | ||
}; | ||
}, [allocationEvents$]); | ||
|
||
return ( | ||
<> | ||
{events.map((event, index) => { | ||
return <EventCard key={index} {...event} />; | ||
})} | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters