/
Home.tsx
177 lines (165 loc) · 4.38 KB
/
Home.tsx
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import React, { useState } from "react";
import {
IonPage,
IonButtons,
IonButton,
IonHeader,
IonToolbar,
IonTitle,
IonContent,
IonLoading,
IonList,
IonModal,
IonAlert,
} from "@ionic/react";
import { useAuth, AuthCheck } from "reactfire";
import "firebase/firestore";
import AddSomethingModal, {
IModalResponse,
IModalData,
} from "../components/AddSomethingModal";
import { useHistory } from "react-router";
import { useDataProvider } from "../DataContext";
import Line from "./Line";
type IShowAlert = null | {
header: string;
subHeader: string;
message: string;
};
const Home: React.FunctionComponent = () => {
// reactfire hook to get auth information
const auth = useAuth();
const history = useHistory();
const { addItem, removeItem, dataCollection, updateItem } = useDataProvider();
// manages the state to determine if we need to open
// the modal or not
const [showModal, setShowModal] = useState<{
show: boolean;
initialData?: IModalData;
}>({ show: false });
// manages the state to determine if we need to open
// the modal or not
const [showErrorAlert, setShowErrorAlert] = useState<IShowAlert>(null);
/**
* call this function to set state to get the alert
* to display
*
* @param message
* @param isError
*/
const showAlert = (message: string, isError: boolean = false) => {
setShowErrorAlert({
header: "App Alert",
subHeader: isError ? "Error" : "Notification",
message: message,
});
};
/**
* @param item
*/
const editSomething = (item: IModalData) => {
setShowModal({ show: true, initialData: item });
};
/**
*
* @param item IModalData
*/
const removeSomething = (item: IModalData) => {
removeItem(item)
.then(() => showAlert("Success"))
.catch((error: any) => {
showAlert(error.message, true);
});
};
/**
*
* @param response IModalResponse
*/
const addSomething = async (response: IModalResponse) => {
setShowModal({ show: false });
if (!response.hasData) {
showAlert("User Cancelled", true);
return;
} else {
try {
if (response.data?.id) {
await updateItem(response.data!);
} else {
await addItem(response.data!);
}
showAlert("Success");
} catch (error) {
showAlert(error.message, true);
}
}
};
return (
<IonPage>
<IonHeader>
<IonToolbar color="light">
<IonButtons slot="end">
<IonButton
onClick={() => {
auth.signOut();
history.replace("/login");
}}
>
Logout
</IonButton>
</IonButtons>
<IonTitle>Home</IonTitle>
</IonToolbar>
</IonHeader>
<IonToolbar style={{ paddingLeft: 16, paddingRight: 16 }}>
<IonButton
title="Add Something"
fill="outline"
onClick={() => setShowModal({ show: true })}
>
Add Something
</IonButton>
</IonToolbar>
<IonContent className="ion-padding">
{/** Show Error when problem **/}
<IonAlert
isOpen={showErrorAlert !== null}
onDidDismiss={() => setShowErrorAlert(null)}
header={showErrorAlert?.header}
subHeader={showErrorAlert?.subHeader}
message={showErrorAlert?.message}
buttons={["OK"]}
/>
{/* ionic modal component */}
{showModal.show && (
<IonModal
isOpen={showModal.show}
onDidDismiss={() => setShowModal({ show: false })}
>
{/* our custom modal content */}
<AddSomethingModal
initialData={showModal.initialData}
onCloseModal={(data: IModalResponse) => addSomething(data)}
/>
</IonModal>
)}
{/* auth check and loader while in progress */}
<AuthCheck fallback={<IonLoading isOpen={true} />}>
{/* list of items from reactfire */}
<IonList>
{dataCollection.map((e: any) => {
return (
<Line
item={e}
key={e.id}
edit={editSomething}
remove={removeSomething}
/>
);
})}
</IonList>
</AuthCheck>
</IonContent>
</IonPage>
);
};
export default React.memo(Home);