-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #207 from jorgenbele/feat-incidents-use-filter-min…
…imal Add functionality for using predefined filters in incident filter toolbar
- Loading branch information
Showing
10 changed files
with
257 additions
and
35 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React, { createContext, useReducer } from "react"; | ||
|
||
import { Filter } from "./api"; | ||
import { filterReducer, FilterActions } from "./reducers/filter"; | ||
|
||
export type InitialStateType = { | ||
// List of all filters that the currently | ||
// logged in user has access to. | ||
filters: Filter[]; | ||
}; | ||
|
||
const initialState: InitialStateType = { | ||
filters: [], | ||
}; | ||
|
||
type ActionsType = FilterActions /* | OtherAction | AnotherActoin ... */; | ||
|
||
const AppContext = createContext<{ | ||
state: InitialStateType; | ||
dispatch: React.Dispatch<ActionsType>; | ||
}>({ | ||
state: initialState, | ||
dispatch: () => null, | ||
}); | ||
|
||
const mainReducer = ({ filters }: InitialStateType, action: ActionsType) => ({ | ||
filters: filterReducer(filters, action), | ||
}); | ||
|
||
const AppProvider: React.FC = ({ children }: { children?: React.ReactNode }) => { | ||
const [state, dispatch] = useReducer(mainReducer, initialState); | ||
|
||
return <AppContext.Provider value={{ state, dispatch }}>{children}</AppContext.Provider>; | ||
}; | ||
|
||
export { AppContext, AppProvider }; |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export type ActionMap<M extends { [index: string]: any }> = { | ||
[Key in keyof M]: M[Key] extends undefined | ||
? { | ||
type: Key; | ||
} | ||
: { | ||
type: Key; | ||
payload: M[Key]; | ||
}; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { Filter } from "../api"; | ||
|
||
import { ActionMap } from "./common"; | ||
|
||
export enum FilterType { | ||
Create = "CREATE_FILTER", | ||
Delete = "DELETE_FILTER", | ||
Modify = "MODIFY_FILTERS", | ||
LoadAll = "LOAD_FILTERS", | ||
} | ||
|
||
type FilterPayload = { | ||
// Used to override all local modifications with | ||
// data from backend | ||
[FilterType.LoadAll]: Filter[]; | ||
|
||
// When a filter is modified. | ||
[FilterType.Modify]: Filter; | ||
|
||
// When a new filter is created. | ||
[FilterType.Create]: Filter; | ||
|
||
// When a filter is deleted | ||
[FilterType.Delete]: Filter["pk"]; | ||
}; | ||
|
||
export type FilterActions = ActionMap<FilterPayload>[keyof ActionMap<FilterPayload>]; | ||
export const filterReducer = (state: Filter[], action: FilterActions) => { | ||
switch (action.type) { | ||
case FilterType.LoadAll: | ||
return action.payload; | ||
case FilterType.Modify: { | ||
const index = state.findIndex((f: Filter) => f.pk === action.payload.pk); | ||
const updated = [...state]; | ||
updated[index] = { ...updated[index], ...action.payload }; | ||
return updated; | ||
} | ||
case FilterType.Create: | ||
return [...state, action.payload]; | ||
case FilterType.Delete: | ||
return [...state.filter((filter: Filter) => filter.pk !== action.payload)]; | ||
} | ||
}; | ||
|
||
type Action<T, P> = (payload: P) => { type: T; payload: P }; | ||
function makeAction<T, P>(type: T): Action<T, P> { | ||
return (payload: P): { type: T; payload: P } => { | ||
return { type, payload }; | ||
}; | ||
} | ||
|
||
export const loadAllFilters = makeAction<FilterType.LoadAll, Filter[]>(FilterType.LoadAll); | ||
export const modifyFilter = makeAction<FilterType.Modify, Filter>(FilterType.Modify); | ||
export const createFilter = makeAction<FilterType.Create, Filter>(FilterType.Create); | ||
export const deleteFilter = makeAction<FilterType.Delete, Filter["pk"]>(FilterType.Delete); |
Oops, something went wrong.