Skip to content

Commit

Permalink
feat: add alerts component and global alerts store
Browse files Browse the repository at this point in the history
  • Loading branch information
invm committed Jul 1, 2023
1 parent dba325f commit d994898
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 7 deletions.
17 changes: 11 additions & 6 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ import './utils/i18n';
import Home from './pages/Home';
import { Route, Router, Routes } from "@solidjs/router"; // 👈 Import the router
import Connection from './pages/Connection';
import { StoreProvider } from './services/Context';
import { Alerts } from './components/Alerts';

function App() {
return (
<Router>
<Routes>
<Route path="/" component={Home} />
<Route path="/connection" component={Connection} />
</Routes>
</Router>
<StoreProvider>
<Router>
<Routes>
<Route path="/" component={Home} />
<Route path="/connection" component={Connection} />
</Routes>
</Router>
<Alerts />
</StoreProvider>
);
}

Expand Down
5 changes: 4 additions & 1 deletion src/components/AddConnectionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import { invoke } from '@tauri-apps/api';

import { Alert, Button, TextInput, Select, ColorCircle } from './UI';
import {
ConnectionColor, PORTS_MAP, Schemes, AvailableConnectionModes, SocketPathDefaults,
PORTS_MAP, Schemes, AvailableConnectionModes, SocketPathDefaults,
connectionColors, ConnectionMode, connectionModes, schemes, HostCredentials, SocketCredentials, FileCredentials
} from '../interfaces';
import { titleCase } from '../utils/formatters';
import { FileInput } from './UI/FileInput';
import { omit } from '../utils/utils';
import { useAppSelector } from '../services/Context';

const MIN_LENGTH_STR = 2;
const MAX_LENGTH_STR = 255;
Expand Down Expand Up @@ -75,6 +76,7 @@ const defaultValues = {
}

const AddConnectionForm = () => {
const { errorService: { addError } } = useAppSelector()
const formHandler = useFormHandler(zodSchema(ConnectionFormSchema));
const { formData, isFormInvalid, setFieldDefaultValue, getFormErrors, setFieldValue } = formHandler;

Expand All @@ -92,6 +94,7 @@ const AddConnectionForm = () => {
await invoke('add_connection', { name, color, scheme })
} catch (error) {
console.error(error);
addError((error as any).message);
}
};

Expand Down
19 changes: 19 additions & 0 deletions src/components/Alerts.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { For } from "solid-js"
import { useAppSelector } from "../services/Context"

export const Alerts = () => {
const { errorService: { errors } } = useAppSelector()
return (
<div class="absolute">
<div class="toast">
<For each={errors}>
{error => (
<div class="alert alert-error">
<span>{error.message}</span>
</div>
)}
</For>
</div>
</div>
)
}
20 changes: 20 additions & 0 deletions src/services/Context.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createContext, ParentComponent, useContext } from "solid-js"
import { ErrorService } from "./Error"


export type RootState = {
errorService: ReturnType<typeof ErrorService>
}


const rootState: RootState = {
errorService: ErrorService()
}

const StoreContext = createContext<RootState>()

export const useAppSelector = () => useContext(StoreContext)!

export const StoreProvider: ParentComponent = (props) => {
return <StoreContext.Provider value={rootState}> {props.children} </StoreContext.Provider>
}
24 changes: 24 additions & 0 deletions src/services/Error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createStore } from "solid-js/store"
import { randomId } from "../utils/utils";

type Error = {
message: string;
id: string;
}

const errorStore = createStore<Error[]>([]);

export const ErrorService = () => {
const [errors, setErrors] = errorStore

const addError = (error: string) => {
const id = randomId()
setErrors(errors.concat({ message: error, id }))

setTimeout(() => {
setErrors(errors.filter(e => e.id !== id))
}, 5000)
}

return { errors, addError }
}
2 changes: 2 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export const firstKey = (obj: Record<string, any>) => {
for (const key in obj) return key;
}

export const randomId = () => Math.random().toString(36).substring(12);

0 comments on commit d994898

Please sign in to comment.