1
|
|
|
/* eslint-disable no-param-reassign */ |
2
|
|
|
import { |
3
|
|
|
Action, action, thunk, Thunk, |
4
|
|
|
} from 'easy-peasy'; |
5
|
|
|
|
6
|
|
|
import { v4 as uuidv4 } from 'uuid'; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Error type |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
type TError = string; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Interface for completeJob with rowIndex: number |
16
|
|
|
*/ |
17
|
|
|
interface ICompleteJob { |
18
|
|
|
rowIndex: number; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Type for employer |
23
|
|
|
*/ |
24
|
|
|
type Employer = { |
25
|
|
|
name: string; |
26
|
|
|
}; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Interface for jobModalItems |
30
|
|
|
*/ |
31
|
|
|
interface IJobModalItem { |
32
|
|
|
uuid?: string; |
33
|
|
|
id: string; |
34
|
|
|
published?: Date; |
35
|
|
|
title: string; |
36
|
|
|
description: string; |
37
|
|
|
extent: string; |
38
|
|
|
name: string; |
39
|
|
|
applicationDue: string; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Interface for jobItems |
44
|
|
|
*/ |
45
|
|
|
interface IJobItem { |
46
|
|
|
uuid?: string; |
47
|
|
|
id: string; |
48
|
|
|
employer: Employer; |
49
|
|
|
published?: Date; |
50
|
|
|
title: string; |
51
|
|
|
description: string; |
52
|
|
|
extent: string; |
53
|
|
|
name: string; |
54
|
|
|
applicationDue: string; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
interface IJobType { |
58
|
|
|
title: string; |
59
|
|
|
description: string; |
60
|
|
|
extent: string; |
61
|
|
|
name: string; |
62
|
|
|
applicationDue: string; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
export interface IJobsModel { |
66
|
|
|
/** |
67
|
|
|
* Globally stored array of jobItems |
68
|
|
|
*/ |
69
|
|
|
jobItems: IJobItem[]; |
70
|
|
|
/** |
71
|
|
|
* List of jobModalItems used in modal to show content |
72
|
|
|
*/ |
73
|
|
|
jobModalItems: IJobModalItem[]; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Store error from API fetching in state |
77
|
|
|
*/ |
78
|
|
|
error: TError; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Save error to state |
82
|
|
|
*/ |
83
|
|
|
setError: Action<IJobsModel, TError>; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Action to store all jobs from API to state |
87
|
|
|
*/ |
88
|
|
|
saveFetchedJobs: Action<IJobsModel, IJobItem[]>; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Thunk to fetch all jobs from API |
92
|
|
|
*/ |
93
|
|
|
fetchRemoteJobs: Thunk<IJobsModel>; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Action to add a job to jobModalItems array |
97
|
|
|
*/ |
98
|
|
|
addJob: Action<IJobsModel, IJobType>; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Action to delete all jobs from jobModalItems array |
102
|
|
|
*/ |
103
|
|
|
deleteAllJobs: Action<IJobsModel>; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Action to delete a single todo from jobModalItems array |
107
|
|
|
*/ |
108
|
|
|
deleteJob: Action<IJobsModel, ICompleteJob>; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
const JobModel: IJobsModel = { |
112
|
|
|
jobItems: [], |
113
|
|
|
jobModalItems: [], |
114
|
|
|
error: '', |
115
|
|
|
setError: action((state, payload) => { |
116
|
|
|
state.error = payload; |
117
|
|
|
}), |
118
|
|
|
saveFetchedJobs: action((state, payload) => { |
119
|
|
|
state.jobItems = payload; |
120
|
|
|
}), |
121
|
|
|
fetchRemoteJobs: thunk(async (actions) => { |
122
|
|
|
try { |
123
|
|
|
// TODO This could be replaced with Axios if wanted |
124
|
|
|
await fetch( |
125
|
|
|
'https://arbeidsplassen.nav.no/public-feed/api/v1/ads?size=100&page=1', |
126
|
|
|
// 'https://arbeidsplassenx.navtet.no/public-feed/api/v1/ads?size=100&page=1', // <- Trigger error handler with this |
127
|
|
|
{ |
128
|
|
|
method: 'GET', |
129
|
|
|
headers: { |
130
|
|
|
Authorization: `Bearer ${process.env.REACT_APP_AUTH}`, |
131
|
|
|
}, |
132
|
|
|
}, |
133
|
|
|
).then((result) => result.json().then((data) => { |
134
|
|
|
actions.saveFetchedJobs(data.content); |
135
|
|
|
})); |
136
|
|
|
} catch (error) { |
137
|
|
|
actions.setError(error.message); |
138
|
|
|
} |
139
|
|
|
}), |
140
|
|
|
addJob: action( |
141
|
|
|
(state, { |
142
|
|
|
title, description, extent, name, applicationDue, |
143
|
|
|
}) => { |
144
|
|
|
state.jobModalItems.push({ |
145
|
|
|
id: uuidv4(), |
146
|
|
|
title, |
147
|
|
|
description, |
148
|
|
|
extent, |
149
|
|
|
name, |
150
|
|
|
applicationDue, |
151
|
|
|
}); |
152
|
|
|
}, |
153
|
|
|
), |
154
|
|
|
// TODO Delete individual jobs and all saved jobs |
155
|
|
|
deleteJob: action((state, { rowIndex }) => { |
156
|
|
|
state.jobModalItems.splice(rowIndex, 1); |
157
|
|
|
}), |
158
|
|
|
deleteAllJobs: action((state) => { |
159
|
|
|
state.jobModalItems.length = 0; |
160
|
|
|
}), |
161
|
|
|
}; |
162
|
|
|
|
163
|
|
|
export default JobModel; |
164
|
|
|
|