Skip to content

Commit

Permalink
Fix category issues (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
gokulkrishh authored Jun 10, 2023
1 parent b8ba11b commit ce41f75
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 17 deletions.
20 changes: 19 additions & 1 deletion components/Table/IncomeTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,21 @@ import NoDataTable from './NoDataTable';
const tdClassNames = 'relative p-4 pl-8 text-zinc-600 text-sm';
const thList = ['Name', 'Price', 'Received Date ↓', 'Category', 'Notes', 'Actions'];

export default function IncomeTable({ onFilterChange, filterKey, isLoading, data = [], onEdit, onDelete, user }) {
const categoryFilterData = Object.keys(incomeCategory)
.filter(Boolean)
.map((key) => ({ name: incomeCategory[key]?.name, key }));

export default function IncomeTable({
onFilterChange,
filterKey,
isLoading,
data = [],
onEdit,
onDelete,
user,
onCategoryFilterChange,
categories,
}) {
const { currency, locale, isPremiumPlan, isPremiumPlanEnded } = user;

if (!isLoading && !data.length) {
Expand All @@ -36,6 +50,10 @@ export default function IncomeTable({ onFilterChange, filterKey, isLoading, data
thList={thList}
isLoading={isLoading}
isPremiumPlan={isPremiumPlan && !isPremiumPlanEnded}
categories={categories}
enableCategoryFilter
onCategoryFilterChange={onCategoryFilterChange}
categoryFilterData={categoryFilterData}
>
{data.map((datum) => {
const isToday = isItToday(new Date(datum.date), new Date());
Expand Down
20 changes: 19 additions & 1 deletion components/Table/InvestmentTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,21 @@ import NoDataTable from './NoDataTable';
const tdClassNames = 'relative p-4 pl-8 text-sm';
const thList = ['Name', 'Price', 'Units', 'Bought Date ↓', 'Category', 'Notes', 'Actions'];

export default function InvestmentTable({ isLoading, data = [], onEdit, onDelete, user, onFilterChange, filterKey }) {
const categoryFilterData = Object.keys(investmentCategory)
.filter(Boolean)
.map((key) => ({ name: investmentCategory[key]?.name, key }));

export default function InvestmentTable({
isLoading,
data = [],
onEdit,
onDelete,
user,
onFilterChange,
filterKey,
onCategoryFilterChange,
categories,
}) {
const { currency, locale, isPremiumPlan, isPremiumPlanEnded } = user;

if (!isLoading && !data.length) {
Expand All @@ -36,6 +50,10 @@ export default function InvestmentTable({ isLoading, data = [], onEdit, onDelete
thList={thList}
isLoading={isLoading}
isPremiumPlan={isPremiumPlan && !isPremiumPlanEnded}
categories={categories}
enableCategoryFilter
onCategoryFilterChange={onCategoryFilterChange}
categoryFilterData={categoryFilterData}
>
{data.map((datum) => {
const isToday = isItToday(new Date(datum.date), new Date());
Expand Down
5 changes: 0 additions & 5 deletions pages/api/expenses/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ export default withUserAuth(async (req, res, user) => {
const { categories = '' } = req.query;
const categoriesList = categories.split(',');

console.log(
'asdasd --->',
categoriesList.map((category) => ({ category: { contains: category } }))
);

if (req.method === 'GET') {
try {
const data = await prisma.expenses.findMany({
Expand Down
5 changes: 4 additions & 1 deletion pages/api/income/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import prisma from 'lib/prisma';

export default withUserAuth(async (req, res, user) => {
if (req.method === 'GET') {
const { categories = '' } = req.query;
const categoriesList = categories.split(',');

try {
const data = await prisma.income.findMany({
where: { user_id: user.id },
where: { user_id: user.id, OR: categoriesList.map((category) => ({ category: { contains: category } })) },
select: {
notes: true,
name: true,
Expand Down
9 changes: 7 additions & 2 deletions pages/api/income/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ import prisma from 'lib/prisma';

export default withUserAuth(async (req, res, user) => {
if (req.method === 'GET') {
const { start, end } = req.query;
const { start, end, categories = '' } = req.query;
const categoriesList = categories.split(',');
try {
const data = await prisma.income.findMany({
where: { user_id: user.id, date: { lte: end, gte: start } },
where: {
user_id: user.id,
date: { lte: end, gte: start },
OR: categoriesList.map((category) => ({ category: { contains: category } })),
},
select: {
notes: true,
name: true,
Expand Down
5 changes: 4 additions & 1 deletion pages/api/investments/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import prisma from 'lib/prisma';

export default withUserAuth(async (req, res, user) => {
if (req.method === 'GET') {
const { categories = '' } = req.query;
const categoriesList = categories.split(',');

try {
const data = await prisma.investments.findMany({
where: { user_id: user.id },
where: { user_id: user.id, OR: categoriesList.map((category) => ({ category: { contains: category } })) },
select: {
notes: true,
name: true,
Expand Down
9 changes: 7 additions & 2 deletions pages/api/investments/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ import prisma from 'lib/prisma';

export default withUserAuth(async (req, res, user) => {
if (req.method === 'GET') {
const { start, end } = req.query;
const { start, end, categories = '' } = req.query;
const categoriesList = categories.split(',');
try {
const data = await prisma.investments.findMany({
where: { user_id: user.id, date: { lte: end, gte: start } },
where: {
user_id: user.id,
date: { lte: end, gte: start },
OR: categoriesList.map((category) => ({ category: { contains: category } })),
},
select: {
notes: true,
name: true,
Expand Down
7 changes: 5 additions & 2 deletions pages/app/income.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ export default function Income({ user }) {
const [show, setShow] = useState(false);
const [selected, setSelected] = useState({});
const [filterKey, setFilterKey] = useState(filterMap.thismonth);
const [categories, setCategories] = useState([]);
useHotkeys(addShortcutKey, () => !isLoading && setShow(true));

const { data = [], mutate, isLoading } = useSWR(getApiUrl(filterKey, 'income'));
const { data = [], mutate, isLoading } = useSWR(getApiUrl(filterKey, 'income', categories));

const onHide = () => setShow(false);
const onEdit = (selected) => {
Expand Down Expand Up @@ -109,7 +110,7 @@ export default function Income({ user }) {

<div className="h-ful mb-20">
<div className="mb-2 flex justify-between">
<h1 className="mr-3 mb-2 text-2xl font-extrabold text-black max-sm:mb-4 max-sm:ml-[45px]">Income</h1>
<h1 className="max-sm:ml-[45px] mb-2 mr-3 text-2xl font-extrabold text-black max-sm:mb-4">Income</h1>
</div>

<h2 className="mb-4 text-black">Summary</h2>
Expand Down Expand Up @@ -145,6 +146,8 @@ export default function Income({ user }) {
onFilterChange={(filterKey) => {
setFilterKey(filterKey);
}}
onCategoryFilterChange={setCategories}
categories={categories}
filterKey={filterKey}
isLoading={isLoading}
data={data}
Expand Down
7 changes: 5 additions & 2 deletions pages/app/investments.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ export default function Investments({ user }) {
const [show, setShow] = useState(false);
const [selected, setSelected] = useState({});
const [filterKey, setFilterKey] = useState(filterMap.thismonth);
const [categories, setCategories] = useState([]);
useHotkeys(addShortcutKey, () => !isLoading && setShow(true));

const { data = [], mutate, isLoading } = useSWR(getApiUrl(filterKey, 'investments'));
const { data = [], mutate, isLoading } = useSWR(getApiUrl(filterKey, 'investments', categories));

const onHide = () => setShow(false);
const onEdit = (selected) => {
Expand Down Expand Up @@ -112,7 +113,7 @@ export default function Investments({ user }) {

<div className="h-ful mb-20">
<div className="mb-2 flex justify-between">
<h1 className="mr-3 mb-2 text-2xl font-extrabold text-black max-sm:mb-4 max-sm:ml-[45px]">Investments</h1>
<h1 className="max-sm:ml-[45px] mb-2 mr-3 text-2xl font-extrabold text-black max-sm:mb-4">Investments</h1>
</div>

<h2 className="mb-4 text-black">Summary</h2>
Expand Down Expand Up @@ -148,6 +149,8 @@ export default function Investments({ user }) {
onFilterChange={(filterKey) => {
setFilterKey(filterKey);
}}
onCategoryFilterChange={setCategories}
categories={categories}
filterKey={filterKey}
isLoading={isLoading}
data={data}
Expand Down

1 comment on commit ce41f75

@vercel
Copy link

@vercel vercel bot commented on ce41f75 Jun 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.