Skip to content

Commit

Permalink
Updated base shell
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarik committed Aug 7, 2024
1 parent 62d9e09 commit f6db3e8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 60 deletions.
4 changes: 2 additions & 2 deletions packages/base-shell/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/base-shell/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "base-shell",
"version": "2.5.10",
"version": "2.5.14",
"description": "base-shell React component",
"main": "lib/index.js",
"module": "es/index.js",
Expand Down
44 changes: 23 additions & 21 deletions packages/base-shell/src/providers/Auth/Provider.js
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 packages/base-shell/src/providers/SimpleValues/Provider.js
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;

0 comments on commit f6db3e8

Please sign in to comment.