Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

Commit

Permalink
Persistent Notifications with Redux
Browse files Browse the repository at this point in the history
  • Loading branch information
viveknigam3003 committed Jul 13, 2020
1 parent 8dc1632 commit 6bce421
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
15 changes: 4 additions & 11 deletions src/components/Notifications/NotificationList.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import List from "@material-ui/core/List";
import Grid from "@material-ui/core/Grid";
//import demoMessages from "./DemoMessages";
import MessageItem from "./MessageItem";
import ReadAll from "./ReadAll";
import IconButton from "@material-ui/core/IconButton";
Expand Down Expand Up @@ -36,15 +35,9 @@ export default function NotificationList(props) {
const dispatch = useDispatch();
const notifications = useSelector((state) => state);

function addNotification() {
function clearAllNotifications() {
dispatch({
type: "ADD",
id: 0,
primary: "Welcome to Rucio!",
secondary: "This is the notifications panel",
server: "rucio-dev-server",
nType: "message",
read: false,
type: "DELETE_ALL",
});
}

Expand All @@ -60,7 +53,7 @@ export default function NotificationList(props) {
id="title"
>
<span style={spanStyle}>All Notifications</span>
<IconButton onClick={addNotification}>
<IconButton onClick={clearAllNotifications}>
<ClearAll fontSize="small" />
</IconButton>
</div>
Expand All @@ -76,7 +69,7 @@ export default function NotificationList(props) {
type={item.type}
status={item.status}
read={item.read}
onClick={(e) => dispatch({type: 'DELETE', index: i})}
onClick={() => dispatch({ type: "DELETE", index: i })}
/>
))
) : (
Expand Down
8 changes: 6 additions & 2 deletions src/components/Notifications/Reducers/NotificationReducer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const notification = [];
const notification = JSON.parse(localStorage.getItem('notifications'))

function notificationReducer(state = notification, action) {
switch (action.type) {
Expand All @@ -14,17 +14,21 @@ function notificationReducer(state = notification, action) {
read: false,
});
console.log(newNotification);
localStorage.setItem('notifications', JSON.stringify(newNotification))
return newNotification;

case "DELETE":
const newNotificationState = [...state];
newNotificationState[action.index].read = true;
newNotificationState.splice(action.id, 1);
console.log(newNotificationState);
localStorage.setItem('notifications', JSON.stringify(newNotificationState))
return newNotificationState;

case "DELETE_ALL":
return [];
localStorage.setItem("notifications", JSON.stringify([]));
const emptyNotificationState = []
return emptyNotificationState

default:
return state;
Expand Down

0 comments on commit 6bce421

Please sign in to comment.