1
|
|
|
import React, { useState, useEffect } from "react" |
2
|
|
|
|
3
|
|
|
import Pagination from "rc-pagination" |
4
|
|
|
import { Button, Panel } from "@navikt/ds-react" |
5
|
|
|
|
6
|
|
|
import Modal from "react-modal" |
7
|
|
|
import { ToastContainer, toast } from "react-toastify" |
8
|
|
|
|
9
|
|
|
import JobModalContent from "../JobModalContent/JobModalContent" |
10
|
|
|
import SavedJobs from "../SavedJobs/SavedJobs" |
11
|
|
|
|
12
|
|
|
import { formatDate } from "@/assets/utils/functions" |
13
|
|
|
import { useStoreActions, useStoreState } from "@/assets/utils/hooks" |
14
|
|
|
import locale from "../../assets/locale/localenb_NO" |
15
|
|
|
|
16
|
|
|
import style from "./JobListings.module.scss" |
17
|
|
|
import "react-toastify/dist/ReactToastify.css" |
18
|
|
|
import "rc-pagination/assets/index.css" |
19
|
|
|
|
20
|
|
|
import { IJobRootObject, IModalContent } from "./JobListings.interface" |
21
|
|
|
|
22
|
|
|
interface IJobListingsProps { |
23
|
|
|
jobItems: IJobRootObject[] |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
const JobListings = ({ jobItems }: IJobListingsProps) => { |
27
|
|
|
const [modalItems, setModalItems] = useState<IModalContent[]>() |
28
|
|
|
const [pageNumber, setPageNumber] = useState<number>(1) |
29
|
|
|
const [modalIsOpen, setModalIsOpen] = useState<boolean>(false) |
30
|
|
|
|
31
|
|
|
const jobsPerPage = 10 |
32
|
|
|
const pagesVisited = pageNumber * jobsPerPage |
33
|
|
|
|
34
|
|
|
const remoteError = useStoreState((state) => state.jobs.error) |
35
|
|
|
|
36
|
|
|
const jobModalItems = useStoreState((state) => state.jobs.jobModalItems) |
37
|
|
|
const addJob = useStoreActions((actions) => actions.jobs.addJob) |
38
|
|
|
|
39
|
|
|
const jobExistsToast = () => toast.error("Jobb er allerede lagret ...") |
40
|
|
|
const errorFetchingJobsToast = (errorMessage: string) => |
41
|
|
|
toast.error(`Feil ved henting av ekstern data ${errorMessage}`) |
42
|
|
|
const jobExists = (search: string) => jobModalItems.findIndex((value) => value.title === search) |
43
|
|
|
|
44
|
|
|
// Changes page to the current page (on click) |
45
|
|
|
const changePage = (page: number) => { |
46
|
|
|
setPageNumber(page) |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
const handleOpenModalClick = (description: string, extent: string, name: string, applicationDue: string) => { |
50
|
|
|
setModalItems([ |
51
|
|
|
{ |
52
|
|
|
description, |
53
|
|
|
name, |
54
|
|
|
extent, |
55
|
|
|
applicationDue, |
56
|
|
|
}, |
57
|
|
|
]) |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
const closeModal = () => { |
61
|
|
|
setModalIsOpen(false) |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
useEffect(() => { |
65
|
|
|
if (remoteError.length > 0) { |
66
|
|
|
errorFetchingJobsToast(remoteError) |
67
|
|
|
} |
68
|
|
|
}, [remoteError]) |
69
|
|
|
|
70
|
|
|
useEffect(() => { |
71
|
|
|
if (modalItems && modalItems[0].name.length) { |
72
|
|
|
setModalIsOpen(true) |
73
|
|
|
} |
74
|
|
|
}, [modalItems]) |
75
|
|
|
|
76
|
|
|
useEffect(() => { |
77
|
|
|
if (jobItems.length) { |
78
|
|
|
Modal.setAppElement("#root") |
79
|
|
|
} |
80
|
|
|
}, [jobItems]) |
81
|
|
|
|
82
|
|
|
return ( |
83
|
|
|
<div> |
84
|
|
|
<SavedJobs handleOpenModalClick={handleOpenModalClick} /> |
85
|
|
|
<ToastContainer position="top-center" /> |
86
|
|
|
<div id="jobcontainer" className={style.container}> |
87
|
|
|
<Modal isOpen={modalIsOpen} onRequestClose={closeModal} contentLabel="title"> |
88
|
|
|
{modalItems && ( |
89
|
|
|
<JobModalContent |
90
|
|
|
name={modalItems[0].name} |
91
|
|
|
description={modalItems[0].description} |
92
|
|
|
extent={modalItems[0].extent} |
93
|
|
|
applicationDue={modalItems[0].applicationDue} |
94
|
|
|
closeModal={closeModal} |
95
|
|
|
/> |
96
|
|
|
)} |
97
|
|
|
</Modal> |
98
|
|
|
{jobItems |
99
|
|
|
.slice(pagesVisited, pagesVisited + jobsPerPage) |
100
|
|
|
.map(({ uuid, title, employer: { name }, published, description, extent, applicationDue }) => ( |
101
|
|
|
<Panel key={uuid} className={style.panel} border> |
102
|
|
|
<span className={`${style["panel-span"]} ${style.title}`}>{title}</span> |
103
|
|
|
<span className={style["panel-span"]}>{Boolean(name.length) && name}</span> |
104
|
|
|
<span className={style["panel-span"]}> |
105
|
|
|
{`Publisert: ${published ? formatDate(published) : ""}`} |
106
|
|
|
</span> |
107
|
|
|
<span className={style["panel-button"]}> |
108
|
|
|
<Button |
109
|
|
|
className={style["hoved-knapp"]} |
110
|
|
|
onClick={() => handleOpenModalClick(description, extent, name, applicationDue)} |
111
|
|
|
> |
112
|
|
|
Vis |
113
|
|
|
</Button> |
114
|
|
|
<Button |
115
|
|
|
variant="secondary" |
116
|
|
|
className={style["sekund-knapp"]} |
117
|
|
|
onClick={() => { |
118
|
|
|
// Check if we try to add an existing job, if yes, show error message |
119
|
|
|
if (jobExists(title) === -1) { |
120
|
|
|
addJob({ |
121
|
|
|
title, |
122
|
|
|
description, |
123
|
|
|
extent, |
124
|
|
|
name, |
125
|
|
|
applicationDue, |
126
|
|
|
}) |
127
|
|
|
} else { |
128
|
|
|
jobExistsToast() |
129
|
|
|
} |
130
|
|
|
}} |
131
|
|
|
> |
132
|
|
|
Lagre |
133
|
|
|
</Button> |
134
|
|
|
</span> |
135
|
|
|
</Panel> |
136
|
|
|
))} |
137
|
|
|
</div> |
138
|
|
|
|
139
|
|
|
<Pagination |
140
|
|
|
className={style.pagination} |
141
|
|
|
current={pageNumber} |
142
|
|
|
total={jobItems.length - 10} |
143
|
|
|
pageSize={jobsPerPage} |
144
|
|
|
locale={locale} |
145
|
|
|
onChange={(page: number) => changePage(page)} |
146
|
|
|
/> |
147
|
|
|
</div> |
148
|
|
|
) |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
export default JobListings |
152
|
|
|
|