Advertisement
raffaelegriecoit

Untitled

Aug 28th, 2024
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.75 KB | None | 0 0
  1. import { useEffect, useState, useCallback } from 'react';
  2. import userServices from '../../services/userServices';
  3. import bookingServices from '../../services/bookingServices';
  4. import authServices from '../../services/authServices';
  5. import { Link, useNavigate } from 'react-router-dom';
  6. import CountdownTimer from '../../components/CountdownTimer';
  7. import { format, parse } from 'date-fns';
  8. import MetaTags from '../../components/MetaTags';
  9.  
  10. const MyAccount = () => {
  11. const { getAllUsers, deleteUserById } = userServices;
  12. const { logoutUser } = authServices;
  13. const { getFilteredBookings } = bookingServices;
  14. const [user, setUser] = useState(null);
  15. const [userBookings, setUserBookings] = useState([]);
  16. const [isAdmin, setIsAdmin] = useState(false);
  17. const navigate = useNavigate();
  18. const [isLoading, setIsLoading] = useState(false);
  19. const [error, setError] = useState(null);
  20. const [confirmDelete, setConfirmDelete] = useState(false);
  21. const [userToDelete, setUserToDelete] = useState(null);
  22.  
  23. const fetchUserData = useCallback(async () => {
  24. setError(null);
  25. setIsLoading(true);
  26. try {
  27. const response = await getAllUsers();
  28. const fetchedUser = response[0];
  29. setUser(fetchedUser);
  30. if (fetchedUser?.type === 'ADMIN') {
  31. setIsAdmin(true);
  32. }
  33. } catch (error) {
  34. setError(error);
  35. } finally {
  36. setIsLoading(false);
  37. }
  38. }, []);
  39.  
  40. const fetchUserBookings = useCallback(async () => {
  41. if (!user) return;
  42. setError(null);
  43. setIsLoading(true);
  44. try {
  45. const response = await getFilteredBookings(`user=${user.id}`);
  46. setUserBookings(response);
  47. } catch (error) {
  48. setError(error);
  49. } finally {
  50. setIsLoading(false);
  51. }
  52. }, [user]);
  53.  
  54. useEffect(() => {
  55. fetchUserData();
  56. }, [fetchUserData]);
  57.  
  58. useEffect(() => {
  59. fetchUserBookings();
  60. }, [user, fetchUserBookings]);
  61.  
  62. const logoutHandler = async () => {
  63. setIsLoading(true);
  64. setError(null);
  65. try {
  66. await logoutUser();
  67. navigate('/');
  68. } catch (error) {
  69. setError(error);
  70. } finally {
  71. setIsLoading(false);
  72. }
  73. };
  74.  
  75. const handleDelete = async () => {
  76. setIsLoading(true);
  77. setError(null);
  78. try {
  79. await deleteUserById(userToDelete);
  80. navigate('/');
  81. } catch (error) {
  82. setError(error);
  83. } finally {
  84. setIsLoading(false);
  85. }
  86. };
  87.  
  88. const expired = (createdAt) => {
  89. const [day, month, year, hour, minute] = createdAt.match(/\d+/g).map(Number);
  90. const startDate = new Date(year, month - 1, day, hour, minute);
  91. const endDate = new Date(startDate.getTime() + 2 * 60 * 60 * 1000 + 10 * 60 * 1000); // 2 hours and 10 minutes after
  92. const now = new Date();
  93. return endDate - now <= 0;
  94. };
  95.  
  96. const addTwoHours = (dataStr) => {
  97. const data = parse(dataStr, 'dd-MM-yyyy HH:mm', new Date());
  98. const nuovaData = new Date(data.getTime() + 2 * 60 * 60 * 1000);
  99. return format(nuovaData, 'dd-MM-yyyy HH:mm');
  100. };
  101.  
  102. return (
  103. <>
  104. <MetaTags
  105. title="My Account - GMapartments"
  106. description="Case vacanze, affiti e bed and breakfast in Puglia a Pulsano, Taranto, sul mare"
  107. keywords="Case vacanze, affiti, bed and breakfast, b&b, Puglia, Pulsano, Taranto, mare"
  108. robotsContent="noindex, nofollow"
  109. />
  110. {confirmDelete && (
  111. <div className="confirm-delete-bg">
  112. <div className="confirm-delete">
  113. <h3>Conferma la cancellazione dell{"'"}account?</h3>
  114. {error && <p className="error">{error.message}</p>}
  115. <div className="buttons-container">
  116. {!isLoading ? (
  117. <button className="delete-btn" onClick={handleDelete}>
  118. Si
  119. </button>
  120. ) : (
  121. <button className="delete-btn" disabled>
  122. Loading...
  123. </button>
  124. )}
  125. <button className="modify-btn" onClick={() => setConfirmDelete(false)}>
  126. No
  127. </button>
  128. </div>
  129. </div>
  130. </div>
  131. )}
  132. {error && <p>{error.message}</p>}
  133. {isLoading && <div className="loader-container"><span className="loader"></span></div>}
  134. {!isLoading && (
  135. <div style={{ marginTop: '120px' }}>
  136. <div className="grid text-center">
  137. <div className="col-100">
  138. <h2 className="font-title">Ciao {user?.first_name}</h2>
  139. </div>
  140. </div>
  141. <div className="grid">
  142. <div className="col-100">
  143. <h3>Informazioni Personali</h3>
  144. <p>Nome: {user?.first_name}</p>
  145. <p>Cognome: {user?.last_name}</p>
  146. <p>Email: {user?.email}</p>
  147. <p>Telefono: {user?.telephone}</p>
  148. </div>
  149. </div>
  150. <div className="grid mt-3">
  151. <div className="col-100">
  152. <h3>Le Tue Prenotazioni</h3>
  153. {userBookings.length > 0 ? (
  154. userBookings.map((booking) => (
  155. <div key={booking.id} className="booking mb-2" style={{ borderBottom: '2px solid var(--light-blue)', paddingBottom: '20px' }}>
  156. <p>Camera: {booking.room.name}</p>
  157. <p>
  158. Nome sulla Prenotazione: {booking.first_name_on_reservation} {booking.last_name_on_reservation}
  159. </p>
  160. <p>E-mail sulla prenotazione: {booking.email_on_reservation}</p>
  161. <p>Cellulare sulla prenotazione: {booking.phone_on_reservation}</p>
  162. <p>Da: {booking.check_in} - A: {booking.check_out}</p>
  163. <p>Costo: {booking.total_cost}€</p>
  164. <p>Creata il: {addTwoHours(booking.created_at)}</p>
  165. <p>Status: {booking.status}</p>
  166. {booking.status !== 'PAID' && booking.status !== 'CANCELED' && (
  167. <p className="error">
  168. Scade tra: <CountdownTimer createdAt={booking.created_at} />
  169. </p>
  170. )}
  171. {booking.status === 'PAID' && (
  172. <Link to={`/delete-booking/${booking.reservation_id}`} className="delete-booking font-body delete-btn">
  173. Cancella Prenotazione
  174. </Link>
  175. )}
  176. {!expired(booking.created_at) && booking.status !== 'PAID' && booking.status !== 'CANCELED' && (
  177. <Link to={`/booking/${booking.reservation_id}/payment`} className="delete-booking font-body delete-btn">
  178. Ritenta il pagamento
  179. </Link>
  180. )}
  181. </div>
  182. ))
  183. ) : (
  184. <p>Nessuna Prenotazione</p>
  185. )}
  186. </div>
  187. </div>
  188. <div className="grid mt-3">
  189. <div className="col-100 mt-2">
  190. {isAdmin && (
  191. <Link to="/dashboard" className="button2">
  192. Vai alla Dashboard
  193. </Link>
  194. )}
  195. </div>
  196. <div className="col-100 mt-2">
  197. {!isLoading && (
  198. <button className="delete-btn" onClick={logoutHandler}>
  199. Logout
  200. </button>
  201. )}
  202. {isLoading && (
  203. <button className="delete-btn" disabled>
  204. Loading...
  205. </button>
  206. )}
  207. </div>
  208. <div className="col-100 mt-4">
  209. <button className="delete-btn" onClick={() => setConfirmDelete(true)}>
  210. Elimina il mio account
  211. </button>
  212. </div>
  213. </div>
  214. </div>
  215. )}
  216. </>
  217. );
  218. };
  219.  
  220. export default MyAccount;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement