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

components/JobListings/JobListings.tsx   A

Complexity

Total Complexity 4
Complexity/F 0

Size

Lines of Code 193
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 158
mnd 4
bc 4
fnc 0
dl 0
loc 193
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
rs 10
1
import React, { useState, useEffect } from "react";
2
3
import Pagination from "rc-pagination";
4
import { Button, Panel, Loader } 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 styles from "./JobListings.module.scss";
17
import "react-toastify/dist/ReactToastify.css";
18
import "rc-pagination/assets/index.css";
19
20
import { IModalContent } from "./JobListings.interface";
21
22
const JobListings: React.FC = () => {
23
  const [loading, setLoading] = useState<boolean>(true);
24
  const [modalItems, setModalItems] = useState<IModalContent[]>();
25
  const [pageNumber, setPageNumber] = useState<number>(1);
26
  const [modalIsOpen, setModalIsOpen] = useState<boolean>(false);
27
28
  const jobsPerPage = 10;
29
  const pagesVisited = pageNumber * jobsPerPage;
30
31
  const remoteError = useStoreState((state) => state.jobs.error);
32
  const jobItems = useStoreState((state) => state.jobs.jobItems);
33
  const jobModalItems = useStoreState((state) => state.jobs.jobModalItems);
34
  const addJob = useStoreActions((actions) => actions.jobs.addJob);
35
36
  const fetchRemoteJobs = useStoreActions(
37
    (actions) => actions.jobs.fetchRemoteJobs
38
  );
39
40
  const jobExistsToast = () => toast.error("Jobb er allerede lagret ...");
41
  const errorFetchingJobsToast = (errorMessage: string) =>
42
    toast.error(`Feil ved henting av ekstern data ${errorMessage}`);
43
  const jobExists = (search: string) =>
44
    jobModalItems.findIndex((value) => value.title === search);
45
46
  // Changes page to the current page (on click)
47
  const changePage = (page: number) => {
48
    setPageNumber(page);
49
  };
50
51
  const handleOpenModalClick = (
52
    description: string,
53
    extent: string,
54
    name: string,
55
    applicationDue: string
56
  ) => {
57
    setModalItems([
58
      {
59
        description,
60
        name,
61
        extent,
62
        applicationDue,
63
      },
64
    ]);
65
  };
66
67
  const closeModal = () => {
68
    setModalIsOpen(false);
69
  };
70
71
  useEffect(() => {
72
    if (remoteError.length > 0) {
73
      errorFetchingJobsToast(remoteError);
74
    }
75
  }, [remoteError]);
76
77
  useEffect(() => {
78
    if (modalItems && modalItems[0].name.length) {
79
      setModalIsOpen(true);
80
    }
81
  }, [modalItems]);
82
83
  useEffect(() => {
84
    fetchRemoteJobs();
85
  }, [fetchRemoteJobs]);
86
87
  useEffect(() => {
88
    if (jobItems.length) {
89
      Modal.setAppElement("#root");
90
      setLoading(false);
91
    } else {
92
      setLoading(true);
93
    }
94
  }, [jobItems]);
95
96
  return (
97
    <div>
98
      <SavedJobs handleOpenModalClick={handleOpenModalClick} />
99
      <ToastContainer position="top-center" />
100
      <div id="jobcontainer" className={styles.container}>
101
        <Modal
102
          isOpen={modalIsOpen}
103
          onRequestClose={closeModal}
104
          contentLabel="title"
105
        >
106
          {modalItems && (
107
            <JobModalContent
108
              name={modalItems[0].name}
109
              description={modalItems[0].description}
110
              extent={modalItems[0].extent}
111
              applicationDue={modalItems[0].applicationDue}
112
              closeModal={closeModal}
113
            />
114
          )}
115
        </Modal>
116
        {!loading &&
117
          jobItems
118
            .slice(pagesVisited, pagesVisited + jobsPerPage)
119
            .map(
120
              ({
121
                uuid,
122
                title,
123
                employer: { name },
124
                published,
125
                description,
126
                extent,
127
                applicationDue,
128
              }) => (
129
                <Panel key={uuid} className={styles.panel} border>
130
                  <span className={`${styles.panelSpan} ${styles.title}`}>
131
                    {title}
132
                  </span>
133
                  <span className={styles.panelSpan}>
134
                    {name.length && name}
135
                  </span>
136
                  <span className={styles.panelSpan}>
137
                    Publisert: {published && formatDate(published)}
138
                  </span>
139
                  <span className={styles.panelButton}>
140
                    <Button
141
                      className={styles.hovedKnapp}
142
                      onClick={() =>
143
                        handleOpenModalClick(
144
                          description,
145
                          extent,
146
                          name,
147
                          applicationDue
148
                        )
149
                      }
150
                    >
151
                      Vis
152
                    </Button>
153
                    <Button
154
                      className={styles.sekundKnapp}
155
                      onClick={() => {
156
                        // Check if we try to add an existing job, if yes, show error message
157
                        if (jobExists(title) === -1) {
158
                          addJob({
159
                            title,
160
                            description,
161
                            extent,
162
                            name,
163
                            applicationDue,
164
                          });
165
                        } else {
166
                          jobExistsToast();
167
                        }
168
                      }}
169
                    >
170
                      Lagre
171
                    </Button>
172
                  </span>
173
                </Panel>
174
              )
175
            )}
176
      </div>
177
      {!loading && (
178
        <Pagination
179
          className={styles.pagination}
180
          current={pageNumber}
181
          total={jobItems.length - 10}
182
          pageSize={jobsPerPage}
183
          locale={locale}
184
          onChange={(page) => changePage(page)}
185
        />
186
      )}
187
      {loading && !remoteError && <Loader className={styles.pagination} />}
188
    </div>
189
  );
190
};
191
192
export default JobListings;
193