Skip to content

Commit

Permalink
chore: adding a dropdown menu on to filter data by monthly or daily
Browse files Browse the repository at this point in the history
  • Loading branch information
Kingscliq committed Sep 25, 2023
1 parent 717d2ca commit c4dcc43
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 23 deletions.
15 changes: 10 additions & 5 deletions leaderboard/src/components/elements/Select/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ export interface SelectDropdownTypes {
textTransform?: string;
loading?: boolean;
name?: string;
label: string;
label?: string;
error?: boolean;
errorMessage?: string;
placeholder?: string;
}

const SelectDropdown = React.forwardRef(function SelectDropdown(
Expand All @@ -31,6 +32,7 @@ const SelectDropdown = React.forwardRef(function SelectDropdown(
label,
error,
errorMessage,
placeholder,
...rest
}: SelectDropdownTypes,
ref
Expand Down Expand Up @@ -82,9 +84,12 @@ const SelectDropdown = React.forwardRef(function SelectDropdown(

return (
<div>
<div className="mb-4">
<FormLabel htmlFor={name} title={label} />
</div>
{label && (
<div className="mb-4">
<FormLabel htmlFor={name} title={label} />
</div>
)}

{multiSelect ? (
<Select
isMulti
Expand Down Expand Up @@ -150,7 +155,7 @@ const SelectDropdown = React.forwardRef(function SelectDropdown(
options={options}
name={name}
isLoading={loading}
placeholder="Select an Item"
placeholder={placeholder || 'Select an Item'}
{...rest}
/>
)}
Expand Down
56 changes: 45 additions & 11 deletions leaderboard/src/components/elements/Table/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { ReactNode, useState } from 'react';
import React, { ReactNode, SetStateAction, useState } from 'react';

import {
useReactTable,
Expand All @@ -17,12 +17,15 @@ import Paragraph from '../Text/Paragraph';
import TextField from '../TextField';
import { search } from '@assets/icons';
import Image from '../Image';
import SelectDropdown from '../Select';

interface TableProps {
data: any;
columns: any;
loading: boolean;
noData?: string;
option?: string;
setOption?: React.Dispatch<SetStateAction<string>>;
}

interface PaginationButtonProps {
Expand All @@ -38,23 +41,42 @@ const TableComponent: React.FC<TableProps> = ({
columns,
loading,
noData,
setOption,
option,
}) => {
return <Table {...{ data, columns }} loading={loading} noData={noData} />;
return (
<Table
{...{ data, columns }}
loading={loading}
noData={noData}
setOption={setOption}
option={option}
/>
);
};

function Table({
data,
columns,
loading,
noData,
setOption,
option,
}: {
data: LeaderBoardData[];
columns: ColumnDef<LeaderBoardData>[];
loading: boolean;
noData?: string;
option?: string;
setOption?: React.Dispatch<SetStateAction<string>>;
}) {
const [globalFilter, setGlobalFilter] = useState<string>('');
const [sorting, setSorting] = useState<SortingState>([]);
const options: { label: string; value: string }[] = [
{ label: 'Monthly', value: 'monthly' },
{ label: 'Daily', value: 'daily' },
];

const table = useReactTable({
data,
columns,
Expand All @@ -81,7 +103,7 @@ function Table({
<button
className={`${className} border rounded p-1 text-xs ${
disabled ? 'bg-primary-100 opacity-60' : 'bg-primary text-white'
} `}
}`}
disabled={disabled || loading}
onClick={onClick}
>
Expand All @@ -91,14 +113,26 @@ function Table({
};
return (
<section>
<div className="flex items-center mt-4 mx-4">
<TextField
value={globalFilter ?? ''}
onChange={(event) => setGlobalFilter(event.target.value)}
className="p-2 font-lg border border-block"
placeholder="Search Member"
leftIcon={<Image src={search} alt={'Search'} />}
/>
<div className="grid grid-cols-2 justify-between mt-4 mx-4">
<div>
<TextField
value={globalFilter ?? ''}
onChange={(event) => setGlobalFilter(event.target.value)}
className="p-2 font-lg border border-block"
placeholder="Search Member"
leftIcon={<Image src={search} alt={'Search'} />}
/>
</div>
<div className="ml-4">
<SelectDropdown
options={options}
defaultValue={option}
placeholder='Toggle By'
onChange={({ value }: { value: string }) => {
setOption!(value);
}}
/>
</div>
</div>
<article className="flex flex-col border border-light-grey">
<div className="overflow-x-auto sm:-mx-6 lg:-mx-8">
Expand Down
5 changes: 4 additions & 1 deletion leaderboard/src/features/Leaderboard/Leaderboard.Module.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Header from './components/Header';
import React from 'react';

const HomeModule = () => {
const { leaderBoard, leadColumns, loadingLeaderBoard } =
const { leaderBoard, leadColumns, loadingLeaderBoard, period, setPeriod } =
useFetchLeaderBoard();

const _leaderboard = React.useMemo(
Expand All @@ -15,13 +15,16 @@ const HomeModule = () => {
: [],
[leaderBoard]
);

return (
<Section>
<Header />
<TableComponent
data={_leaderboard}
columns={leadColumns}
loading={loadingLeaderBoard}
option={period}
setOption={setPeriod}
/>
</Section>
);
Expand Down
15 changes: 9 additions & 6 deletions leaderboard/src/features/Leaderboard/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@ import { useQuery } from '@tanstack/react-query';
import { ColumnDef } from '@tanstack/react-table';
import { totalPoints } from '@utils/helpers';
import { axios } from 'lib';
import React from 'react';
import React, { useState } from 'react';
import { toast } from 'react-toastify';

export const useFetchLeaderBoard = () => {
const fetchLeaderBoard = async () => {
const fetchLeaderBoard = async (period: string) => {
try {
const response = await axios.get(
`directory_items.json/?order=likes_received&period=monthly`
`directory_items.json/?order=likes_received&period=${
period || 'monthly'
}`
);
return response?.data;
} catch (error) {
throw error;
}
};

const [period, setPeriod] = useState<string>('monthly');
const leadColumns = React.useMemo<ColumnDef<LeaderBoardData>[]>(
() => [
{
Expand Down Expand Up @@ -99,14 +102,14 @@ export const useFetchLeaderBoard = () => {
);

const { data: leaderBoard, isFetching: loadingLeaderBoard } = useQuery({
queryKey: ['leader-board'],
queryFn: fetchLeaderBoard,
queryKey: ['leader-board', period],
queryFn: () => fetchLeaderBoard(period),
onError: (err) =>
toast.error(
typeof err === 'string'
? err
: 'Something went wrong fetching status list'
),
});
return { leaderBoard, loadingLeaderBoard, leadColumns };
return { leaderBoard, loadingLeaderBoard, leadColumns, period, setPeriod };
};

0 comments on commit c4dcc43

Please sign in to comment.