Advertisement
Guest User

Untitled

a guest
May 28th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. import { useRouter } from 'next/router'
  2. import { countryCodes } from '../../models/countryCodes'
  3. import { sanitizeLicensePlate } from '../../helpers/licensePlateHelper'
  4. import { getMessages } from '../../helpers/apiHelper'
  5. import useSWR from 'swr'
  6. import moment from 'moment'
  7.  
  8. export default () => {
  9. const router = useRouter()
  10. const { countryCode, licensePlate } = router.query
  11.  
  12. if (!countryCode || !licensePlate)
  13. {
  14. return 'Not found'
  15. }
  16.  
  17. // validate country code
  18. let country = countryCodes.find(c => c.code == countryCode);
  19. let countryIsValid = country != null;
  20. let plateIsValid = sanitizeLicensePlate(licensePlate) == licensePlate;
  21.  
  22. if (!countryIsValid || !plateIsValid)
  23. {
  24. return 'Not found'
  25. }
  26.  
  27. const { data, error } = useSWR(`${countryCode}/${licensePlate}`, getMessages)
  28.  
  29. let list = <p>Loading...</p>;
  30. if (data && Array.isArray(data))
  31. {
  32. if (data.length == 0)
  33. {
  34. list = <p>No messages yet</p>
  35. }
  36. else
  37. {
  38. list = <ul>{data.map((message, index) => (<li key={`m${index}`}>
  39. <div className="card">
  40. <div className="avatar">
  41. <i className="fa fa-user-secret fa-2x"></i>
  42. </div>
  43. <div className="content">
  44. <p>{message.content}</p>
  45. <span><i className="fas fa-mobile-alt"></i> @ {moment.utc(message.created).local().format("YYYY-MM-DD H:mm:ss")}</span>
  46. </div>
  47. </div>
  48. </li>))}</ul>
  49. }
  50. }
  51. if (error)
  52. {
  53. list = <p>Error</p>
  54. }
  55. return (
  56. <div className="horizontal-flex">
  57. <header>
  58. <div className="logo vertical with-margin">
  59. <div>
  60. <h1>carmsngr</h1>
  61. </div>
  62. </div>
  63. </header>
  64.  
  65. <main>
  66. <h2><i className={`flag-icon flag-icon-${countryCode}`}></i> {country.plateDisplay(licensePlate)}</h2>
  67. {list}
  68. <div>
  69. <p className="black">To submit a message send an SMS or WhatsApp message in the following format:<br /><strong>{`${countryCode},${licensePlate},{your message}`}</strong><br />(e.g. "{`${countryCode},${licensePlate},Your left brake light is broken`}")<br />to <strong>+1 (334) 367-4637</strong>.<br />Following countries are supported: cz, sk, at, de, pl</p>
  70. </div>
  71. </main>
  72.  
  73. </div>
  74. )
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement