|
| 1 | +import React from 'react'; |
| 2 | +import { Grid, Column, Stack, Text, IconButton } from '@codesandbox/components'; |
| 3 | +import css from '@styled-system/css'; |
| 4 | +import { useOvermind } from 'app/overmind'; |
| 5 | +import { SandboxCard, SkeletonCard } from './SandboxCard'; |
| 6 | +import { SANDBOXES_PER_PAGE } from './constants'; |
| 7 | + |
| 8 | +export const LikedSandboxes = ({ menuControls }) => { |
| 9 | + const { |
| 10 | + actions: { |
| 11 | + profile: { likedSandboxesPageChanged }, |
| 12 | + }, |
| 13 | + state: { |
| 14 | + profile: { |
| 15 | + current: { username }, |
| 16 | + isLoadingSandboxes, |
| 17 | + currentLikedSandboxesPage, |
| 18 | + likedSandboxes, |
| 19 | + }, |
| 20 | + }, |
| 21 | + } = useOvermind(); |
| 22 | + |
| 23 | + // explicitly call it on first page render |
| 24 | + React.useEffect(() => { |
| 25 | + if (currentLikedSandboxesPage === 1) likedSandboxesPageChanged(1); |
| 26 | + }, [currentLikedSandboxesPage, likedSandboxesPageChanged]); |
| 27 | + |
| 28 | + const sandboxes = ( |
| 29 | + (likedSandboxes[username] && |
| 30 | + likedSandboxes[username][currentLikedSandboxesPage]) || |
| 31 | + [] |
| 32 | + ) |
| 33 | + // only show public sandboxes on profile |
| 34 | + .filter(sandbox => sandbox.privacy === 0); |
| 35 | + |
| 36 | + return ( |
| 37 | + <Stack as="section" direction="vertical" gap={6}> |
| 38 | + <Stack justify="space-between" align="center"> |
| 39 | + <Text size={7} weight="bold"> |
| 40 | + Liked Sandboxes |
| 41 | + </Text> |
| 42 | + </Stack> |
| 43 | + |
| 44 | + <Grid |
| 45 | + rowGap={6} |
| 46 | + columnGap={6} |
| 47 | + css={{ |
| 48 | + gridTemplateColumns: 'repeat(auto-fit, minmax(300px, 1fr))', |
| 49 | + }} |
| 50 | + > |
| 51 | + {isLoadingSandboxes |
| 52 | + ? Array(SANDBOXES_PER_PAGE) |
| 53 | + .fill(true) |
| 54 | + .map((_, index) => ( |
| 55 | + // eslint-disable-next-line |
| 56 | + <Column key={index}> |
| 57 | + <SkeletonCard /> |
| 58 | + </Column> |
| 59 | + )) |
| 60 | + : sandboxes.map((sandbox, index) => ( |
| 61 | + <Column key={sandbox.id}> |
| 62 | + <SandboxCard sandbox={sandbox} menuControls={menuControls} /> |
| 63 | + </Column> |
| 64 | + ))} |
| 65 | + </Grid> |
| 66 | + <Pagination /> |
| 67 | + </Stack> |
| 68 | + ); |
| 69 | +}; |
| 70 | + |
| 71 | +const Pagination = () => { |
| 72 | + const { |
| 73 | + actions: { |
| 74 | + profile: { likedSandboxesPageChanged }, |
| 75 | + }, |
| 76 | + state: { |
| 77 | + profile: { |
| 78 | + currentLikedSandboxesPage, |
| 79 | + current: { givenLikeCount }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + } = useOvermind(); |
| 83 | + |
| 84 | + const numberOfPages = Math.ceil(givenLikeCount / SANDBOXES_PER_PAGE); |
| 85 | + |
| 86 | + return ( |
| 87 | + <nav role="navigation" aria-label="Pagination Navigation"> |
| 88 | + <Stack |
| 89 | + as="ul" |
| 90 | + gap={4} |
| 91 | + justify="center" |
| 92 | + align="center" |
| 93 | + css={css({ marginX: 0, marginY: 10, listStyle: 'none' })} |
| 94 | + > |
| 95 | + <li> |
| 96 | + <IconButton |
| 97 | + name="backArrow" |
| 98 | + title="Previous page" |
| 99 | + onClick={() => |
| 100 | + likedSandboxesPageChanged(currentLikedSandboxesPage - 1) |
| 101 | + } |
| 102 | + disabled={currentLikedSandboxesPage === 1} |
| 103 | + /> |
| 104 | + </li> |
| 105 | + <li> |
| 106 | + <IconButton |
| 107 | + name="backArrow" |
| 108 | + title="Next page" |
| 109 | + style={{ transform: 'scaleX(-1)' }} |
| 110 | + onClick={() => |
| 111 | + likedSandboxesPageChanged(currentLikedSandboxesPage + 1) |
| 112 | + } |
| 113 | + disabled={currentLikedSandboxesPage === numberOfPages} |
| 114 | + /> |
| 115 | + </li> |
| 116 | + </Stack> |
| 117 | + </nav> |
| 118 | + ); |
| 119 | +}; |
0 commit comments