Rename Service provider to a Config provider

This commit is contained in:
Aj - Thomas
2022-05-02 15:08:52 +02:00
parent a060be10c3
commit ea77bc2a18

View File

@@ -1,25 +1,33 @@
// src/context/state.js // src/context/state.js
import { createContext, ReactNode, useContext, useState } from 'react'; import { createContext, ReactNode, useContext, useState } from 'react';
import { serviceItem } from './types'; import { Config, serviceItem } from './types';
type servicesContextType = { type configContextType = {
services: serviceItem[]; config: Config;
setServicesState: (services: serviceItem[]) => void; setConfig: (newconfig: Config) => void;
addService: (service: serviceItem) => void; addService: (service: serviceItem) => void;
removeService: (name: string) => void; removeService: (name: string) => void;
saveConfig: (newconfig: Config) => void;
}; };
const servicesContext = createContext<servicesContextType>({ const configContext = createContext<configContextType>({
services: [], config: {
setServicesState: () => {}, services: [],
settings: {
searchBar: true,
searchUrl: 'https://www.google.com/search?q=',
},
},
setConfig: () => {},
addService: () => {}, addService: () => {},
removeService: () => {}, removeService: () => {},
saveConfig: () => {},
}); });
export function useServices() { export function useConfig() {
const context = useContext(servicesContext); const context = useContext(configContext);
if (context === undefined) { if (context === undefined) {
throw new Error('useServices must be used within a ServicesProvider'); throw new Error('useConfig must be used within a ConfigProvider');
} }
return context; return context;
} }
@@ -28,40 +36,65 @@ type Props = {
children: ReactNode; children: ReactNode;
}; };
export function ServicesProvider({ children }: Props) { export function ConfigProvider({ children }: Props) {
const [services, setServices] = useState<serviceItem[]>([ const [config, setConfigInternal] = useState<Config>({
{ services: [
type: 'Other', {
name: 'example', type: 'Other',
icon: 'https://c.tenor.com/o656qFKDzeUAAAAC/rick-astley-never-gonna-give-you-up.gif', name: 'example',
url: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', icon: 'https://c.tenor.com/o656qFKDzeUAAAAC/rick-astley-never-gonna-give-you-up.gif',
url: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
},
],
settings: {
searchBar: true,
searchUrl: 'https://www.google.com/search?q=',
}, },
]); });
function setServicesState(services: serviceItem[]) { function setConfig(newConfig: Config) {
setServices(services); setConfigInternal(newConfig);
localStorage.setItem('services', JSON.stringify(services)); saveConfig(newConfig);
} }
function addService(item: serviceItem) { function addService(item: serviceItem) {
setServices([...services, item]); setConfigInternal({
localStorage.setItem('services', JSON.stringify([...services, item])); ...config,
services: [...config.services, item],
});
saveConfig({
...config,
services: [...config.services, item],
});
} }
function removeService(name: string) { function removeService(name: string) {
setServices(services.filter((s) => s.name !== name)); // Remove the service with name in config item
localStorage.setItem('services', JSON.stringify(services.filter((s) => s.name !== name))); setConfigInternal({
...config,
services: config.services.filter((service) => service.name !== name),
});
saveConfig({
...config,
services: config.services.filter((service) => service.name !== name),
});
}
function saveConfig(newconfig: Config) {
if (!newconfig) return;
localStorage.setItem('config', JSON.stringify(newconfig));
} }
const value = { const value = {
services, config,
setServicesState, setConfig,
addService, addService,
removeService, removeService,
saveConfig,
}; };
return ( return (
<> <>
<servicesContext.Provider value={value}>{children}</servicesContext.Provider> <configContext.Provider value={value}>{children}</configContext.Provider>
</> </>
); );
} }