revert prev. commit and extract message state to a new file
This commit is contained in:
parent
266a80b071
commit
7d69e88bbf
|
@ -0,0 +1,30 @@
|
||||||
|
import React from 'react'
|
||||||
|
import { Message } from './SubscriptionsHook'
|
||||||
|
|
||||||
|
export type MessageStateType = {
|
||||||
|
set: React.Dispatch<React.SetStateAction<Message[]>>
|
||||||
|
get: Message[]
|
||||||
|
add(message: Message): void
|
||||||
|
removeKey(key: string): void
|
||||||
|
}
|
||||||
|
|
||||||
|
export const MessageState: MessageStateType = {
|
||||||
|
set: fn => {
|
||||||
|
console.warn('set function is not defined yet, called with', fn)
|
||||||
|
},
|
||||||
|
get: [],
|
||||||
|
add: message => {
|
||||||
|
MessageState.set(messages => {
|
||||||
|
const newMessages = messages.filter(msg => msg.key !== message.key)
|
||||||
|
newMessages.push(message)
|
||||||
|
|
||||||
|
return newMessages
|
||||||
|
})
|
||||||
|
},
|
||||||
|
removeKey: key => {
|
||||||
|
MessageState.set(messages => {
|
||||||
|
const newMessages = messages.filter(msg => msg.key !== key)
|
||||||
|
return newMessages
|
||||||
|
})
|
||||||
|
},
|
||||||
|
}
|
|
@ -1,11 +1,11 @@
|
||||||
import React from 'react'
|
|
||||||
import { render, screen, fireEvent } from '@testing-library/react'
|
import { render, screen, fireEvent } from '@testing-library/react'
|
||||||
|
import React from 'react'
|
||||||
import { NotificationType } from '../../__generated__/globalTypes'
|
import { NotificationType } from '../../__generated__/globalTypes'
|
||||||
import { MessageState } from './Messages'
|
import { MessageState } from './MessageState'
|
||||||
import MessagePlain from './Message'
|
import MessagePlain from './Message'
|
||||||
import MessageProgress from './MessageProgress'
|
import MessageProgress from './MessageProgress'
|
||||||
|
|
||||||
// Define the mock for SubscriptionsHook before importing it
|
// Define the mock for SubscriptionsHook before using it
|
||||||
const MockSubscriptionsHook = ({ messages, setMessages }: any) => {
|
const MockSubscriptionsHook = ({ messages, setMessages }: any) => {
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
|
|
|
@ -5,6 +5,7 @@ import MessageProgress from './MessageProgress'
|
||||||
import MessagePlain from './Message'
|
import MessagePlain from './Message'
|
||||||
import SubscriptionsHook, { Message } from './SubscriptionsHook'
|
import SubscriptionsHook, { Message } from './SubscriptionsHook'
|
||||||
import { NotificationType } from '../../__generated__/globalTypes'
|
import { NotificationType } from '../../__generated__/globalTypes'
|
||||||
|
import { MessageState } from './MessageState'
|
||||||
|
|
||||||
const Container = styled.div`
|
const Container = styled.div`
|
||||||
position: fixed;
|
position: fixed;
|
||||||
|
@ -19,34 +20,6 @@ const Container = styled.div`
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
type MessageStateType = {
|
|
||||||
set: React.Dispatch<React.SetStateAction<Message[]>>
|
|
||||||
get: Message[]
|
|
||||||
add(message: Message): void
|
|
||||||
removeKey(key: string): void
|
|
||||||
}
|
|
||||||
|
|
||||||
export const MessageState: MessageStateType = {
|
|
||||||
set: fn => {
|
|
||||||
console.warn('set function is not defined yet, called with', fn)
|
|
||||||
},
|
|
||||||
get: [],
|
|
||||||
add: message => {
|
|
||||||
MessageState.set(messages => {
|
|
||||||
const newMessages = messages.filter(msg => msg.key != message.key)
|
|
||||||
newMessages.push(message)
|
|
||||||
|
|
||||||
return newMessages
|
|
||||||
})
|
|
||||||
},
|
|
||||||
removeKey: key => {
|
|
||||||
MessageState.set(messages => {
|
|
||||||
const newMessages = messages.filter(msg => msg.key != key)
|
|
||||||
return newMessages
|
|
||||||
})
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
const Messages = () => {
|
const Messages = () => {
|
||||||
const [messages, setMessages] = useState<Message[]>([])
|
const [messages, setMessages] = useState<Message[]>([])
|
||||||
MessageState.set = setMessages
|
MessageState.set = setMessages
|
||||||
|
|
Loading…
Reference in New Issue