Passed
Push — feature/micro-ref-email ( b987f2...6f6312 )
by Tristan
04:47
created

resources/assets/js/store/Application/applicationSelector.ts   A

Complexity

Total Complexity 3
Complexity/F 0

Size

Lines of Code 65
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 57
mnd 3
bc 3
fnc 0
dl 0
loc 65
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
/* eslint camelcase: "off", @typescript-eslint/camelcase: "off" */
2
import createCachedSelector from "re-reselect";
3
import { RootState } from "../store";
4
import { EntityState, UiState } from "./applicationReducer";
5
import { Application } from "../../models/types";
6
import { PropType } from "../../models/app";
7
import { hasKey, getId, notEmpty } from "../../helpers/queries";
8
9
const entities = (state: RootState): EntityState => state.applications.entities;
10
// eslint-disable-next-line
11
const ui = (state: RootState): UiState => state.applications.ui;
12
13
const getApplicationState = (state: RootState) => entities(state).applications;
14
15
const getApplicationReviewState = (state: RootState) =>
16
  entities(state).applicationReviews;
17
18
const constructNonNormalizedApplication = (
19
  applications: ReturnType<typeof getApplicationState>,
20
  applicationReviews: PropType<EntityState, "applicationReviews">,
21
  id: number,
22
): Application | null => {
23
  const applicationNormalized = hasKey(applications, id)
24
    ? applications[id]
25
    : null;
26
  if (applicationNormalized === null) {
27
    return null;
28
  }
29
  if (hasKey(applicationReviews.idByApplicationId, id)) {
30
    const reviewId = applicationReviews.idByApplicationId[id];
31
    return {
32
      ...applicationNormalized,
33
      application_review: applicationReviews.byId[reviewId],
34
    };
35
  }
36
  return {
37
    ...applicationNormalized,
38
    application_review: undefined,
39
  };
40
};
41
42
export const getApplicationById = createCachedSelector(
43
  getApplicationState,
44
  getApplicationReviewState,
45
  (state: RootState, ownProps: { id: number }): number => ownProps.id,
46
  constructNonNormalizedApplication,
47
)((state, ownProps): number => ownProps.id);
48
49
export const getApplicationsByJob = createCachedSelector(
50
  getApplicationState,
51
  getApplicationReviewState,
52
  (state: RootState, ownProps: { jobId: number }): number => ownProps.jobId,
53
  (applications, applicationReviews, jobId): Application[] => {
54
    const applicationIds = Object.values(applications)
55
      .filter((application) => application.job_poster_id === jobId)
56
      .map(getId);
57
    console.log(applicationIds);
58
    return applicationIds
59
      .map((id) =>
60
        constructNonNormalizedApplication(applications, applicationReviews, id),
61
      )
62
      .filter(notEmpty);
63
  },
64
)((state, ownProps): number => ownProps.jobId);
65