Passed
Push — master ( 95cc94...6fe538 )
by Daniel
01:55 queued 12s
created

src/components/JobListings/JobListings.tsx   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 175
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 139
mnd 1
bc 1
fnc 0
dl 0
loc 175
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
rs 10
1
import React, { useState, useEffect } from 'react';
2
import Pagination from 'paginering';
3
import Panel from 'nav-frontend-paneler';
4
import NavFrontendSpinner from 'nav-frontend-spinner';
5
import { Hovedknapp, Knapp } from 'nav-frontend-knapper';
6
import Modal from 'react-modal';
7
8
import JobModalContent from '../JobModalContent/JobModalContent';
9
import SavedJobs from '../SavedJobs/SavedJobs';
10
11
import { formatDate } from '../../assets/utils/functions';
12
import { useStoreActions } from '../../assets/utils/hooks';
13
14
import styles from './JobListings.module.scss';
15
16
import { IModalContent } from './JobListings.interface';
17
18
const JobListings: React.FC = () => {
19
  const [items, setItems] = useState([]);
20
  const [loading, setLoading] = useState<boolean>(true);
21
  const [error, setError] = useState<boolean>(false);
22
  const [modalItems, setModalItems] = useState<IModalContent[]>();
23
  const [pageNumber, setPageNumber] = useState<number>(1);
24
  const [modalIsOpen, setModalIsOpen] = useState<boolean>(false);
25
26
  const jobsPerPage = 10;
27
  const pagesVisited = pageNumber * jobsPerPage;
28
29
  const addJob = useStoreActions((actions) => actions.jobs.addJob);
30
31
  // Changes page to the current page (on click)
32
  const changePage = (page: number) => {
33
    setPageNumber(page);
34
  };
35
36
  const handleOpenModalClick = (
37
    description: string,
38
    extent: string,
39
    name: string,
40
    applicationDue: string,
41
  ) => {
42
    setModalItems([
43
      {
44
        description,
45
        name,
46
        extent,
47
        applicationDue,
48
      },
49
    ]);
50
  };
51
52
  const closeModal = () => {
53
    setModalIsOpen(false);
54
  };
55
56
  useEffect(() => {
57
    if (modalItems && modalItems[0].name.length) {
58
      setModalIsOpen(true);
59
    }
60
  }, [modalItems]);
61
62
  useEffect(() => {
63
    fetch(
64
      'https://arbeidsplassen.nav.no/public-feed/api/v1/ads?size=100&page=1',
65
      {
66
        method: 'GET',
67
        headers: {
68
          Authorization: `Bearer ${process.env.REACT_APP_AUTH}`,
69
        },
70
      },
71
    )
72
      .then((result) => result.json().then((data) => {
73
        setItems(data.content);
74
        Modal.setAppElement('#root');
75
        setLoading(false);
76
      }))
77
      .catch(() => {
78
        setError(true);
79
      });
80
  }, []);
81
82
  return (
83
    <div>
84
      <SavedJobs handleOpenModalClick={handleOpenModalClick} />
85
      {error && (
86
        <span className="errorMessage">
87
          Feil under lasting av annonser, prøv igjen senere.
88
        </span>
89
      )}
90
      <div id="jobcontainer" className={styles.container}>
91
        <Modal
92
          isOpen={modalIsOpen}
93
          onRequestClose={closeModal}
94
          contentLabel="title"
95
        >
96
          {modalItems && (
97
            <JobModalContent
98
              name={modalItems[0].name}
99
              description={modalItems[0].description}
100
              extent={modalItems[0].extent}
101
              applicationDue={modalItems[0].applicationDue}
102
              closeModal={closeModal}
103
            />
104
          )}
105
        </Modal>
106
        {!loading
107
          && items
108
            .slice(pagesVisited, pagesVisited + jobsPerPage)
109
            .map(
110
              ({
111
                uuid,
112
                title,
113
                employer: { name },
114
                published,
115
                description,
116
                extent,
117
                applicationDue,
118
              }) => (
119
                <Panel key={uuid} className={styles.panel} border>
120
                  <span className={`${styles.panelSpan} ${styles.title}`}>
121
                    {title}
122
                  </span>
123
                  <span className={styles.panelSpan}>{name}</span>
124
                  <span className={styles.panelSpan}>
125
                    Publisert:
126
                    {' '}
127
                    {formatDate(published)}
128
                  </span>
129
                  <span className={styles.panelButton}>
130
                    <Hovedknapp
131
                      className={styles.hovedKnapp}
132
                      onClick={() => handleOpenModalClick(
133
                        description,
134
                        extent,
135
                        name,
136
                        applicationDue,
137
                      )}
138
                    >
139
                      Vis
140
                    </Hovedknapp>
141
                    <Knapp
142
                      className={styles.sekundKnapp}
143
                      onClick={() => addJob({
144
                        title,
145
                        description,
146
                        extent,
147
                        name,
148
                        applicationDue,
149
                      })}
150
                    >
151
                      Lagre
152
                    </Knapp>
153
                  </span>
154
                </Panel>
155
              ),
156
            )}
157
      </div>
158
      {!loading && (
159
        <Pagination
160
          className={styles.pagination}
161
          currentPage={1}
162
          numberOfItems={items.length}
163
          itemsPerPage={jobsPerPage}
164
          onChange={(page) => changePage(page)}
165
        />
166
      )}
167
      {loading && !error && (
168
        <NavFrontendSpinner className={styles.pagination} />
169
      )}
170
    </div>
171
  );
172
};
173
174
export default JobListings;
175