Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
FMS_Project_Frontend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
bashar.hussein
FMS_Project_Frontend
Commits
10df46a0
Commit
10df46a0
authored
Aug 09, 2023
by
ReemyHasan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
SSE Fixing
parent
b5ec0666
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
108 additions
and
50 deletions
+108
-50
index.tsx
packages/admin/src/components/layout/index.tsx
+57
-2
index.tsx
packages/admin/src/features/show-traps/index.tsx
+51
-48
No files found.
packages/admin/src/components/layout/index.tsx
View file @
10df46a0
import
React
,
{
ReactNode
,
useState
}
from
"react"
;
import
React
,
{
ReactNode
,
useState
,
useEffect
,
useContext
}
from
"react"
;
import
{
Layout
}
from
"antd"
;
import
{
Layout
}
from
"antd"
;
import
AppContentView
from
"./components/content-view"
;
import
AppContentView
from
"./components/content-view"
;
import
AppSidebar
from
"./components/sidebar"
;
import
AppSidebar
from
"./components/sidebar"
;
import
AppHeader
from
"./components/header"
;
import
AppHeader
from
"./components/header"
;
import
{
useCookies
}
from
"react-cookie"
;
import
DataContext
from
"../../context/trap-context"
;
import
{
fetchData
}
from
"../../services/traps-service"
;
type
AppLayoutProps
=
{
type
AppLayoutProps
=
{
children
:
ReactNode
;
children
:
ReactNode
;
};
};
...
@@ -12,7 +14,60 @@ const AppLayout = ({ children }: AppLayoutProps) => {
...
@@ -12,7 +14,60 @@ const AppLayout = ({ children }: AppLayoutProps) => {
const
toggleCollapse
=
()
=>
{
const
toggleCollapse
=
()
=>
{
setIsCollapsed
(
!
isCollapsed
);
setIsCollapsed
(
!
isCollapsed
);
};
};
const
{
data
,
setData
}
=
useContext
(
DataContext
);
const
[
cookies
,
setCookie
]
=
useCookies
([]);
useEffect
(()
=>
{
if
(
!
cookies
[
"fetch"
]
)
{
const
fetchData1
=
async
()
=>
{
try
{
const
response
=
await
fetchData
(
cookies
[
"token"
]);
setData
(
response
);
setCookie
(
"fetch"
,
1
);
}
catch
(
error
)
{
console
.
error
(
"Error fetching data:"
,
error
);
}
};
fetchData1
();
// console.log(data);
}
// console.log(data);
const
authToken
=
`Bearer
${
cookies
[
"token"
]}
`
;
const
source
=
new
EventSource
(
`http://localhost:6647/api/notifications/sub?token=
${
authToken
}
`
);
source
.
addEventListener
(
"open"
,
()
=>
{
console
.
log
(
"SSE opened!"
);
});
source
.
addEventListener
(
"message"
,
(
e
)
=>
{
const
message
=
JSON
.
parse
(
e
.
data
);
console
.
log
(
message
);
if
(
message
.
new_val
!=
null
)
{
const
newData
=
message
.
new_val
;
// setData1((prevData: any) => [...prevData, newData]);
setData
((
prevData
:
any
)
=>
[...
prevData
,
newData
]);
}
else
{
setData
((
prevData
:
any
)
=>
prevData
.
filter
((
item
:
any
)
=>
item
.
id
!==
message
.
old_val
.
id
)
);
// setData1((prevData: any) =>
// prevData.filter((item: any) => item.id !== message.old_val.id)
// );
}
});
source
.
addEventListener
(
"error"
,
(
e
)
=>
{
console
.
error
(
"Error: "
,
e
);
});
return
()
=>
{
source
.
close
();
console
.
log
(
"SSE closed "
);
};
},
[]);
return
(
return
(
<
div
className=
{
"app-layout"
}
>
<
div
className=
{
"app-layout"
}
>
...
...
packages/admin/src/features/show-traps/index.tsx
View file @
10df46a0
...
@@ -19,60 +19,63 @@ export default function ShowTraps() {
...
@@ -19,60 +19,63 @@ export default function ShowTraps() {
const
[
data1
,
setData1
]
=
useState
(
data
);
const
[
data1
,
setData1
]
=
useState
(
data
);
const
[
loading
,
setLoading
]
=
useState
(
false
);
const
[
loading
,
setLoading
]
=
useState
(
false
);
const
[
cookies
,
setCookie
]
=
useCookies
([]);
const
[
cookies
,
setCookie
]
=
useCookies
([]);
useEffect
(()
=>
{
useEffect
(()
=>
{
if
(
!
cookies
[
"fetch"
]
)
{
const
fetchData1
=
async
()
=>
{
try
{
const
response
=
await
fetchData
(
cookies
[
"token"
]);
setData
(
response
);
setData1
(
response
);
setCookie
(
"fetch"
,
1
);
}
catch
(
error
)
{
console
.
error
(
"Error fetching data:"
,
error
);
}
};
fetchData1
();
console
.
log
(
data
);
}
console
.
log
(
data
);
setData1
(
data
);
setData1
(
data
);
},
[
data
])
// useEffect(() => {
// if (!cookies["fetch"] ) {
// const fetchData1 = async () => {
// try {
// const response = await fetchData(cookies["token"]);
// setData(response);
// setData1(response);
// setCookie("fetch", 1);
// } catch (error) {
// console.error("Error fetching data:", error);
// }
// };
const
authToken
=
`Bearer
${
cookies
[
"token"
]}
`
;
// fetchData1();
const
source
=
new
EventSource
(
// console.log(data);
`http://localhost:6647/api/notifications/sub?token=
${
authToken
}
`
// }
);
// console.log(data);
// setData1(data);
source
.
addEventListener
(
"open"
,
()
=>
{
// const authToken = `Bearer ${cookies["token"]}`;
console
.
log
(
"SSE opened!"
);
// const source = new EventSource(
});
// `http://localhost:6647/api/notifications/sub?token=${authToken}`
// );
source
.
addEventListener
(
"message"
,
(
e
)
=>
{
// source.addEventListener("open", () => {
const
message
=
JSON
.
parse
(
e
.
data
);
// console.log("SSE opened!");
console
.
log
(
message
);
// });
if
(
message
.
new_val
!=
null
)
{
const
newData
=
message
.
new_val
;
setData1
((
prevData
:
any
)
=>
[...
prevData
,
newData
]);
setData
((
prevData
:
any
)
=>
[...
prevData
,
newData
]);
}
else
{
setData
((
prevData
:
any
)
=>
prevData
.
filter
((
item
:
any
)
=>
item
.
id
!==
message
.
old_val
.
id
)
);
setData1
((
prevData
:
any
)
=>
prevData
.
filter
((
item
:
any
)
=>
item
.
id
!==
message
.
old_val
.
id
)
);
}
});
source
.
addEventListener
(
"error"
,
(
e
)
=>
{
// source.addEventListener("message", (e) => {
console
.
error
(
"Error: "
,
e
);
// const message = JSON.parse(e.data);
});
// console.log(message);
// if (message.new_val != null) {
// const newData = message.new_val;
// setData1((prevData: any) => [...prevData, newData]);
// setData((prevData: any) => [...prevData, newData]);
// } else {
// setData((prevData: any) =>
// prevData.filter((item: any) => item.id !== message.old_val.id)
// );
// setData1((prevData: any) =>
// prevData.filter((item: any) => item.id !== message.old_val.id)
// );
// }
// });
// source.addEventListener("error", (e) => {
// console.error("Error: ", e);
// });
return
()
=>
{
//
return () => {
source
.
close
();
//
source.close();
console
.
log
(
"SSE closed "
);
//
console.log("SSE closed ");
};
//
};
},
[]);
//
}, []);
return
(
return
(
<
div
>
<
div
>
<
Spin
spinning=
{
loading
}
>
<
Spin
spinning=
{
loading
}
>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment