Skip to content

Commit

Permalink
fix default cost in import
Browse files Browse the repository at this point in the history
  • Loading branch information
paolini committed Dec 3, 2024
1 parent 85f7ce6 commit e5da214
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 15 deletions.
3 changes: 1 addition & 2 deletions app/admin/cost/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use client"
import { useQuery, gql, useMutation } from '@apollo/client'
import { useState } from 'react'
import { useQuery, gql } from '@apollo/client'

import Provider from '../../components/Provider'
import Loading from '../../components/Loading'
Expand Down
30 changes: 22 additions & 8 deletions app/admin/import/page.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
"use client"
import { gql, useMutation } from '@apollo/client'
import { gql, useMutation, useQuery } from '@apollo/client'
import { useState } from 'react'
import moment from 'moment-timezone'

import Provider from '../../components/Provider'
import Button from '../../components/Button'
import Loading from '../../components/Loading'
import Error from '../../components/Error'
import { myDate } from '../../utils'

const SAVE_TRANSACTION = gql`
mutation SaveTransaction($_id: String, $timestamp: String, $email: String, $count: Int, $amountCents: Int, $description: String) {
transaction(_id: $_id, timestamp: $timestamp, email: $email, count: $count, amountCents: $amountCents, description: $description)
}`

const GET_COST = gql`
query GetCost {
cost
}`


export default function ImportPage({}) {
return <Provider>
<ImportWidget />
Expand All @@ -35,7 +44,7 @@ type Variables = {
description?: string
}

function parseRow(mapping: Mapping, cols: COLS) {
function parseRow(mapping: Mapping, cols: COLS, COST: number) {
let error = ''
const mapped = Object.fromEntries(headers.map(h => {
let val = mapping[h]
Expand All @@ -60,7 +69,7 @@ function parseRow(mapping: Mapping, cols: COLS) {
const count = parseInt(mapped.count)
if (`${count}` !== mapped.count) error ||= `invalid count ${mapped.count}`
variables.count = count
if (!mapped.amount) variables.amountCents = -count * 20
if (!mapped.amount) variables.amountCents = -count * COST
}

if (mapped.amount) {
Expand All @@ -75,7 +84,7 @@ function parseRow(mapping: Mapping, cols: COLS) {
variables.email = mapped.email
if (!mapped.email || !mapped.email.includes('@')) error ||= `invalid email ${mapped.email}`

variables.description = mapped.description || `imported on ${new Date().toISOString()}`
variables.description = mapped.description || `importato il ${myDate(new Date().toISOString())}`

return {
...variables,
Expand All @@ -88,13 +97,18 @@ function parseRow(mapping: Mapping, cols: COLS) {
}

function ImportWidget() {
const {loading: loadingCost, error: errorCost, data: dataCost} = useQuery(GET_COST)
const COST = dataCost?.cost
const [submitTransaction, transactionMutation] = useMutation(SAVE_TRANSACTION, {
refetchQueries: ["GetTransactions"]})
const [table, setTable] = useState<RowType[]>([])
const [mapping, setMapping] = useState<Mapping>({})

const ncols = table.reduce((max, el) => Math.max(el.cols.length,max), 0)

if (loadingCost) return <Loading />
if (errorCost) return <Error error={errorCost}/>

return <>
{ table.length === 0 && <p>
Copia le righe dal tuo foglio di calcolo
Expand Down Expand Up @@ -146,16 +160,16 @@ function ImportWidget() {
</tr>
</thead>
<tbody>
{table.map((row, i)=> ({row, i, parse: parseRow(mapping, row.cols)})).map(item =>
{table.map((row, i)=> ({row, i, parse: parseRow(mapping, row.cols, COST)})).map(item =>
<tr key={item.i}>
<td>{item.row.state || item.parse.error || 'valid'}</td>
<td className={"bg-gray-300 " + (item.row.state || item.parse.error ? (item.row.state==="imported" ? "text-blue-500" : "text-red-500") : "text-green-500")}>{item.row.state || item.parse.error || 'valid'}</td>
<td>{new Date(item.parse.timestamp||'').toLocaleString()}</td>
<td>{item.parse.count}</td>
<td>{item.parse.amountCents}</td>
<td>{item.parse.email}</td>
<td>{item.parse.description}</td>
{item.row.cols.map((cell, j) =>
<td key={j}>{cell}</td>
<td className="bg-gray-300" key={j}>{cell}</td>
)}
</tr>
)}
Expand All @@ -181,7 +195,7 @@ function ImportWidget() {
setTable(table => [...table, row])
continue
}
const parse = parseRow(mapping, row.cols)
const parse = parseRow(mapping, row.cols, COST)
if (parse.error) {
setTable(table => [...table, {
state: parse.error,
Expand Down
2 changes: 1 addition & 1 deletion app/components/Balance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function Balance() {
if (loading) return <Loading />
if (error) return <Error error={error}/>
return <div>
<p>Bilancio complessivo: <Amount prefix="€" cents={data.balance.cents}/> per <b>{data.balance.count}</b> caffé</p>
<p>Bilancio complessivo: <Amount prefix="€" cents={data.balance.cents}/>, caffé: <b>{data.balance.count}</b></p>
</div>
}

2 changes: 1 addition & 1 deletion app/components/Credit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function Credit() {
if (loading) return <Loading />
if (error) return <Error error={error}/>
return <div>
<p>Il tuo credito: <Amount prefix="€" cents={data.credit.cents}/>, caffé: {data.credit.count}</p>
<p>Il tuo credito: <Amount prefix="€" cents={data.credit.cents}/>, caffé: <b>{data.credit.count}</b></p>
</div>
}

Expand Down
2 changes: 1 addition & 1 deletion app/components/Transactions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function Transactions() {
<thead>
<tr>
<th colSpan={2}>quando</th>
<th>caffé</th>
<th>#</th>
<th></th>
<th>descrizione</th>
<th>tessera</th>
Expand Down
2 changes: 1 addition & 1 deletion app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export default function Home() {
function Dashboard() {
return <main>
<CoffeeForm />
<Credit />
<Balance />
<Credit />
<Transactions />
</main>
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dm-coffee",
"version": "1.1.1",
"version": "1.1.2",
"private": true,
"scripts": {
"dev": "next dev",
Expand Down

0 comments on commit e5da214

Please sign in to comment.