Skip to content

Commit

Permalink
Added analytics and new type system
Browse files Browse the repository at this point in the history
  • Loading branch information
thedanielforum committed Oct 20, 2021
1 parent 1b77290 commit 62aec1f
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 31 deletions.
31 changes: 18 additions & 13 deletions components/DailySpendCard.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
import { Fragment, memo } from "react";
import { DateTime } from "luxon";
import { gql, useQuery } from "@apollo/client";
import { DateTime } from "luxon";

const GET_AMOUNT_AVG = gql`
query totalTransactionsAmountAverage($from: DateTime!) {
aggregateTransaction(filter: { when: { gt: $from } }) {
amountAvg
const GET_AMOUNT_MAX = gql`
query totalTransactionsSum($from: DateTime!) {
aggregateTransaction(
filter: { and: { when: { ge: $from }, type: { eq: EXPENSE } } }
) {
amountSum
}
}
`;

const DailySpendCard: React.FC = () => {
const { loading, error, data } = useQuery(GET_AMOUNT_AVG, {
variables: { from: DateTime.local().startOf("month") },
const { loading, error, data } = useQuery(GET_AMOUNT_MAX, {
variables: { from: DateTime.local().startOf("month").toString() },
});
if (error) {
console.error(error);
}

const days = Math.ceil(
DateTime.local().diff(DateTime.local().startOf("month"), ["days"]).days
);

const getAmount = (): number => {
if (
data &&
data.aggregateTransaction &&
data.aggregateTransaction.amountAvg
data.aggregateTransaction.amountSum
) {
return data.aggregateTransaction.amountAvg;
return Math.ceil(data.aggregateTransaction.amountSum / days);
}
return 0;
};
Expand All @@ -42,7 +48,9 @@ const DailySpendCard: React.FC = () => {
</div>
) : (
<Fragment>
<span className="h2 mb-0">{getAmount()}</span>
<span className="h2 mb-0">
{getAmount().toLocaleString()}
</span>
{false && (
<span
className="badge bg-danger-soft mt-n1"
Expand All @@ -54,9 +62,6 @@ const DailySpendCard: React.FC = () => {
</Fragment>
)}
</div>
<div className="col-auto">
<span className="h2 fe fe-dollar-sign text-muted mb-0"></span>
</div>
</div>
</div>
</div>
Expand Down
2 changes: 2 additions & 0 deletions components/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface InputProps {
touched: { [key: string]: boolean };
errors: { [key: string]: string };
type: "text" | "number" | "date";
pattern?: string;
label?: string;
disabled?: boolean;
readOnly?: boolean;
Expand All @@ -29,6 +30,7 @@ const Input: React.FC<InputProps & FieldInputProps<any>> = (props) => {
type={props.type}
name={props.name}
value={props.value || ""}
pattern={props.pattern}
onChange={props.onChange}
onBlur={props.onBlur}
disabled={props.disabled}
Expand Down
15 changes: 12 additions & 3 deletions components/NewTransaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@ import NewTransactionForm, {
const ADD_TRANSACTION_MUTATION = gql`
mutation addTransaction(
$user: UserRef!
$type: TransactionType!
$amount: Float!
$when: DateTime!
$category: CategoryRef!
) {
addTransaction(
input: { user: $user, amount: $amount, when: $when, category: $category }
input: {
user: $user
type: $type
amount: $amount
when: $when
category: $category
}
) {
transaction {
id
Expand Down Expand Up @@ -60,13 +67,15 @@ const NewTransaction: React.FC = () => {
user: {
id: user?.id,
},
type: "EXPENSE",
amount: Number(values.amount),
when: DateTime.fromISO(values.when),
category: {
id: values.category,
},
},
});
// analytics.logEvent("add_transaction");
setSubmitting(false);
setShow(false);
} catch (err) {
Expand All @@ -81,7 +90,7 @@ const NewTransaction: React.FC = () => {
className="btn btn-primary lift"
onClick={() => setShow(true)}
>
New Transaction
New Expense
</button>
<Modal
show={show}
Expand All @@ -91,7 +100,7 @@ const NewTransaction: React.FC = () => {
>
<div className="modal-card card">
<div className="card-header">
<h4 className="card-header-title">Reigster Transaction</h4>
<h4 className="card-header-title">Reigster Expense</h4>
</div>
<div className="card-body" style={{ maxHeight: "none" }}>
<NewTransactionForm
Expand Down
1 change: 0 additions & 1 deletion components/RecentTransactions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ const RecentTransactions: React.FC = () => {
showCancelButton: true,
confirmButtonText: "Delete",
reverseButtons: true,
showLoaderOnConfirm: true,
});
if (isConfirmed) {
await deleteTransaction({
Expand Down
11 changes: 5 additions & 6 deletions components/TotalSpendCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import { gql, useQuery } from "@apollo/client";

const GET_AMOUNT_MAX = gql`
query totalTransactionsSum($from: DateTime!) {
aggregateTransaction(filter: { when: { gt: $from } }) {
aggregateTransaction(
filter: { and: { when: { ge: $from }, type: { eq: EXPENSE } } }
) {
amountSum
}
}
`;

const TotalSpendCard: React.FC = () => {
const { loading, error, data } = useQuery(GET_AMOUNT_MAX, {
variables: { from: DateTime.local().startOf("month") },
variables: { from: DateTime.local().startOf("month").toString() },
});
if (error) {
console.error(error);
Expand All @@ -24,7 +26,7 @@ const TotalSpendCard: React.FC = () => {
data.aggregateTransaction &&
data.aggregateTransaction.amountSum
) {
return data.aggregateTransaction.amountSum;
return data.aggregateTransaction.amountSum.toLocaleString();
}
return 0;
};
Expand All @@ -46,9 +48,6 @@ const TotalSpendCard: React.FC = () => {
<span className="h2 mb-0">{getAmount()}</span>
)}
</div>
<div className="col-auto">
<span className="h2 fe fe-dollar-sign text-muted mb-0"></span>
</div>
</div>
</div>
</div>
Expand Down
16 changes: 9 additions & 7 deletions components/TransactionsCountCard.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
import { memo } from "react";
import { gql, useQuery } from "@apollo/client";
import { DateTime } from "luxon";

const GET_TOTOAL_TRANSACTIONS = gql`
query totalTransactions {
aggregateTransaction {
query totalTransactions($from: DateTime!) {
aggregateTransaction(
filter: { and: { when: { ge: $from }, type: { eq: EXPENSE } } }
) {
count
}
}
`;

const TransactionsCountCard: React.FC = () => {
const { loading, error, data } = useQuery(GET_TOTOAL_TRANSACTIONS);
const { loading, error, data } = useQuery(GET_TOTOAL_TRANSACTIONS, {
variables: { from: DateTime.local().startOf("month").toString() },
});
if (error) {
console.error(error);
}

const getCount = (): number => {
if (data && data.aggregateTransaction && data.aggregateTransaction.count) {
return data.aggregateTransaction.count;
return data.aggregateTransaction.count.toLocaleString();
}
return 0;
};
Expand All @@ -39,9 +44,6 @@ const TransactionsCountCard: React.FC = () => {
<span className="h2 mb-0">{getCount()}</span>
)}
</div>
<div className="col-auto">
<span className="h2 fe fe-clock text-muted mb-0"></span>
</div>
</div>
</div>
</div>
Expand Down
3 changes: 2 additions & 1 deletion forms/NewTransactionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ const NewTransactionForm: React.FC<NewTransactionFormProps> = (props) => {
touched={touched}
errors={errors}
name="amount"
type="text"
type="number"
pattern="\d*"
label="Amount"
placeholder="10,000"
tabIndex={0}
Expand Down
2 changes: 2 additions & 0 deletions lib/initAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ const initAuth = () => {
databaseURL: process.env.NEXT_PUBLIC_FIREBASE_DATABASE_URL,
},
firebaseClientInitConfig: {
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
apiKey: process.env.NEXT_PUBLIC_FIREBASE_PUBLIC_API_KEY,
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
databaseURL: process.env.NEXT_PUBLIC_FIREBASE_DATABASE_URL,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
measurementId: process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID,
},
cookies: {
name: "Ikura",
Expand Down
15 changes: 15 additions & 0 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import "../styles/theme.scss";
import "sweetalert2/src/sweetalert2.scss";
import "firebase/analytics";
import type { AppProps } from "next/app";
import { useEffect } from "react";
import Head from "next/head";
import { ApolloProvider } from "@apollo/client";
import { useApollo } from "../lib/apolloClient";
import initAuth from "../lib/initAuth";
import { useAuthUser, withAuthUser } from "next-firebase-auth";
import firebase from "firebase/app";
import Navbar from "../components/Navbar";
import Footer from "../components/Footer";

Expand All @@ -15,6 +18,18 @@ function CustomApp({ Component, pageProps }: AppProps) {
const user = useAuthUser();
const apolloClient = useApollo(pageProps, user);

useEffect(() => {
if (firebase.apps.length > 0) {
const analytics = firebase.analytics();

firebase.auth().onIdTokenChanged((user) => {
if (user) {
analytics.setUserId(user.uid);
}
});
}
}, [firebase.apps.length]);

return (
<ApolloProvider client={apolloClient}>
<Head>
Expand Down
3 changes: 3 additions & 0 deletions pages/settings/categories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import CategoryForm, { CategoryFormValues } from "../../forms/CategoryForm";
const ADD_CATEGORY_MUTATION = gql`
mutation addCategory(
$user: UserRef!
$type: CategoryType!
$title: String!
$createdAt: DateTime!
$description: String
Expand All @@ -20,6 +21,7 @@ const ADD_CATEGORY_MUTATION = gql`
addCategory(
input: {
user: $user
type: $type
title: $title
createdAt: $createdAt
description: $description
Expand Down Expand Up @@ -68,6 +70,7 @@ const SettingsCategories: NextPage = () => {
user: {
id: user?.id,
},
type: "PRIVATE",
title: values.title,
description: values.description,
createdAt: DateTime.now(),
Expand Down

1 comment on commit 62aec1f

@vercel
Copy link

@vercel vercel bot commented on 62aec1f Oct 20, 2021

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.