Skip to content

[data view editor] Form state refactor - move all state logic into service and out of react state management#143604

Merged
mattkime merged 33 commits intoelastic:mainfrom
mattkime:reduce_react_state_data_view_create
Nov 29, 2022
Merged

[data view editor] Form state refactor - move all state logic into service and out of react state management#143604
mattkime merged 33 commits intoelastic:mainfrom
mattkime:reduce_react_state_data_view_create

Conversation

@mattkime
Copy link
Copy Markdown
Contributor

@mattkime mattkime commented Oct 18, 2022

Summary

This PR completes the data view editor state service. Of note:

  • All form business logic is now in service although validation is still handled by the form lib
  • Service is initialized with dependencies, form config, and initial state
  • Service state is updated via simple method calls
  • Service state updates are provided to React code via observables using useObservable
  • Service is provided via parent component which uses useRef so the service is created once per flyout creation

IMO the diff isn't very helpful. It might be easier to review the changed files in completion.

Follow up to #142421

showAllIndices: allowHidden,
});
dataViewEditorService.setIndexPattern(title);
}, [dataViewEditorService, title]);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

setting state

const isLoadingDataViewNames$ = useRef(new BehaviorSubject<boolean>(true));
const existingDataViewNames$ = useRef(new BehaviorSubject<string[]>([]));
const isLoadingDataViewNames = useObservable(isLoadingDataViewNames$.current, true);
const existingDataViewNames = useObservable(dataViewEditorService.dataViewNames$);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

getting state

// alernates between value and undefined so validation can treat new value as thought its a promise
rollupIndexForProvider$ = new Subject<string | undefined | null>();

matchedIndices$ = new BehaviorSubject<MatchedIndicesSet>(matchedIndiciesDefault);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

public observables for react code to subscribe to


setIndexPattern = (indexPattern: string) => {
this.indexPattern = indexPattern;
this.loadIndices();
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

simple state update that kicks off observable updates

services: { dataViews, notifications, http },
} = useKibana<DataViewEditorContext>();

const dataViewEditorService = useRef(
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The intention is to have a stable reference to a single service for the life of the component.

@mattkime mattkime changed the title partial progress [data view editor] Form state refactor - move all state logic into service and out of react state management Nov 3, 2022
@mattkime mattkime added v8.6.0 Feature:Data Views Data Views code and UI - index patterns before 8.0 Feature:Kibana Management Feature label for Data Views, Advanced Setting, Saved Object management pages Team:DataDiscovery Discover, search (data plugin and KQL), data views, saved searches. For ES|QL, use Team:ES|QL. t// labels Nov 3, 2022
Copy link
Copy Markdown
Contributor

@Dosant Dosant left a comment

Choose a reason for hiding this comment

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

Recent changes to the service initialization lgtm 👍

I started testing,
Noticed that you edit a data view, press save, then it goes into an indefinite "loading" state. In main it simply saves and closes the flyout. I assume that the culprit might cause other similar bugs


Also seem to be the same problem for editing and ad-hoc view, even after doing some changes,

Screenshot 2022-11-04 at 15 16 00

@mattkime
Copy link
Copy Markdown
Contributor Author

mattkime commented Nov 5, 2022

@Dosant I resolved that issue - the index pattern field wasn't being updated and therefore an observable wasn't getting a new value which was necessary for validation. I added a line of code which generates the new value.

I think there's a bit of a mismatch between form lib and using observables for state. In the long run I'd like to rectify this but I see a number of conversations around state management before that could happen.

Copy link
Copy Markdown
Contributor

@Dosant Dosant left a comment

Choose a reason for hiding this comment

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

Retested, seem to be working well

@mattkime mattkime requested a review from a team as a code owner November 25, 2022 06:28
Copy link
Copy Markdown
Contributor

@jughosta jughosta left a comment

Choose a reason for hiding this comment

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

LGTM 👍
Thanks for the refactoring! Left some suggestions which can be addressed later if relevant.


// state
private indexPattern = '';
private allowHidden = false;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would be great if allowHidden becomes enabled when editing a saved data view for hidden indices.

Steps:

  • Create a data view for hidden indices
  • Open it in Edit mode and see that it can't find matching indices unless the switch is toggled manually again.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also it does not restore a saved time field value:

Nov-28-2022 10-32-37

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'll address these in a follow up PR.

name: initialName = '',
},
requireTimestampField = false,
}: DataViewEditorServiceConstructorArgs) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A redundant error message is being briefly shown when submitting a valid form.

For example after editing a time field:
Nov-28-2022 10-27-33

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

addressed

@mattkime mattkime enabled auto-merge (squash) November 28, 2022 23:47
@mattkime mattkime merged commit 9e0da98 into elastic:main Nov 29, 2022
@kibana-ci
Copy link
Copy Markdown

💚 Build Succeeded

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
dataViewEditor 63 62 -1

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
dataViewEditor 34.8KB 35.5KB +769.0B
Unknown metric groups

ESLint disabled in files

id before after diff
osquery 1 2 +1

ESLint disabled line counts

id before after diff
enterpriseSearch 19 21 +2
fleet 59 65 +6
osquery 109 115 +6
securitySolution 442 448 +6
total +20

References to deprecated APIs

id before after diff
dataViewEditor 5 0 -5

Total ESLint disabled count

id before after diff
enterpriseSearch 20 22 +2
fleet 68 74 +6
osquery 110 117 +7
securitySolution 519 525 +6
total +21

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@kibanamachine
Copy link
Copy Markdown
Contributor

💔 All backports failed

Status Branch Result
8.6 Backport failed because of merge conflicts

Manual backport

To create the backport manually run:

node scripts/backport --pr 143604

Questions ?

Please refer to the Backport tool documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport:skip This PR does not require backporting Feature:Data Views Data Views code and UI - index patterns before 8.0 Feature:Kibana Management Feature label for Data Views, Advanced Setting, Saved Object management pages release_note:skip Skip the PR/issue when compiling release notes Team:DataDiscovery Discover, search (data plugin and KQL), data views, saved searches. For ES|QL, use Team:ES|QL. t// v8.7.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants