Skip to content

Commit

Permalink
Sorted by dates by default (#1257)
Browse files Browse the repository at this point in the history
* sorted on dates by  default

* removed unnecessary comments

* Update packages/storage-ui/src/Components/Pages/CidsPage.tsx

Co-authored-by: Thibaut Sardan <33178835+Tbaut@users.noreply.github.com>

* added sort directions

Co-authored-by: Thibaut Sardan <33178835+Tbaut@users.noreply.github.com>
  • Loading branch information
tanmoyAtb and Tbaut authored Jul 13, 2021
1 parent 2de4dc5 commit acceb11
Showing 1 changed file with 49 additions and 6 deletions.
55 changes: 49 additions & 6 deletions packages/storage-ui/src/Components/Pages/CidsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import React, { useState } from "react"
import React, { useMemo, useState } from "react"
import { makeStyles, createStyles } from "@chainsafe/common-theme"
import { Button, PlusIcon, Table, TableBody, TableHead, TableHeadCell, TableRow, Typography } from "@chainsafe/common-components"
import { useStorage } from "../../Contexts/StorageContext"
import { Trans } from "@lingui/macro"
import CidRow from "../Elements/CidRow"
import { CSSTheme } from "../../Themes/types"
import AddCIDModal from "../Modules/AddCIDModal"
import { PinStatus } from "@chainsafe/files-api-client"

export const desktopGridSettings = "3fr 190px 190px 190px 190px 70px !important"
export const mobileGridSettings = "3fr 190px 190px 190px 190px 70px !important"
export const desktopGridSettings = "3fr 160px 120px 120px 140px 70px !important"
export const mobileGridSettings = "3fr 160px 120px 120px 140px 70px !important"

const useStyles = makeStyles(({ animation, breakpoints, constants }: CSSTheme) =>
createStyles({
Expand Down Expand Up @@ -49,10 +50,46 @@ const useStyles = makeStyles(({ animation, breakpoints, constants }: CSSTheme) =
})
)

type SortColumn = "size" | "date_uploaded"
type SortDirection = "ascend" | "descend"

const CidsPage = () => {
const classes = useStyles()
const { pins } = useStorage()
const [addCIDOpen, setAddCIDOpen] = useState(false)
const [sortColumn, setSortColumn] = useState<SortColumn>("date_uploaded")
const [sortDirection, setSortDirection] = useState<SortDirection>("descend")

const handleSortToggle = (
targetColumn: SortColumn
) => {
if (sortColumn !== targetColumn) {
setSortColumn(targetColumn)
setSortDirection("descend")
} else {
if (sortDirection === "ascend") {
setSortDirection("descend")
} else {
setSortDirection("ascend")
}
}
}

const sortedPins: PinStatus[] = useMemo(() => {
let temp = []

switch (sortColumn) {
case "size": {
temp = pins.sort((a, b) => (a.info?.size < b.info?.size ? -1 : 1))
break
}
default: {
temp = pins.sort((a, b) => (a.created < b.created ? -1 : 1))
break
}
}
return sortDirection === "descend" ? temp.reverse() : temp
}, [pins, sortDirection, sortColumn])

return (
<>
Expand Down Expand Up @@ -99,13 +136,19 @@ const CidsPage = () => {
<Trans>Cid</Trans>
</TableHeadCell>
<TableHeadCell
sortButtons={false}
sortButtons={true}
onSortChange={() => handleSortToggle("date_uploaded")}
sortDirection={sortColumn === "date_uploaded" ? sortDirection : undefined}
sortActive={sortColumn === "date_uploaded"}
align="center"
>
<Trans>Created</Trans>
</TableHeadCell>
<TableHeadCell
sortButtons={false}
sortButtons={true}
onSortChange={() => handleSortToggle("size")}
sortDirection={sortColumn === "size" ? sortDirection : undefined}
sortActive={sortColumn === "size"}
align="center"
>
<Trans>Size</Trans>
Expand All @@ -121,7 +164,7 @@ const CidsPage = () => {
</TableRow>
</TableHead>
<TableBody>
{pins.map((pinStatus, index) =>
{sortedPins.map((pinStatus, index) =>
<CidRow
pinStatus={pinStatus}
key={index}
Expand Down

0 comments on commit acceb11

Please sign in to comment.