Passed
Push — feature/azure-webapp-pipeline-... ( f7c88d...cc7783 )
by Grant
05:46 queued 13s
created

resources/assets/js/store/Classification/classificationSelector.ts   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 50
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 39
c 0
b 0
f 0
dl 0
loc 50
rs 10
mnd 1
bc 1
fnc 0
bpm 0
cpm 0
noi 0
1
import { createSelector } from "reselect";
2
import { RootState } from "../store";
3
import { Classification } from "../../models/types";
4
5
// I still have no idea
6
const getClassificationState = (state: RootState): { [key: string]: Classification } =>
7
 state.classification.byId;
8
9
 // Get the list of classifications already stored in the state (via the function in classificationactions)
10
export const getClassifications = createSelector(
11
  getClassificationState,
12
  (ClassificationState): Classification[] => Object.values(ClassificationState),
13
);
14
15
export const classificationsExtractKeyValueJsonArray = function(classifications : Classification[]) : {value : number, label: string}[] {
16
    let classificationsArr : {value : number, label: string}[] = [];
17
    classifications.forEach(function(classification) {
18
      classificationsArr.push({value : classification.id, label : classification.key
19
      })
20
    })
21
    return classificationsArr
22
}
23
24
export const classificationsExtractKeyValueJson = function(classifications) : JSON {
25
  let classificationsJson : any = {}
26
  let output : JSON;
27
28
  classifications.forEach(function (classification) {
29
    classificationsJson[classification.key] = classification.id
30
  })
31
32
  output = <JSON>classificationsJson
33
  return output
34
}
35
36
export const getClassificationKey = function(classifications : Classification[], classification: number | string): string {
37
  let lClassification : Classification = classifications.filter(c => c.id == classification)[0]
38
  return lClassification ? lClassification.key : ""
39
}
40
41
// Get a classification from the redux state by passing in an ID
42
export const getClassificationById = (
43
  state: RootState,
44
  id: number,
45
): Classification  =>
46
  getClassificationState(state)[id]
47
48
export const classificationsIsLoading = (state: RootState): boolean =>
49
  state.classification.loading;
50