-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore/sort_funcs_refactor #499
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1bb516a
useSortData refactor
EduardZaydler 7db5fb4
patternlist error handling
EduardZaydler c35364f
patternlist initial sort
EduardZaydler 87cac43
fix patternlist stories
EduardZaydler 2254a90
pattern list story
EduardZaydler 6929b8b
Merge branch 'master' into chore/universal_sorting_func
EduardZaydler ff4b282
pattern list props destructure
EduardZaydler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
{ | ||
"parser": "typescript", | ||
"printWidth": 100, | ||
"tabWidth": 4, | ||
"trailingComma": "es5", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,111 +1,61 @@ | ||
import * as React from "react"; | ||
import React, { useState, useEffect } from "react"; | ||
import MoiraApi from "../Api/MoiraApi"; | ||
import { Pattern } from "../Domain/Pattern"; | ||
import { SortingColumn } from "../Components/PatternList/PatternList"; | ||
import { withMoiraApi } from "../Api/MoiraApiInjection"; | ||
import PatternList from "../Components/PatternList/PatternList"; | ||
import { Layout, LayoutContent, LayoutTitle } from "../Components/Layout/Layout"; | ||
import { setDocumentTitle } from "../helpers/setDocumentTitle"; | ||
import { toggleLoading, setError } from "../store/Reducers/UIReducer.slice"; | ||
import { useAppDispatch, useAppSelector } from "../store/hooks"; | ||
import { UIState } from "../store/selectors"; | ||
import { useSortData } from "../hooks/useSortData"; | ||
|
||
type Props = { moiraApi: MoiraApi }; | ||
type State = { | ||
loading: boolean; | ||
error?: string; | ||
list?: Array<Pattern>; | ||
sortingColumn: SortingColumn; | ||
sortingDown: boolean; | ||
}; | ||
|
||
class PatternListContainer extends React.Component<Props, State> { | ||
public state: State = { | ||
sortingColumn: "trigger", | ||
sortingDown: false, | ||
loading: false, | ||
}; | ||
const PatternListContainer: React.FC<Props> = ({ moiraApi }) => { | ||
const dispatch = useAppDispatch(); | ||
const { isLoading, error } = useAppSelector(UIState); | ||
const [list, setList] = useState<Pattern[] | undefined>(); | ||
const { sortedData, sortConfig, handleSort } = useSortData(list ?? [], "metrics"); | ||
|
||
public componentDidMount() { | ||
useEffect(() => { | ||
setDocumentTitle("Patterns"); | ||
this.getData(this.props); | ||
} | ||
|
||
public UNSAFE_componentWillReceiveProps(nextProps: Props) { | ||
this.getData(nextProps); | ||
} | ||
getData(); | ||
}, []); | ||
|
||
public render(): React.ReactElement { | ||
const { loading, error, list, sortingColumn, sortingDown } = this.state; | ||
return ( | ||
<Layout loading={loading} error={error}> | ||
<LayoutContent> | ||
<LayoutTitle>Patterns</LayoutTitle> | ||
{list && ( | ||
<PatternList | ||
items={this.sortPatterns(list)} | ||
onSort={(sorting) => { | ||
if (sorting === sortingColumn) { | ||
this.setState({ sortingDown: !sortingDown }); | ||
} else { | ||
this.setState({ | ||
sortingColumn: sorting, | ||
sortingDown: true, | ||
}); | ||
} | ||
}} | ||
sortingColumn={sortingColumn} | ||
sortingDown={sortingDown} | ||
onRemove={this.removePattern} | ||
/> | ||
)} | ||
</LayoutContent> | ||
</Layout> | ||
); | ||
} | ||
|
||
private async getData(props: Props) { | ||
const getData = async () => { | ||
dispatch(toggleLoading(true)); | ||
try { | ||
const { list } = await props.moiraApi.getPatternList(); | ||
this.setState({ list }); | ||
const { list } = await moiraApi.getPatternList(); | ||
setList(list); | ||
} catch (error) { | ||
this.setState({ error: error.message }); | ||
dispatch(setError(error.message)); | ||
} finally { | ||
this.setState({ loading: false }); | ||
dispatch(toggleLoading(false)); | ||
} | ||
} | ||
}; | ||
|
||
private removePattern = async (pattern: string) => { | ||
this.setState({ loading: true }); | ||
await this.props.moiraApi.delPattern(pattern); | ||
this.getData(this.props); | ||
const removePattern = async (pattern: string) => { | ||
dispatch(toggleLoading(true)); | ||
await moiraApi.delPattern(pattern); | ||
getData(); | ||
}; | ||
|
||
private sortPatterns(patterns: Array<Pattern>): Array<Pattern> { | ||
const { sortingColumn, sortingDown } = this.state; | ||
const sorting = { | ||
trigger: (x: Pattern, y: Pattern) => { | ||
const valA = x.triggers.length || 0; | ||
const valB = y.triggers.length || 0; | ||
if (valA < valB) { | ||
return sortingDown ? -1 : 1; | ||
} | ||
if (valA > valB) { | ||
return sortingDown ? 1 : -1; | ||
} | ||
return 0; | ||
}, | ||
metric: (x: Pattern, y: Pattern) => { | ||
const valA = x.metrics.length || 0; | ||
const valB = y.metrics.length || 0; | ||
if (valA < valB) { | ||
return sortingDown ? -1 : 1; | ||
} | ||
if (valA > valB) { | ||
return sortingDown ? 1 : -1; | ||
} | ||
return 0; | ||
}, | ||
}; | ||
return patterns.slice(0).sort(sorting[sortingColumn]); | ||
} | ||
} | ||
return ( | ||
<Layout loading={isLoading} error={error}> | ||
<LayoutContent> | ||
<LayoutTitle>Patterns</LayoutTitle> | ||
{list && ( | ||
<PatternList | ||
items={sortedData} | ||
onSort={handleSort} | ||
sortConfig={sortConfig} | ||
onRemove={removePattern} | ||
/> | ||
)} | ||
</LayoutContent> | ||
</Layout> | ||
); | ||
}; | ||
|
||
export default withMoiraApi(PatternListContainer); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
поправил