From d890c1aa6403d05ba4ac7a8e46ce70c21cfc9eee Mon Sep 17 00:00:00 2001 From: Ari Date: Wed, 31 Aug 2022 15:15:30 -0400 Subject: [PATCH] functioning profiles --- api/src/api/app.py | 10 ++-- api/src/api/routes/automations.py | 8 ++- api/src/app.py | 2 +- api/src/automations/manager.py | 1 - api/tests/unittests/api/util.py | 2 +- webapp/src/apiService/core.ts | 14 ++++-- webapp/src/apiService/index.ts | 2 +- webapp/src/apiService/types.ts | 7 +-- webapp/src/apiService/util.ts | 15 ++++-- .../AutomationManager/MetadataBox/index.tsx | 36 ++++++++------ webapp/src/components/BottomBar/index.css | 7 +++ webapp/src/components/BottomBar/index.tsx | 2 +- webapp/src/components/Icons/ButtonIcons.tsx | 38 +++++++++----- .../src/components/Inputs/InputTextView.tsx | 49 ++++++++++++------- webapp/src/index.tsx | 16 ++---- webapp/src/lang/eng.json | 3 ++ webapp/src/lang/fra.json | 2 + 17 files changed, 136 insertions(+), 78 deletions(-) diff --git a/api/src/api/app.py b/api/src/api/app.py index b75d223..f9c35ac 100644 --- a/api/src/api/app.py +++ b/api/src/api/app.py @@ -3,13 +3,13 @@ from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import RedirectResponse from fastapi.staticfiles import StaticFiles -from src.automations.manager import AutomationManager from src.env import API_PREFIX, ORIGIN, ROOT_FOLDER +from src.hass_config.loader import HassConfig from .routes import automations, details, ping, socket -def make_app(automation_mgr: AutomationManager) -> FastAPI: +def make_app(hass_config: HassConfig) -> FastAPI: app = FastAPI() has_web_folder = (ROOT_FOLDER / "web").exists() if has_web_folder: @@ -33,11 +33,9 @@ def make_app(automation_mgr: AutomationManager) -> FastAPI: ) app.include_router(ping.router, prefix="/ping") app.include_router( - automations.make_automation_router(automation_mgr), prefix=f"{API_PREFIX}/automations" - ) - app.include_router( - details.make_details_router(automation_mgr.hass_config), prefix=f"{API_PREFIX}/details" + automations.make_automation_router(hass_config), prefix=f"{API_PREFIX}/automations" ) + app.include_router(details.make_details_router(hass_config), prefix=f"{API_PREFIX}/details") app.include_router(socket.router, prefix=f"{API_PREFIX}/socket") @app.get("/") diff --git a/api/src/api/routes/automations.py b/api/src/api/routes/automations.py index 753f135..b277bf0 100644 --- a/api/src/api/routes/automations.py +++ b/api/src/api/routes/automations.py @@ -5,13 +5,15 @@ from src.automations.errors import FailedDeletion from src.automations.manager import AutomationManager from src.automations.types import Automation, ExtenededAutomation from src.errors import ErrorSet +from src.hass_config.loader import HassConfig -def make_automation_router(automations: AutomationManager) -> APIRouter: +def make_automation_router(hass_config: HassConfig) -> APIRouter: router = APIRouter() @router.post("/list") def list_autos(body: ListParams) -> ListData[ExtenededAutomation]: + automations = AutomationManager(hass_config) try: automations.reload() return ListData( @@ -27,18 +29,22 @@ def make_automation_router(automations: AutomationManager) -> APIRouter: @router.post("/item") def insert_auto(body: Automation): + automations = AutomationManager(hass_config) automations.create(body) @router.put("/item") def update_auto(body: ExtenededAutomation): + automations = AutomationManager(hass_config) automations.update(body) @router.post("/item/tags") def update_tags(body: UpdateTags): + automations = AutomationManager(hass_config) automations.update_tags(body.automation_id, body.tags) @router.delete("/item") def delete_auto(body: ExtenededAutomation): + automations = AutomationManager(hass_config) try: automations.delete(body) except FailedDeletion as err: diff --git a/api/src/app.py b/api/src/app.py index 259a8eb..bdab093 100644 --- a/api/src/app.py +++ b/api/src/app.py @@ -3,4 +3,4 @@ from src.automations.manager import AutomationManager from src.hass_config import find_hass_config hass_config = find_hass_config() -app = make_app(AutomationManager(hass_config)) +app = make_app(hass_config) diff --git a/api/src/automations/manager.py b/api/src/automations/manager.py index 9fa2c46..429417d 100644 --- a/api/src/automations/manager.py +++ b/api/src/automations/manager.py @@ -16,7 +16,6 @@ from .errors import ( from .loader import ( get_base_automation_key, load_and_iter_automations, - load_automation_path, ) from .tags import TagManager from .types import Automation, ExtenededAutomation diff --git a/api/tests/unittests/api/util.py b/api/tests/unittests/api/util.py index aa28ffb..095538c 100644 --- a/api/tests/unittests/api/util.py +++ b/api/tests/unittests/api/util.py @@ -19,4 +19,4 @@ class BaseTestCase(TestCase): self.hass_config, self.automation_loader, ) = get_example_automation_loader(config_to_copy=self.source_config_path) - self.client = TestClient(make_app(self.automation_loader)) + self.client = TestClient(make_app(self.hass_config)) diff --git a/webapp/src/apiService/core.ts b/webapp/src/apiService/core.ts index 5b8ed3c..2fb7205 100644 --- a/webapp/src/apiService/core.ts +++ b/webapp/src/apiService/core.ts @@ -23,10 +23,16 @@ export const useAPIService = ( // initial load of autos useEffect(() => { - autoManager.reload(); - userProfileManager.reload(); - // eslint-disable-next-line - }, []); + if (state.automations.ready === undefined) { + autoManager.reload(); + } + }, [state.automations.ready, autoManager]); + // initial load of profile + useEffect(() => { + if (state.userProfile.ready === undefined) { + userProfileManager.reload(); + } + }, [state.userProfile.ready, userProfileManager]); return { state: { diff --git a/webapp/src/apiService/index.ts b/webapp/src/apiService/index.ts index 2f6ca02..d10ec7d 100644 --- a/webapp/src/apiService/index.ts +++ b/webapp/src/apiService/index.ts @@ -15,7 +15,7 @@ const baseURL = new URL( ); export const wsURL = baseURL + "socket"; export const remoteAutoAPI = makeRemoteAPI(baseURL + "automations"); -export const detailsAPI = makeRemoteAPI(baseURL + "details/"); +export const detailsAPI = makeRemoteAPI(baseURL + "details"); export const useConnectedApiService = () => useAPIService(makeAutomationAPI(remoteAutoAPI), makeProfileAPI(detailsAPI)); export const useMockApiService = ( diff --git a/webapp/src/apiService/types.ts b/webapp/src/apiService/types.ts index 64041ac..9b20357 100644 --- a/webapp/src/apiService/types.ts +++ b/webapp/src/apiService/types.ts @@ -19,7 +19,8 @@ export type APIResponse = ok: false; error: string; }; -export type Errorable = +export type Waitable = + | { ready: undefined } | ({ ready: true } & APIResponse) | { ready: false; @@ -31,8 +32,8 @@ export interface ListData { } export interface ApiState { - automations: Errorable>; - userProfile: Errorable; + automations: Waitable>; + userProfile: Waitable; } export type UserProfile = { diff --git a/webapp/src/apiService/util.ts b/webapp/src/apiService/util.ts index 8ae36fe..ef5f3e5 100644 --- a/webapp/src/apiService/util.ts +++ b/webapp/src/apiService/util.ts @@ -6,8 +6,8 @@ import { UserProfile } from "apiService/types"; export const useDefaultApiState = () => useState({ - automations: { ready: false }, - userProfile: { ready: false }, + automations: { ready: undefined }, + userProfile: { ready: undefined }, }); export const makeReloadAutomations = ( @@ -76,6 +76,15 @@ export const makeProfileManager = ( userProfileAPI .update(data) .then(() => reload()) - .catch(() => reload()), + .catch((err) => + setState({ + ...state, + userProfile: { + ok: false, + error: String(err), + ready: true, + }, + }) + ), }; }; diff --git a/webapp/src/components/AutomationManager/MetadataBox/index.tsx b/webapp/src/components/AutomationManager/MetadataBox/index.tsx index ab90d41..7fee24e 100644 --- a/webapp/src/components/AutomationManager/MetadataBox/index.tsx +++ b/webapp/src/components/AutomationManager/MetadataBox/index.tsx @@ -9,12 +9,15 @@ import DeleteForeverIcon from "@mui/icons-material/DeleteForever"; import SelectIcon from "@mui/icons-material/HighlightAltSharp"; import RunIcon from "@mui/icons-material/RunCircleOutlined"; +import Tooltip from "@mui/material/Tooltip"; +import Box from "@mui/material/Box"; +import Chip from "@mui/material/Chip"; + import { InputTextView } from "components/Inputs/InputTextView"; import { ButtonIcon } from "components/Icons/ButtonIcons"; import { AutomationManagerAuto } from "../types"; import { Tags } from "./Tags"; import { TagDB } from "../TagDB"; -import { Chip, Tooltip } from "@mui/material"; import { useLang } from "lang"; export type MetadataBoxProps = AutomationManagerAuto & { @@ -34,7 +37,7 @@ export const MetadataBox: FC = (props) => { ? langStore.get(`AUTOMATION_STATE_${props.state}`) : props.state; return ( -
= (props) => { /> - - - + = (props) => {
} onClick={props.onSelect} - title={langStore.get("SELECT_AUTOMATION")} /> } onClick={props.onRun} - title={langStore.get("TRIGGER_AUTOMATION")} /> } onClick={props.onDelete} - title={langStore.get("DELETE_AUTOMATION")} />
- + ); }; diff --git a/webapp/src/components/BottomBar/index.css b/webapp/src/components/BottomBar/index.css index a79134c..49a6135 100644 --- a/webapp/src/components/BottomBar/index.css +++ b/webapp/src/components/BottomBar/index.css @@ -40,4 +40,11 @@ .bottom-bar .settings svg { height: 0.5em; width: 0.5em; +} + +.user-profile { + width: 100%; + height: 100%; + min-width: 10em; + min-height: 10em; } \ No newline at end of file diff --git a/webapp/src/components/BottomBar/index.tsx b/webapp/src/components/BottomBar/index.tsx index 925da50..61fdf43 100644 --- a/webapp/src/components/BottomBar/index.tsx +++ b/webapp/src/components/BottomBar/index.tsx @@ -34,7 +34,7 @@ export const BottomBar: FC = ({ api }) => { version: {version} ); diff --git a/webapp/src/components/Icons/ButtonIcons.tsx b/webapp/src/components/Icons/ButtonIcons.tsx index e3fc561..6e74d8a 100644 --- a/webapp/src/components/Icons/ButtonIcons.tsx +++ b/webapp/src/components/Icons/ButtonIcons.tsx @@ -1,6 +1,8 @@ import "./ButtonIcons.css"; +import Tooltip from "@mui/material/Tooltip"; import { FC, ReactNode } from "react"; -import MuiIconButton, { IconButtonTypeMap } from "@mui/material/IconButton"; +import IconButton, { IconButtonTypeMap } from "@mui/material/IconButton"; +import { useLang } from "lang"; export type ButtonIconProps = { icon: ReactNode; @@ -10,21 +12,31 @@ export type ButtonIconProps = { disabled?: boolean; color?: IconButtonTypeMap["props"]["color"]; borderless?: boolean; + titleSlug?: string; + titleParams?: Record; }; export const ButtonIcon: FC = (props) => { + const langStore = useLang(); return ( - - {props.icon} - + + {props.icon} + + ); }; diff --git a/webapp/src/components/Inputs/InputTextView.tsx b/webapp/src/components/Inputs/InputTextView.tsx index 1d4fdf7..ed16c30 100644 --- a/webapp/src/components/Inputs/InputTextView.tsx +++ b/webapp/src/components/Inputs/InputTextView.tsx @@ -4,6 +4,8 @@ import { FC, PropsWithChildren, useState } from "react"; import CheckCircleOutlineOutlinedIcon from "@mui/icons-material/CheckCircleOutlineOutlined"; import ModeEditOutlineOutlinedIcon from "@mui/icons-material/ModeEditOutlineOutlined"; import TextField from "@mui/material/TextField"; +import Tooltip from "@mui/material/Tooltip"; +import { useLang } from "lang"; export type InputTextViewProps = { className?: string; @@ -12,11 +14,15 @@ export type InputTextViewProps = { initialViewMode?: boolean; placeholder?: string; disabled?: boolean; + borderless?: boolean; + titleSlug?: string; + titleParams?: Record; }; export const InputTextView: FC> = ( props ) => { + const langStore = useLang(); const [viewMode, setViewMode] = useState(props.initialViewMode ?? true); const [text, setText] = useState(props.value); const className = [ @@ -27,24 +33,33 @@ export const InputTextView: FC> = ( if (viewMode) { return ( -
!props.disabled && setViewMode(false)} + - {!!props.value ? ( -
- {props.value} -
- ) : ( -
- {props.placeholder} -
- )} - {!props.disabled && ( - - )} - {props.children} -
+
!props.disabled && setViewMode(false)} + > + {!!props.value ? ( +
+ {props.value} +
+ ) : ( +
+ {props.placeholder} +
+ )} + {!props.disabled && ( + + )} + {props.children} +
+ ); } else { return ( diff --git a/webapp/src/index.tsx b/webapp/src/index.tsx index 05fbfe1..00ddbc6 100644 --- a/webapp/src/index.tsx +++ b/webapp/src/index.tsx @@ -1,24 +1,18 @@ import React from "react"; -import ReactDOM from "react-dom"; import "./styles/root.css"; import App from "./App"; import reportWebVitals from "./reportWebVitals"; -ReactDOM.render( +import { createRoot } from "react-dom/client"; +const container = document.getElementById("root"); +const root = createRoot(container!); +root.render( - , - document.getElementById("root") + ); // If you want to start measuring performance in your app, pass a function // to log results (for example: reportWebVitals(console.info)) // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals reportWebVitals(console.info); - -// import { createRoot } from 'react-dom/client'; -// const container = document.getElementById('root'); -// const root = createRoot(container!); -// root.render( -// -// ); diff --git a/webapp/src/lang/eng.json b/webapp/src/lang/eng.json index 496e58c..7b34976 100644 --- a/webapp/src/lang/eng.json +++ b/webapp/src/lang/eng.json @@ -77,6 +77,8 @@ "THEME": "Theme", "LANGUAGE": "Language", + "USER_PRERENCES": "Settings", + "SELECT_AUTOMATION": "Select automation", "TRIGGER_AUTOMATION": "Trigger automation", "DELETE_AUTOMATION": "Delete automation", @@ -91,6 +93,7 @@ "MISSING_TEXT": "MISSING TEXT -- {{langId}}", "AUTOMATION_METADATA_TOOLTIP_TEXT": "This automation is located in the file {{source_file}}. It's currently '{{state}}', it's entity id is '{{entityId}}' and it's automation id is '{{id}}'", + "AUTOMATION_METADATA_TOOLTIP_SIMPLE": "ID={{id}}, EntityID={{entityId}}", "ERROR_FAILED_TO_SAVE": "Failed to save automation \"{{aid}}\" contents - {{contents}} -- not found in `/config`", "ERROR_INVALID_AUTOMATION_STATE": "Invalid state", diff --git a/webapp/src/lang/fra.json b/webapp/src/lang/fra.json index 712ea4e..566e639 100644 --- a/webapp/src/lang/fra.json +++ b/webapp/src/lang/fra.json @@ -80,6 +80,8 @@ "THEME": "Thème", "LANGUAGE": "Langue", + "USER_PRERENCES": "Préférences de l'utilisateur", + "SELECT_AUTOMATION": "Sélectionnez l'automatisation", "TRIGGER_AUTOMATION": "Déclencher l'automatisation", "DELETE_AUTOMATION": "Supprimer l'automatisation",