Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Commit

Permalink
✨ able to save changes to db
Browse files Browse the repository at this point in the history
  • Loading branch information
frenicohansen committed Feb 12, 2024
1 parent 9cdfce1 commit ebf7dd9
Show file tree
Hide file tree
Showing 24 changed files with 321 additions and 178 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"files.trimTrailingWhitespace": true,
"editor.formatOnSave": false,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "always"
},
"[prisma]": {
"editor.formatOnSave": true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
Warnings:
- A unique constraint covering the columns `[name,budgetCategoryGroupId]` on the table `budget_category` will be added. If there are existing duplicate values, this will fail.
- A unique constraint covering the columns `[name,budgetId]` on the table `budget_category_group` will be added. If there are existing duplicate values, this will fail.
*/
-- CreateIndex
CREATE UNIQUE INDEX "budget_category_name_budgetCategoryGroupId_key" ON "budget_category"("name", "budgetCategoryGroupId");

-- CreateIndex
CREATE UNIQUE INDEX "budget_category_group_name_budgetId_key" ON "budget_category_group"("name", "budgetId");
2 changes: 2 additions & 0 deletions api/db/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ model BudgetCategoryGroup {
budgetCategories BudgetCategory[]
monthlyCategoryGroupActivities MonthlyCategoryGroupActivity[]
@@unique([name, budgetId])
@@index([budgetId])
@@map("budget_category_group")
}
Expand All @@ -175,6 +176,7 @@ model BudgetCategory {
monthlyBudgetPerCategories MonthlyBudgetPerCategory[]
@@unique([name, budgetCategoryGroupId])
@@index([budgetCategoryGroupId])
@@map("budget_category")
}
1 change: 1 addition & 0 deletions api/src/graphql/budgetCategories.sdl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const schema = gql`
type Query {
budgetCategories: [BudgetCategory!]! @requireAuth
budgetCategory(id: String!): BudgetCategory @requireAuth
budgetCategoriesWithNoAssignedFor(budgetId:String!, month: Int!, year: Int!): [BudgetCategory!]! @requireAuth
}
input CreateBudgetCategoryInput {
Expand Down
30 changes: 13 additions & 17 deletions api/src/graphql/monthlyBudgetPerCategories.sdl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,25 @@ export const schema = gql`
monthlyBudgetPerCategory(id: String!): MonthlyBudgetPerCategory @requireAuth
}
input CreateMonthlyBudgetPerCategoryInput {
month: Int!
year: Int!
input UpdateAssignedBudgetForCategoryInput {
assigned: Float!
budgetCategoryId: String!
}
input UpdateMonthlyBudgetPerCategoryInput {
month: Int
year: Int
assigned: Float
budgetCategoryId: String
input CreateEmptyBudgetForCategoriesInput {
categoryIds: [String!]!
month: Int!
year: Int!
}
type Mutation {
createMonthlyBudgetPerCategory(
input: CreateMonthlyBudgetPerCategoryInput!
): MonthlyBudgetPerCategory! @requireAuth
updateMonthlyBudgetPerCategory(
id: String!
input: UpdateMonthlyBudgetPerCategoryInput!
createEmptyBudgetForCategories(
input: CreateEmptyBudgetForCategoriesInput!
): PrismaCreateManyResult! @requireAuth
updateAssignedBudgetForCategory(
categoryId: String!
month: Int!
year: Int!
input: UpdateAssignedBudgetForCategoryInput!
): MonthlyBudgetPerCategory! @requireAuth
deleteMonthlyBudgetPerCategory(id: String!): MonthlyBudgetPerCategory!
@requireAuth
}
`
5 changes: 5 additions & 0 deletions api/src/graphql/prismaResult.sdl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const schema = gql`
type PrismaCreateManyResult {
count: Int!
}
`
21 changes: 21 additions & 0 deletions api/src/services/budgetCategories/budgetCategories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,27 @@ export const budgetCategory: QueryResolvers['budgetCategory'] = ({ id }) => {
})
}

export const budgetCategoriesWithNoAssignedFor: QueryResolvers['budgetCategoriesWithNoAssignedFor'] =
({ budgetId, month, year }) => {
return db.budgetCategory.findMany({
where: {
budgetCategoryGroup: {
budget: {
id: budgetId,
},
},
NOT: {
monthlyBudgetPerCategories: {
some: {
month,
year,
},
},
},
},
})
}

export const createBudgetCategory: MutationResolvers['createBudgetCategory'] =
({ input }) => {
return db.budgetCategory.create({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { MonthlyBudgetPerCategory } from '@prisma/client'
/* import type { MonthlyBudgetPerCategory } from '@prisma/client'
import {
monthlyBudgetPerCategories,
Expand Down Expand Up @@ -86,3 +86,4 @@ describe('monthlyBudgetPerCategories', () => {
}
)
})
*/
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,31 @@ export const monthlyBudgetPerCategory: QueryResolvers['monthlyBudgetPerCategory'
})
}

export const createMonthlyBudgetPerCategory: MutationResolvers['createMonthlyBudgetPerCategory'] =
export const createEmptyBudgetForCategories: MutationResolvers['createEmptyBudgetForCategories'] =
({ input }) => {
return db.monthlyBudgetPerCategory.create({
data: input,
const { categoryIds, month, year } = input
return db.monthlyBudgetPerCategory.createMany({
data: categoryIds.map((budgetCategoryId) => ({
budgetCategoryId,
month,
year,
assigned: 0,
})),
})
}

export const updateMonthlyBudgetPerCategory: MutationResolvers['updateMonthlyBudgetPerCategory'] =
({ id, input }) => {
export const updateAssignedBudgetForCategory: MutationResolvers['updateAssignedBudgetForCategory'] =
({ categoryId, month, year, input }) => {
const { assigned } = input
return db.monthlyBudgetPerCategory.update({
data: input,
where: { id },
})
}

export const deleteMonthlyBudgetPerCategory: MutationResolvers['deleteMonthlyBudgetPerCategory'] =
({ id }) => {
return db.monthlyBudgetPerCategory.delete({
where: { id },
data: { assigned },
where: {
month_year_budgetCategoryId: {
month,
year,
budgetCategoryId: categoryId,
},
},
})
}

Expand Down
3 changes: 2 additions & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"sonner": "^1.4.0",
"zod": "^3.22.4"
"zod": "^3.22.4",
"zustand": "^4.5.0"
},
"devDependencies": {
"@pandacss/dev": "^0.30.2",
Expand Down
2 changes: 2 additions & 0 deletions web/src/components/MonthlyBudgetsCell/MonthlyBudgetsCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const QUERY = gql`
) {
budget(id: $budgetId, userId: $userId) {
budgetCategoryGroups {
id
name
sortOrder
monthlyCategoryGroupActivity(month: $month, year: $year) {
Expand All @@ -28,6 +29,7 @@ export const QUERY = gql`
available
}
budgetCategories {
id
name
sortOrder
monthlyBudgetPerCategory(month: $month, year: $year) {
Expand Down
18 changes: 10 additions & 8 deletions web/src/components/MonthlyBudgetsCell/convertData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,24 @@ export const convertBudgetGQLIntoDisplayable: MonthlyBudgetGQLIntoDisplayable =
const categories = group.budgetCategories
const subRows = categories.map((category) => {
return {
id: category.id,
category: category.name,
assigned: category.monthlyBudgetPerCategory[0].assigned,
assigned: category.monthlyBudgetPerCategory[0]?.assigned || 0,
activity:
category.monthlyBudgetPerCategory[0].monthlyCategoryActivity
.activity,
category.monthlyBudgetPerCategory[0]?.monthlyCategoryActivity
.activity || 0,
available:
category.monthlyBudgetPerCategory[0].monthlyCategoryActivity
.available,
category.monthlyBudgetPerCategory[0]?.monthlyCategoryActivity
.available || 0,
}
})

return {
id: group.id,
category: group.name,
assigned: group.monthlyCategoryGroupActivity[0].assigned,
activity: group.monthlyCategoryGroupActivity[0].activity,
available: group.monthlyCategoryGroupActivity[0].available,
assigned: group.monthlyCategoryGroupActivity[0]?.assigned || 0,
activity: group.monthlyCategoryGroupActivity[0]?.activity || 0,
available: group.monthlyCategoryGroupActivity[0]?.available || 0,
subRows,
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { useState, useEffect } from 'react'
import { type Row, type Getter, Column, Table } from '@tanstack/react-table'
import { ChevronDown, ChevronRight } from 'lucide-react'

import { useMutation } from '@redwoodjs/web'

import { QUERY as FIND_THIS_MONTH_BUDGET } from 'src/components/MonthlyBudgetsCell'
import { useSelectedMonth, useSelectedYear } from 'src/lib/store'

import type { MonthlyBudget } from './columns'

import { Checkbox } from '@/ui/checkbox'
Expand Down Expand Up @@ -41,6 +46,24 @@ export const CExpand = ({ row }: { row: Row<MonthlyBudget> }) => {
)
}

const UPDATE_BUDGET = gql`
mutation UpdateAssignedBudgetForCategory(
$categoryId: String!
$month: Int!
$year: Int!
$input: UpdateAssignedBudgetForCategoryInput!
) {
updateAssignedBudgetForCategory(
categoryId: $categoryId
month: $month
year: $year
input: $input
) {
id
}
}
`

export const CEditableCurrency = ({
getValue,
row,
Expand Down Expand Up @@ -78,13 +101,29 @@ export const CEditableCurrency = ({
updateData: (rowIndex: number[], columnId: string, value: string) => void
}

const [updateAssignedBudgetForCategory] = useMutation(UPDATE_BUDGET, {
refetchQueries: [FIND_THIS_MONTH_BUDGET],
})
const month = useSelectedMonth()
const year = useSelectedYear()

const onBlur = () => {
tableMeta.updateData(
[row.getParentRow().index, row.index],
column.id,
value
)
setValue(format(value))
if (!row.getCanExpand()) {
updateAssignedBudgetForCategory({
variables: {
categoryId: row.original.id,
month,
year,
input: { assigned: convertToFloat(value) },
},
})
}
}

const onFocus = (e: React.FocusEvent<HTMLInputElement, Element>): void => {
Expand Down
3 changes: 2 additions & 1 deletion web/src/components/Spreadsheet/columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import {
CCategory,
CCurrency,
CEditableCurrency,
} from './cell'
} from './cells'
import { HExpand, HCheckbox } from './header'

// This type is used to define the shape of our data.
// You can use a Zod schema here if you want.
export type MonthlyBudget = {
id: string
category: string
assigned: number
activity: number
Expand Down
Loading

0 comments on commit ebf7dd9

Please sign in to comment.