-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tarik
committed
Aug 7, 2024
1 parent
62d9e09
commit f6db3e8
Showing
4 changed files
with
65 additions
and
60 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,44 +1,46 @@ | ||
import React, { useEffect, useReducer } from 'react' | ||
import Context from './Context' | ||
import React, { useEffect, useReducer } from "react"; | ||
import Context from "./Context"; | ||
|
||
function reducer(state, action) { | ||
const { type, auth } = action | ||
const { type, auth } = action; | ||
switch (type) { | ||
case 'SET_AUTH': | ||
return auth | ||
case 'UPDATE_AUTH': | ||
return { ...state, ...auth } | ||
case "SET_AUTH": | ||
return auth; | ||
case "UPDATE_AUTH": | ||
return { ...state, ...auth }; | ||
default: | ||
throw new Error() | ||
throw new Error(); | ||
} | ||
} | ||
|
||
const Provider = ({ persistKey = 'auth', children }) => { | ||
const persistAuth = JSON.parse(localStorage.getItem(persistKey)) | ||
const Provider = ({ persistKey = "auth", children }) => { | ||
const persistAuth = JSON.parse( | ||
localStorage.getItem(persistKey)?.replace("undefined", "{}") || "{}" | ||
); | ||
|
||
const [auth, dispatch] = useReducer(reducer, persistAuth || {}) | ||
const [auth, dispatch] = useReducer(reducer, persistAuth || {}); | ||
|
||
useEffect(() => { | ||
try { | ||
localStorage.setItem(persistKey, JSON.stringify(auth)) | ||
localStorage.setItem(persistKey, JSON.stringify(auth)); | ||
} catch (error) { | ||
console.warn(error) | ||
console.warn(error); | ||
} | ||
}, [auth, persistKey]) | ||
}, [auth, persistKey]); | ||
|
||
const setAuth = (auth) => { | ||
dispatch({ type: 'SET_AUTH', auth }) | ||
} | ||
dispatch({ type: "SET_AUTH", auth }); | ||
}; | ||
|
||
const updateAuth = (auth) => { | ||
dispatch({ type: 'UPDATE_AUTH', auth }) | ||
} | ||
dispatch({ type: "UPDATE_AUTH", auth }); | ||
}; | ||
|
||
return ( | ||
<Context.Provider value={{ auth, setAuth, updateAuth }}> | ||
{children} | ||
</Context.Provider> | ||
) | ||
} | ||
); | ||
}; | ||
|
||
export default Provider | ||
export default Provider; |
75 changes: 39 additions & 36 deletions
75
packages/base-shell/src/providers/SimpleValues/Provider.js
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,77 +1,80 @@ | ||
import React, { useEffect, useReducer } from 'react' | ||
import Context from './Context' | ||
import React, { useEffect, useReducer } from "react"; | ||
import Context from "./Context"; | ||
|
||
function reducer(state, action) { | ||
const { type, key, value, persist } = action | ||
const { type, key, value, persist } = action; | ||
switch (type) { | ||
case 'add': | ||
return { ...state, [key]: { value, persist } } | ||
case 'clear': | ||
const { [key]: clearedKey, ...rest } = state | ||
return { ...rest } | ||
case 'clear_all': | ||
return {} | ||
case "add": | ||
return { ...state, [key]: { value, persist } }; | ||
case "clear": | ||
const { [key]: clearedKey, ...rest } = state; | ||
return { ...rest }; | ||
case "clear_all": | ||
return {}; | ||
default: | ||
throw new Error() | ||
throw new Error(); | ||
} | ||
} | ||
|
||
function getInitState(persistKey) { | ||
let persistedValues = {} | ||
let persistedValues = {}; | ||
try { | ||
persistedValues = JSON.parse(localStorage.getItem(persistKey)) || {} | ||
persistedValues = | ||
JSON.parse( | ||
localStorage.getItem(persistKey)?.replace("undefined", "{}") || "{}" | ||
) || {}; | ||
} catch (error) { | ||
console.warn(error) | ||
console.warn(error); | ||
} | ||
return persistedValues | ||
return persistedValues; | ||
} | ||
|
||
const Provider = ({ children, persistKey = 'simple_values' }) => { | ||
const [state, dispatch] = useReducer(reducer, getInitState(persistKey)) | ||
const Provider = ({ children, persistKey = "simple_values" }) => { | ||
const [state, dispatch] = useReducer(reducer, getInitState(persistKey)); | ||
|
||
useEffect(() => { | ||
try { | ||
const persistValues = {} | ||
const persistValues = {}; | ||
|
||
Object.keys(state).map((k) => { | ||
if (state[k].persist) { | ||
persistValues[k] = { value: state[k].value, persist: true } | ||
persistValues[k] = { value: state[k].value, persist: true }; | ||
} | ||
|
||
return k | ||
}) | ||
return k; | ||
}); | ||
|
||
localStorage.setItem(persistKey, JSON.stringify(persistValues)) | ||
localStorage.setItem(persistKey, JSON.stringify(persistValues)); | ||
} catch (error) { | ||
console.warn(error) | ||
console.warn(error); | ||
} | ||
}, [state, persistKey]) | ||
}, [state, persistKey]); | ||
|
||
const setValue = (key, value, persist = false) => { | ||
dispatch({ type: 'add', key, value, persist }) | ||
} | ||
dispatch({ type: "add", key, value, persist }); | ||
}; | ||
|
||
const getValue = (key, defaultValue) => { | ||
if (state[key] !== undefined) { | ||
return state[key].value | ||
return state[key].value; | ||
} else { | ||
return defaultValue | ||
return defaultValue; | ||
} | ||
} | ||
}; | ||
|
||
const clearValue = (key) => { | ||
dispatch({ type: 'clear', key }) | ||
} | ||
dispatch({ type: "clear", key }); | ||
}; | ||
|
||
const clearAll = () => { | ||
dispatch({ type: 'clear_all' }) | ||
} | ||
dispatch({ type: "clear_all" }); | ||
}; | ||
|
||
return ( | ||
<Context.Provider value={{ setValue, getValue, clearValue, clearAll }}> | ||
{children} | ||
</Context.Provider> | ||
) | ||
} | ||
); | ||
}; | ||
|
||
export default Provider | ||
export default Provider; |