index.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import React, { FunctionComponent } from 'react';
  2. import Critter, { Time, Month, Colors } from 'types';
  3. import { Table as Tb } from 'reactstrap';
  4. import Tag from 'components/Tag';
  5. import Button from 'components/Button';
  6. import moment from 'moment';
  7. // import { includes } from 'ramda';
  8. import { parseMonth } from 'services/DataParser';
  9. import './styles.css';
  10. type TableProps = {
  11. data: Critter[],
  12. }
  13. const Table: FunctionComponent<TableProps> = ({ data }) => {
  14. // const isInTimeRange = (rangeTime: Time) => {
  15. // return rangeTime.some(([from, to]) => moment().isBetween(moment().hour(from).minute(0), moment().hour(to < from ? to + 24 : to).minute(0)));
  16. // }
  17. // const availableNow = () => {
  18. // const currentMonth = moment().month() + 1;
  19. // const xs = data.filter(fish => {
  20. // return includes(currentMonth, fish.months) && isInTimeRange(fish.time)
  21. // });
  22. // }
  23. const showAvailability = (months: Month[]): { color: Colors, text: string} => {
  24. const currentMonth = (moment().get('month') as Month) + 1;
  25. if (months.length === 12){
  26. return {
  27. color: 'green',
  28. text: 'all year'
  29. }
  30. }
  31. const availableNow = months.findIndex(m => m === currentMonth);
  32. if (availableNow === -1) {
  33. const availableFrom = months.find(m => m > currentMonth) || 1;
  34. return {
  35. color: 'purple',
  36. text: `from ${parseMonth(availableFrom)}`
  37. }
  38. }
  39. const nextIteration = (i: number) => (i + 1) % months.length;
  40. let i = availableNow;
  41. while (true) {
  42. const actual = months[i];
  43. const next = months[nextIteration(i)];
  44. if ( (actual % 12) + 1 !== next) {
  45. const lastMonth = actual === currentMonth;
  46. return lastMonth ? {
  47. color: 'red',
  48. text: 'last month available'
  49. } : {
  50. color: 'orange',
  51. text: `until ${parseMonth(actual)}`
  52. }
  53. } else {
  54. i = nextIteration(i);
  55. }
  56. }
  57. }
  58. const DisplayMonths: FunctionComponent<{months: Month[]}> = ({ months }) => {
  59. const result = showAvailability(months);
  60. return (
  61. <Tag color={result.color}>{result.text}</Tag>
  62. )
  63. }
  64. const DisplayTime: FunctionComponent<{time: Time}> = ({ time }) => {
  65. return (
  66. <div>
  67. {
  68. time.map(([from, to]) => {
  69. return (from === 0 && to === 24) ?
  70. 'All day' : `${from}hs - ${to}hs`
  71. }).join(' & ')
  72. }
  73. </div>
  74. );
  75. }
  76. return (
  77. <div className="cc-critter-schedule">
  78. <Button>Show available now</Button>
  79. <Button color='link'>link button yaya</Button>
  80. <Tb striped hover responsive className="critter-table">
  81. <thead>
  82. <tr>
  83. <th></th>
  84. <th>Name</th>
  85. <th>price</th>
  86. <th>location</th>
  87. <th>time</th>
  88. <th>availability</th>
  89. </tr>
  90. </thead>
  91. <tbody>
  92. {
  93. data.map((critter, key) => (
  94. <tr key={key}>
  95. <td className="critter-img"><img className="critter-img" src={`${process.env.REACT_APP_API}/${critter.img}`} alt={critter.name}/></td>
  96. <td>{critter.name}</td>
  97. <td>{critter.price}</td>
  98. <td>{critter.location}</td>
  99. <td>{DisplayTime(critter)}</td>
  100. <td>{DisplayMonths(critter)}</td>
  101. </tr>
  102. ))
  103. }
  104. </tbody>
  105. </Tb>
  106. </div>
  107. )
  108. }
  109. export default Table;