-
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.
custome push notification with utiles.js
- Loading branch information
Showing
8 changed files
with
164 additions
and
41 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { useSelector } from "react-redux"; | ||
import { NotifyType } from "../app/types"; | ||
import reducer from "../redux/reducer"; | ||
|
||
export default function useNotify(): NotifyType { | ||
return useSelector((state: ReturnType<typeof reducer>) => state.notify); | ||
} |
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,24 +1,33 @@ | ||
import { useState } from "react"; | ||
import { useDispatch } from "react-redux"; | ||
import { pushSubscribe } from "../app/utiles"; | ||
import useNotify from "../hooks/useNotify"; | ||
import { notifyTokenUpdate } from "../redux/notifyStore"; | ||
|
||
export default function Tokenization() { | ||
const updateTokenization = () => { | ||
const currentToken = localStorage.getItem('notify_token'); | ||
localStorage.setItem('prevous_token', currentToken || '') | ||
localStorage.removeItem('notify_token') | ||
location.reload() | ||
|
||
const [loading, setLoading] = useState(false) | ||
|
||
const notify = useNotify(); | ||
const dispatch = useDispatch(); | ||
|
||
function updateToken() { | ||
setLoading(true) | ||
pushSubscribe(function (token) { | ||
if (token) dispatch(notifyTokenUpdate(token)); | ||
}).finally(() => setLoading(false)); | ||
} | ||
|
||
return ( | ||
<div className="w-full mx-10 my-20"> | ||
<div className="mx-10 my-20"> | ||
<div className="my-10"> | ||
<span className=" font-bold mr-5">Current Token:</span> | ||
<span className=" ">{localStorage.getItem('notify_token')}</span> | ||
<div className="font-bold mb-6 text-center">Token</div> | ||
<span className=" break-words">{notify.token}</span> | ||
</div> | ||
<div className="flex justify-center"> | ||
<button onClick={updateTokenization} type="button" className="text-white bg-gradient-to-r from-blue-500 via-blue-600 to-blue-700 hover:bg-gradient-to-br focus:ring-4 focus:outline-none focus:ring-blue-300 dark:focus:ring-blue-800 font-medium rounded-lg text-sm px-5 py-2.5 text-center me-2 mb-2">Clear Token and replace new</button> | ||
</div> | ||
<div className="my-10"> | ||
<span className=" font-bold mr-5">Prevous Token:</span> | ||
<span className=" ">{localStorage.getItem('prevous_token')}</span> | ||
<button disabled={loading} onClick={updateToken} type="button" className="text-white bg-gradient-to-r from-blue-500 via-blue-600 to-blue-700 hover:bg-gradient-to-br focus:ring-4 focus:outline-none focus:ring-blue-300 dark:focus:ring-blue-800 font-medium rounded-lg text-sm px-5 py-2.5 text-center me-2 mb-2">Generate Token</button> | ||
</div> | ||
<div className="flex justify-center my-2">{loading && "Loading..."}</div> | ||
</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
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,48 @@ | ||
import { Action, NotifyType } from "../app/types"; | ||
|
||
export const NOTIFY_TOKEN_ADD = "notify-token-add"; | ||
export const NOTIFY_TOKEN_UPDATE = "notify-token-update"; | ||
|
||
|
||
export function notifyTokenAdd(token: string) { | ||
return { | ||
type: NOTIFY_TOKEN_ADD, | ||
payload: token | ||
} | ||
} | ||
|
||
export function notifyTokenUpdate(token: string) { | ||
return { | ||
type: NOTIFY_TOKEN_UPDATE, | ||
payload: token | ||
} | ||
} | ||
|
||
const initState: NotifyType = { | ||
token: localStorage.getItem('notification_token'), | ||
} | ||
|
||
export const notifyReducer = (state = initState, action: Action): NotifyType => { | ||
|
||
switch (action.type) { | ||
|
||
//add new todo in todo list | ||
case NOTIFY_TOKEN_ADD: | ||
|
||
return { | ||
...state, | ||
token: action.payload | ||
} | ||
|
||
|
||
case NOTIFY_TOKEN_UPDATE: | ||
|
||
return { | ||
...state, | ||
token: action.payload | ||
} | ||
|
||
default: | ||
return state | ||
} | ||
} |
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,8 +1,10 @@ | ||
|
||
import { combineReducers } from "redux"; | ||
import todoReducer from '../pages/todo/redux/todoReducer'; | ||
import { notifyReducer } from './notifyStore'; | ||
|
||
|
||
export default combineReducers({ | ||
...todoReducer | ||
...todoReducer, | ||
notify: notifyReducer | ||
}); |