This repository has been archived by the owner on Aug 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ScooterRows.js
82 lines (76 loc) · 1.97 KB
/
ScooterRows.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import React from 'react';
import { Link } from 'react-router-dom';
import { useQuery } from '@apollo/react-hooks';
import { gql } from 'apollo-boost';
import GetCategoryById from '../categories/GetCategoryById';
function ScooterRows() {
// Definér først query til at hente array med alle elscootere
const GET_SCOOTERS = gql`
{
getScooters {
_id
itemNo
name
price
priceVAT
categoryId
}
}
`;
// Anvend query
const { loading, error, data } = useQuery(GET_SCOOTERS);
// Tjekker feljbeskeden og fjerner 'GraphQL error' fra strengen.
const errorHandler = () => {
if (error.message === 'GraphQL error: Ingen elscootere fundet!') {
return error.message.slice(15);
} else {
return 'Error!';
}
};
if (loading)
return (
<tr>
<td>Loading...</td>
</tr>
);
if (error)
return (
<tr>
<td>{errorHandler()}</td>
</tr>
);
// Returnér nu alle props for hvert enkel scooter som en tabel-række
return data.getScooters.map((scooter, index) => {
const { _id, itemNo, name, price, priceVAT, categoryId } = scooter; // Destructuring
return (
<tr key={_id} className="tableRowStyles">
<td>
<Link to={`/editScooter/${_id}`} className="linkStyles">
{itemNo}
</Link>
</td>
<td>
<Link to={`/editScooter/${_id}`} className="linkStyles">
{name}
</Link>
</td>
<td>
<Link to={`/editScooter/${_id}`} className="linkStyles">
{price} DKK
</Link>
</td>
<td>
<Link to={`/editScooter/${_id}`} className="linkStyles">
{priceVAT} DKK
</Link>
</td>
<td>
<Link to={`/editScooter/${_id}`} className="linkStyles">
<GetCategoryById categoryId={categoryId} />
</Link>
</td>
</tr>
);
});
}
export default ScooterRows;