From e5da214bf375526584dc8f326d85561fac247b16 Mon Sep 17 00:00:00 2001 From: Emanuele Paolini Date: Tue, 3 Dec 2024 07:44:26 +0100 Subject: [PATCH] fix default cost in import --- app/admin/cost/page.tsx | 3 +-- app/admin/import/page.tsx | 30 ++++++++++++++++++++++-------- app/components/Balance.tsx | 2 +- app/components/Credit.tsx | 2 +- app/components/Transactions.tsx | 2 +- app/page.tsx | 2 +- package.json | 2 +- 7 files changed, 28 insertions(+), 15 deletions(-) diff --git a/app/admin/cost/page.tsx b/app/admin/cost/page.tsx index 632fd47..3daa0f8 100644 --- a/app/admin/cost/page.tsx +++ b/app/admin/cost/page.tsx @@ -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' diff --git a/app/admin/import/page.tsx b/app/admin/import/page.tsx index fdd714b..c1b0cc4 100644 --- a/app/admin/import/page.tsx +++ b/app/admin/import/page.tsx @@ -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 @@ -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] @@ -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) { @@ -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, @@ -88,6 +97,8 @@ 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([]) @@ -95,6 +106,9 @@ function ImportWidget() { const ncols = table.reduce((max, el) => Math.max(el.cols.length,max), 0) + if (loadingCost) return + if (errorCost) return + return <> { table.length === 0 &&

Copia le righe dal tuo foglio di calcolo @@ -146,16 +160,16 @@ function ImportWidget() { - {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 => - {item.row.state || item.parse.error || 'valid'} + {item.row.state || item.parse.error || 'valid'} {new Date(item.parse.timestamp||'').toLocaleString()} {item.parse.count} {item.parse.amountCents} {item.parse.email} {item.parse.description} {item.row.cols.map((cell, j) => - {cell} + {cell} )} )} @@ -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, diff --git a/app/components/Balance.tsx b/app/components/Balance.tsx index 9698157..adfcc46 100644 --- a/app/components/Balance.tsx +++ b/app/components/Balance.tsx @@ -19,7 +19,7 @@ export default function Balance() { if (loading) return if (error) return return

-

Bilancio complessivo: per {data.balance.count} caffé

+

Bilancio complessivo: , caffé: {data.balance.count}

} diff --git a/app/components/Credit.tsx b/app/components/Credit.tsx index 3c6bccf..d44af87 100644 --- a/app/components/Credit.tsx +++ b/app/components/Credit.tsx @@ -17,7 +17,7 @@ export default function Credit() { if (loading) return if (error) return return
-

Il tuo credito: , caffé: {data.credit.count}

+

Il tuo credito: , caffé: {data.credit.count}

} diff --git a/app/components/Transactions.tsx b/app/components/Transactions.tsx index f9b51e1..53c971b 100644 --- a/app/components/Transactions.tsx +++ b/app/components/Transactions.tsx @@ -26,7 +26,7 @@ export default function Transactions() { quando - caffé + # € descrizione tessera diff --git a/app/page.tsx b/app/page.tsx index 01bc4d1..cc6d600 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -16,8 +16,8 @@ export default function Home() { function Dashboard() { return
- +
} diff --git a/package.json b/package.json index 8fce616..4ff7a97 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dm-coffee", - "version": "1.1.1", + "version": "1.1.2", "private": true, "scripts": { "dev": "next dev",