-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathRemodel.tsx
More file actions
147 lines (136 loc) · 4.54 KB
/
Remodel.tsx
File metadata and controls
147 lines (136 loc) · 4.54 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { useEffect, useState } from "react";
import { useAtomValue, useSetAtom } from "jotai";
import { useParams, Link, useNavigate, useLocation } from "react-router-dom";
import { Notification, Icon, Row, Col } from "@canonical/react-components";
import { useRemodels } from "../../hooks";
import { remodelsListState } from "../../state/remodelsState";
import { brandIdState, brandStoreState } from "../../state/brandStoreState";
import { setPageTitle, isClosedPanel } from "../../utils";
import { PortalEntrance } from "../Portals/Portals";
import RemodelTable from "./RemodelTable";
import ConfigureRemodelForm from "./ConfigureRemodelForm";
import type { UseQueryResult } from "react-query";
import type { Remodel, RemodelResponse, ApiResponse } from "../../types/shared";
function Remodel(): React.JSX.Element {
const { id, modelId } = useParams();
const location = useLocation();
const brandId = useAtomValue(brandIdState);
const {
isLoading,
isError,
error,
data,
refetch,
}: UseQueryResult<ApiResponse<RemodelResponse>, Error> = useRemodels(
brandId,
modelId,
);
const setRemodels = useSetAtom(remodelsListState);
const [showNotification, setShowNotification] = useState(false);
const [showErrorNotification, setShowErrorNotification] = useState(false);
const [errorMessage, setErrorMessage] = useState("");
const brandStore = useAtomValue(brandStoreState(id));
const navigate = useNavigate();
brandStore
? setPageTitle(`Remodels in ${brandStore.name}`)
: setPageTitle("Remodels");
useEffect(() => {
if (!isLoading && !isError && data) {
setRemodels(data.data?.allowlist || []);
}
}, [isLoading, isError, data, brandId, id]);
return (
<>
<div className="u-fixed-width u-flex-column u-flex-grow">
{isError && error && (
<Notification severity="negative">
Error: {error.message}
</Notification>
)}
{isLoading ? (
<p>
<Icon name="spinner" className="u-animation--spin" />
Fetching remodels...
</p>
) : data && data.success === false ? (
<Notification severity="caution">
{data.message || "Unable to fetch remodels"}
</Notification>
) : (
<>
<Row>
<Col size={12} className="u-align--right">
<Link
className={`p-button--positive ${isError && !data ? "is-disabled" : ""}`}
to={`/admin/${id}/models/${modelId}/remodel/configure`}
>
Configure remodels
</Link>
</Col>
</Row>
<div className="u-flex-column u-flex-grow">
<RemodelTable />
</div>
</>
)}
</div>
<PortalEntrance name="notification">
{showNotification && (
<div className="u-fixed-width">
<Notification
severity="positive"
onDismiss={() => {
setShowNotification(false);
}}
>
New remodel configured
</Notification>
</div>
)}
{showErrorNotification && (
<div className="u-fixed-width">
<Notification
severity="negative"
onDismiss={() => {
setShowErrorNotification(false);
}}
>
{errorMessage || "Unable to configure remodel"}
</Notification>
</div>
)}
</PortalEntrance>
<PortalEntrance name="aside">
<div
className={`l-aside__overlay ${
isClosedPanel(location.pathname, "configure") ? "u-hide" : ""
}`}
onClick={() => {
navigate(`/admin/${id}/models/${modelId}/remodel`);
}}
onKeyDown={(e) => {
if (e.key === "Enter" || e.key === " ") {
navigate(`/admin/${id}/models/${modelId}/remodel`);
}
}}
role="button"
tabIndex={0}
aria-label="Return to remodels"
></div>
<aside
className={`l-aside ${
isClosedPanel(location.pathname, "configure") ? "is-collapsed" : ""
}`}
>
<ConfigureRemodelForm
refetch={refetch}
setShowNotification={setShowNotification}
setShowErrorNotification={setShowErrorNotification}
setErrorMessage={setErrorMessage}
/>
</aside>
</PortalEntrance>
</>
);
}
export default Remodel;