|
| 1 | +import React, { useCallback, useEffect, useState } from 'react'; |
| 2 | +import { getTime, subMonths, subWeeks, format } from 'date-fns'; |
| 3 | +import DayPicker from 'react-day-picker'; |
| 4 | +import { Button } from '@codesandbox/common/lib/components/Button'; |
| 5 | +import Margin from '@codesandbox/common/lib/components/spacing/Margin'; |
| 6 | +import MaxWidth from '@codesandbox/common/lib/components/flex/MaxWidth'; |
| 7 | +import { useOvermind } from 'app/overmind'; |
| 8 | +import { SubTitle } from 'app/components/SubTitle'; |
| 9 | +import { DelayedAnimation } from 'app/components/DelayedAnimation'; |
| 10 | +import { Navigation } from 'app/pages/common/Navigation'; |
| 11 | +import 'react-day-picker/lib/style.css'; |
| 12 | +import { Container, Buttons, Heading, PickerWrapper } from './elements'; |
| 13 | +import SandboxCard from './SandboxCard'; |
| 14 | + |
| 15 | +const Curator: React.FC = () => { |
| 16 | + const { |
| 17 | + state: { |
| 18 | + explore: { popularSandboxes }, |
| 19 | + }, |
| 20 | + actions: { |
| 21 | + explore: { pickSandboxModal, popularSandboxesMounted }, |
| 22 | + }, |
| 23 | + } = useOvermind(); |
| 24 | + |
| 25 | + const [selectedDate, setSelectedDate] = useState(null); |
| 26 | + const [showPicker, setShowPicker] = useState(false); |
| 27 | + |
| 28 | + const fetchPopularSandboxes = useCallback( |
| 29 | + date => { |
| 30 | + popularSandboxesMounted({ date }); |
| 31 | + }, |
| 32 | + [popularSandboxesMounted] |
| 33 | + ); |
| 34 | + |
| 35 | + useEffect(() => { |
| 36 | + fetchPopularSandboxes(getTime(subWeeks(new Date(), 1))); |
| 37 | + }, [fetchPopularSandboxes]); |
| 38 | + |
| 39 | + const pickSandbox = useCallback( |
| 40 | + (id, title, description) => { |
| 41 | + pickSandboxModal({ details: { description, id, title } }); |
| 42 | + }, |
| 43 | + [pickSandboxModal] |
| 44 | + ); |
| 45 | + |
| 46 | + const handleDayClick = useCallback( |
| 47 | + date => { |
| 48 | + fetchPopularSandboxes(getTime(new Date(date))); |
| 49 | + |
| 50 | + setSelectedDate(date); |
| 51 | + setShowPicker(false); |
| 52 | + }, |
| 53 | + [fetchPopularSandboxes] |
| 54 | + ); |
| 55 | + |
| 56 | + return ( |
| 57 | + <MaxWidth> |
| 58 | + <Margin horizontal={1.5} vertical={1.5}> |
| 59 | + <Navigation title="Curator Page" /> |
| 60 | + |
| 61 | + <Heading>Curator Page</Heading> |
| 62 | + |
| 63 | + <SubTitle> |
| 64 | + Here you can choose the sandboxes that go in the explore page |
| 65 | + </SubTitle> |
| 66 | + |
| 67 | + <Buttons> |
| 68 | + Most popular sandboxes in the: |
| 69 | + <Button |
| 70 | + onClick={() => |
| 71 | + fetchPopularSandboxes(getTime(subWeeks(new Date(), 1))) |
| 72 | + } |
| 73 | + small |
| 74 | + > |
| 75 | + Last Week |
| 76 | + </Button> |
| 77 | + <Button |
| 78 | + onClick={() => |
| 79 | + fetchPopularSandboxes(getTime(subMonths(new Date(), 1))) |
| 80 | + } |
| 81 | + small |
| 82 | + > |
| 83 | + Last Month |
| 84 | + </Button> |
| 85 | + <Button |
| 86 | + onClick={() => |
| 87 | + fetchPopularSandboxes(getTime(subMonths(new Date(), 6))) |
| 88 | + } |
| 89 | + small |
| 90 | + > |
| 91 | + Last 6 Months |
| 92 | + </Button> |
| 93 | + <Button onClick={() => setShowPicker(show => !show)} small> |
| 94 | + {selectedDate |
| 95 | + ? format(new Date(selectedDate), 'dd/MM/yyyy') |
| 96 | + : 'Custom'} |
| 97 | + </Button> |
| 98 | + {showPicker ? ( |
| 99 | + <PickerWrapper> |
| 100 | + <DayPicker |
| 101 | + onDayClick={handleDayClick} |
| 102 | + selectedDays={selectedDate} |
| 103 | + /> |
| 104 | + </PickerWrapper> |
| 105 | + ) : null} |
| 106 | + </Buttons> |
| 107 | + |
| 108 | + {popularSandboxes ? ( |
| 109 | + <Container> |
| 110 | + {popularSandboxes.sandboxes.map(sandbox => ( |
| 111 | + <SandboxCard |
| 112 | + key={sandbox.id} |
| 113 | + {...sandbox} |
| 114 | + pickSandbox={pickSandbox} |
| 115 | + /> |
| 116 | + ))} |
| 117 | + </Container> |
| 118 | + ) : ( |
| 119 | + <DelayedAnimation |
| 120 | + style={{ |
| 121 | + color: 'rgba(255, 255, 255, 0.5)', |
| 122 | + fontWeight: 600, |
| 123 | + marginTop: '2rem', |
| 124 | + textAlign: 'center', |
| 125 | + }} |
| 126 | + delay={0} |
| 127 | + > |
| 128 | + Fetching Sandboxes... |
| 129 | + </DelayedAnimation> |
| 130 | + )} |
| 131 | + </Margin> |
| 132 | + </MaxWidth> |
| 133 | + ); |
| 134 | +}; |
| 135 | + |
| 136 | +export default Curator; |
0 commit comments