Passed
Push — master ( 2a9974...92305b )
by Daniel
01:39 queued 11s
created

store/model/jobs.ts   A

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 155
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 89
mnd 2
bc 2
fnc 0
dl 0
loc 155
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
rs 10
1
/* eslint-disable no-param-reassign */
2
import { Action, action, thunk, Thunk } from "easy-peasy";
3
4
import { v4 as uuidv4 } from "uuid";
5
6
/**
7
 * Error type
8
 */
9
10
type TError = string;
11
12
/**
13
 * Interface for completeJob with rowIndex: number
14
 */
15
interface ICompleteJob {
16
  rowIndex: number;
17
}
18
19
/**
20
 * Type for employer
21
 */
22
type Employer = {
23
  name: string;
24
};
25
26
/**
27
 * Interface for jobModalItems
28
 */
29
interface IJobModalItem {
30
  uuid?: string;
31
  id: string;
32
  published?: Date;
33
  title: string;
34
  description: string;
35
  extent: string;
36
  name: string;
37
  applicationDue: string;
38
}
39
40
/**
41
 * Interface for jobItems
42
 */
43
interface IJobItem {
44
  uuid?: string;
45
  id: string;
46
  employer: Employer;
47
  published?: Date;
48
  title: string;
49
  description: string;
50
  extent: string;
51
  name: string;
52
  applicationDue: string;
53
}
54
55
interface IJobType {
56
  title: string;
57
  description: string;
58
  extent: string;
59
  name: string;
60
  applicationDue: string;
61
}
62
63
export interface IJobsModel {
64
  /**
65
   * Globally stored array of jobItems
66
   */
67
  jobItems: IJobItem[];
68
  /**
69
   * List of jobModalItems used in modal to show content
70
   */
71
  jobModalItems: IJobModalItem[];
72
73
  /**
74
   * Store error from API fetching in state
75
   */
76
  error: TError;
77
78
  /**
79
   * Save error to state
80
   */
81
  setError: Action<IJobsModel, TError>;
82
83
  /**
84
   * Action to store all jobs from API to state
85
   */
86
  saveFetchedJobs: Action<IJobsModel, IJobItem[]>;
87
88
  /**
89
   * Thunk to fetch all jobs from API
90
   */
91
  fetchRemoteJobs: Thunk<IJobsModel>;
92
93
  /**
94
   * Action to add a job to jobModalItems array
95
   */
96
  addJob: Action<IJobsModel, IJobType>;
97
98
  /**
99
   * Action to delete all jobs from jobModalItems array
100
   */
101
  deleteAllJobs: Action<IJobsModel>;
102
103
  /**
104
   * Action to delete a single job from jobModalItems array
105
   */
106
  deleteJob: Action<IJobsModel, ICompleteJob>;
107
}
108
109
const JobModel: IJobsModel = {
110
  jobItems: [],
111
  jobModalItems: [],
112
  error: "",
113
  setError: action((state, payload) => {
114
    state.error = payload;
115
  }),
116
  saveFetchedJobs: action((state, payload) => {
117
    state.jobItems = payload;
118
  }),
119
  fetchRemoteJobs: thunk(async (actions) => {
120
    try {
121
      // Fetch data from NextJS API route
122
      await fetch("/api/fetchNavJobs").then((result) =>
123
        result.json().then((data) => {
124
          actions.saveFetchedJobs(data);
125
        })
126
      );
127
    } catch (error) {
128
      if (error instanceof Error) {
129
        actions.setError(error.message);
130
      }
131
    }
132
  }),
133
  addJob: action(
134
    (state, { title, description, extent, name, applicationDue }) => {
135
      state.jobModalItems.push({
136
        id: uuidv4(),
137
        title,
138
        description,
139
        extent,
140
        name,
141
        applicationDue,
142
      });
143
    }
144
  ),
145
  // Should also be able to delete individual jobs and all saved jobs
146
  deleteJob: action((state, { rowIndex }) => {
147
    state.jobModalItems.splice(rowIndex, 1);
148
  }),
149
  deleteAllJobs: action((state) => {
150
    state.jobModalItems.length = 0;
151
  }),
152
};
153
154
export default JobModel;
155