Skip to content

Changes for new storage#2228

Merged
balloob merged 15 commits intodevfrom
edit-storage
Dec 10, 2018
Merged

Changes for new storage#2228
balloob merged 15 commits intodevfrom
edit-storage

Conversation

@bramkragten
Copy link
Copy Markdown
Member

@bramkragten bramkragten commented Dec 7, 2018

Requires home-assistant/core#19101

Fixes #2224
Fixes #2223
Fixes #2222
Fixes #2221
Fixes #2219

@ghost ghost assigned bramkragten Dec 7, 2018
@ghost ghost added the in progress label Dec 7, 2018
@bramkragten bramkragten mentioned this pull request Dec 7, 2018

export interface LovelaceCardConfig {
id?: string;
index?: number;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't support stacks editing, however, since it's frontend only, we can change it later.

Copy link
Copy Markdown
Member Author

@bramkragten bramkragten Dec 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we should have a parent_index for stacks or something

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or we just have an array with indices.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that would be even better, then it could work with endless stacks

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const [viewIndex, ...cardIndices] = obj_path

config: LovelaceCardConfig
): Promise<void> => notImplemented();

export const deleteCard = (
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should mutate these functions to be like this:

export const deleteCard = (config: LovelaceCardConfig, viewIndex, cardIndex): LovelaceCardConfig;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And then the caller can route the result to updateCardConfig. Will make it easy to test the functions and reason about it too.

@ghost ghost assigned balloob Dec 8, 2018
@balloob
Copy link
Copy Markdown
Member

balloob commented Dec 8, 2018

Here is example of how we should get config edit functions:

We should represent the path of an object as [viewIdx, cardIdx, <cardIdx, …>]. That way we can support nested objects in the future easily.

export const addCard = (
  config: LovelaceConfig,
  path: [number],
  cardConfig: LovelaceCardConfig
): LovelaceConfig => {
  const [viewIndex] = path;
  const views: LovelaceViewConfig[] = [];

  config.views.forEach((viewConf, index) => {
    if (index !== viewIndex) {
      views.push(config.views[index]);
      return;
    }

    const cards = viewConf.cards
      ? [...viewConf.cards, cardConfig]
      : [cardConfig];

    views.push({
      ...viewConf,
      cards,
    });
  });

  return {
    ...config,
    views,
  };
};

export const replaceCard = (
  config: LovelaceConfig,
  path: [number, number],
  cardConfig: LovelaceCardConfig
): LovelaceConfig => {
  const [viewIndex, cardIndex] = path;
  const views: LovelaceViewConfig[] = [];

  config.views.forEach((viewConf, index) => {
    if (index !== viewIndex) {
      views.push(config.views[index]);
      return;
    }

    views.push({
      ...viewConf,
      cards: (viewConf.cards || []).map((origConf, ind) =>
        ind === cardIndex ? cardConfig : origConf
      ),
    });
  });

  return {
    ...config,
    views,
  };
};

export const deleteCard = (
  config: LovelaceConfig,
  path: [number, number]
): LovelaceConfig => {
  const [viewIndex, cardIndex] = path;
  const views: LovelaceViewConfig[] = [];

  config.views.forEach((viewConf, index) => {
    if (index !== viewIndex) {
      views.push(config.views[index]);
      return;
    }

    views.push({
      ...viewConf,
      cards: (viewConf.cards || []).filter(
        (_origConf, ind) => ind !== cardIndex
      ),
    });
  });

  return {
    ...config,
    views,
  };
};

export const addView = (
  config: LovelaceConfig,
  viewConfig: LovelaceViewConfig
): LovelaceConfig => ({
  ...config,
  views: config.views.concat(viewConfig),
});

export const replaceView = (
  config: LovelaceConfig,
  viewIndex: number,
  viewConfig: LovelaceViewConfig
): LovelaceConfig => ({
  ...config,
  views: config.views.map((origView, index) =>
    index === viewIndex ? viewConfig : origView
  ),
});

export const deleteView = (
  config: LovelaceConfig,
  viewIndex: number
): LovelaceConfig => ({
  ...config,
  views: config.views.filter((_origView, index) => index !== viewIndex),
});

@@ -0,0 +1,17 @@
import { LovelaceConfig } from "../../../data/lovelace";

export const formatLovelaceConfig = (
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might not need this if we can just pass viewIndex and cardIndex to the edit button for cards.

The edit card dialog then gets params (config: LovelaceConfig, [viewId, cardId])

@balloob balloob merged commit 4f0a965 into dev Dec 10, 2018
@delete-merged-branch delete-merged-branch bot deleted the edit-storage branch December 10, 2018 07:57
@ghost ghost removed the in progress label Dec 10, 2018
@balloob balloob mentioned this pull request Dec 10, 2018
@github-actions github-actions bot locked and limited conversation to collaborators Jul 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

3 participants