Convert other pages to composition API

This commit is contained in:
Jan Bader 2022-02-14 22:24:42 +00:00
parent d11c0036b5
commit 0a030eaee1
9 changed files with 196 additions and 231 deletions

View File

@ -1,5 +1,5 @@
<script lang="ts" setup> <script lang="ts" setup>
import { ref } from "vue" import { computed, ref } from "vue"
import Autocomplete, { Suggestion } from '../components/Autocomplete.vue' import Autocomplete, { Suggestion } from '../components/Autocomplete.vue'
import Currency from "../components/Currency.vue"; import Currency from "../components/Currency.vue";
import TransactionRow from "../components/TransactionRow.vue"; import TransactionRow from "../components/TransactionRow.vue";
@ -22,10 +22,10 @@ function saveTransaction(e: MouseEvent) {
POST("/transaction/new", JSON.stringify({ POST("/transaction/new", JSON.stringify({
budget_id: props.budgetid, budget_id: props.budgetid,
account_id: props.accountid, account_id: props.accountid,
date: TransactionDate, date: TransactionDate.value,
payee: Payee, payee: Payee.value,
category: Category, category: Category.value,
memo: Memo, memo: Memo.value,
amount: Amount, amount: Amount,
state: "Uncleared" state: "Uncleared"
})) }))

View File

@ -1,10 +1,8 @@
<script lang="ts"> <script lang="ts" setup>
import { defineComponent } from "vue"; import { onMounted } from 'vue';
export default defineComponent({ onMounted(() => {
mounted() { document.title = "Budgeteer - Admin";
document.title = "Budgeteer - Admin";
},
}) })
</script> </script>

View File

@ -1,20 +1,25 @@
<script lang="ts"> <script lang="ts" setup>
import { mapState } from "pinia"
import { defineComponent } from "vue"
import Currency from "../components/Currency.vue" import Currency from "../components/Currency.vue"
import { useBudgetsStore } from "../stores/budget" import { useBudgetsStore } from "../stores/budget"
import { useAccountStore } from "../stores/budget-account" import { useAccountStore } from "../stores/budget-account"
import { useSettingsStore } from "../stores/settings" import { useSettingsStore } from "../stores/settings"
export default defineComponent({ const props = defineProps<{
props: ["budgetid", "accountid"], budgetid: string,
components: { Currency }, accountid: string,
computed: { }>();
...mapState(useSettingsStore, ["ExpandMenu"]),
...mapState(useBudgetsStore, ["CurrentBudgetName", "CurrentBudgetID"]), const ExpandMenu = useSettingsStore().Menu.Expand;
...mapState(useAccountStore, ["OnBudgetAccounts", "OnBudgetAccountsBalance", "OffBudgetAccounts", "OffBudgetAccountsBalance"])
} const budgetStore = useBudgetsStore();
}) const CurrentBudgetName = budgetStore.CurrentBudgetName;
const CurrentBudgetID = budgetStore.CurrentBudgetID;
const accountStore = useAccountStore();
const OnBudgetAccounts = accountStore.OnBudgetAccounts;
const OffBudgetAccounts = accountStore.OffBudgetAccounts;
const OnBudgetAccountsBalance = accountStore.OnBudgetAccountsBalance;
const OffBudgetAccountsBalance = accountStore.OffBudgetAccountsBalance;
</script> </script>
<template> <template>

View File

@ -1,68 +1,55 @@
<script lang="ts"> <script lang="ts" setup>
import { mapState } from "pinia"; import { computed, defineProps, onMounted, PropType, watch, watchEffect } from "vue";
import { defineComponent, PropType } from "vue";
import Currency from "../components/Currency.vue"; import Currency from "../components/Currency.vue";
import { useBudgetsStore } from "../stores/budget"; import { useBudgetsStore } from "../stores/budget";
import { Category, useAccountStore } from "../stores/budget-account"; import { useAccountStore } from "../stores/budget-account";
interface Date { interface Date {
Year: number, Year: number,
Month: number, Month: number,
} }
export default defineComponent({ const props = defineProps<{
props: { budgetid: string,
budgetid: {} as PropType<string>, year: string,
year: {} as PropType<number>, month: string,
month: {} as PropType<number>, }>()
},
computed: { const budgetsStore = useBudgetsStore();
...mapState(useBudgetsStore, ["CurrentBudgetID"]), const CurrentBudgetID = computed(() => budgetsStore.CurrentBudgetID);
Categories() : Category[] {
const accountStore = useAccountStore(); const accountStore = useAccountStore();
return [...accountStore.CategoriesForMonth(this.selected.Year, this.selected.Month)]; //const categoriesForMonth = accountStore.CategoriesForMonth;
}, const Categories = computed(() => {
previous() : Date { const yearMap = accountStore.Months.get(selected.value.Year);
return { const monthMap = yearMap?.get(selected.value.Month);
Year: new Date(this.selected.Year, this.selected.Month - 1, 1).getFullYear(), console.log("MTH", monthMap)
Month: new Date(this.selected.Year, this.selected.Month - 1, 1).getMonth(), const CategoriesForMonth = [ ...monthMap?.values() || [] ];
}; console.log("YAY?")
}, return CategoriesForMonth;
current() : Date { //return [...categoriesForMonth(selected.value.Year, selected.value.Month)];
return { });
Year: new Date().getFullYear(), const previous = computed(() => ({
Month: new Date().getMonth(), Year: new Date(selected.value.Year, selected.value.Month - 1, 1).getFullYear(),
}; Month: new Date(selected.value.Year, selected.value.Month - 1, 1).getMonth(),
}, }));
selected() : Date { const current = computed(() => ({
return { Year: new Date().getFullYear(),
Year: this.year ?? this.current.Year, Month: new Date().getMonth(),
Month: Number(this.month ?? this.current.Month) + 1 }));
} const selected = computed(() => ({
}, Year: Number(props.year) ?? current.value.Year,
next() : Date { Month: Number(props.month ?? current.value.Month) + 1
return { }));
Year: new Date(this.selected.Year, Number(this.month) + 1, 1).getFullYear(), const next = computed(() => ({
Month: new Date(this.selected.Year, Number(this.month) + 1, 1).getMonth(), Year: new Date(selected.value.Year, Number(props.month) + 1, 1).getFullYear(),
}; Month: new Date(selected.value.Year, Number(props.month) + 1, 1).getMonth(),
} }));
},
mounted() : Promise<void> { watchEffect(() => {
document.title = "Budgeteer - Budget for " + this.selected.Month + "/" + this.selected.Year; if (props.year != undefined && props.month != undefined)
return useAccountStore().FetchMonthBudget(this.budgetid ?? "", this.selected.Year, this.selected.Month); return useAccountStore().FetchMonthBudget(props.budgetid ?? "", Number(props.year), Number(props.month));
}, });
watch: {
year() {
if (this.year != undefined && this.month != undefined)
return useAccountStore().FetchMonthBudget(this.budgetid ?? "", this.year, this.month);
},
month() {
if (this.year != undefined && this.month != undefined)
return useAccountStore().FetchMonthBudget(this.budgetid ?? "", this.year, this.month);
},
},
components: { Currency }
})
/*{{define "title"}} /*{{define "title"}}
{{printf "Budget for %s %d" .Date.Month .Date.Year}} {{printf "Budget for %s %d" .Date.Month .Date.Year}}

View File

@ -1,17 +1,13 @@
<script lang="ts"> <script lang="ts" setup>
import NewBudget from '../dialogs/NewBudget.vue'; import NewBudget from '../dialogs/NewBudget.vue';
import Card from '../components/Card.vue'; import Card from '../components/Card.vue';
import { defineComponent } from 'vue';
import { mapState } from 'pinia';
import { useSessionStore } from '../stores/session'; import { useSessionStore } from '../stores/session';
export default defineComponent({ const props = defineProps<{
props: ["budgetid"], budgetid: string,
components: { NewBudget, Card }, }>();
computed: {
...mapState(useSessionStore, ["BudgetsList"]), const BudgetsList = useSessionStore().BudgetsList;
}
})
</script> </script>
<template> <template>

View File

@ -1,9 +1,4 @@
<script lang="ts"> <script lang="ts" setup>
import { defineComponent } from 'vue';
export default defineComponent({
})
</script> </script>
<template> <template>

View File

@ -1,46 +1,48 @@
<script lang="ts"> <script lang="ts" setup>
import { defineComponent } from "vue"; import { onMounted, ref } from "vue";
import { useRouter } from "vue-router";
import { useSessionStore } from "../stores/session"; import { useSessionStore } from "../stores/session";
export default defineComponent({ const error = ref("");
data() { const login = ref({ user: "", password: "" });
return {
error: "",
login: {
user: "",
password: ""
},
showPassword: false
}
},
mounted() {
document.title = "Budgeteer - Login";
},
methods: {
formSubmit(e : MouseEvent) {
e.preventDefault();
useSessionStore().login(this.$data.login)
.then(x => {
this.$data.error = "";
this.$router.replace("/dashboard");
})
.catch(x => this.$data.error = "The entered credentials are invalid!");
// TODO display invalidCredentials onMounted(() => {
// TODO redirect to dashboard on success document.title = "Budgeteer - Login";
} });
}
}) function formSubmit(e: MouseEvent) {
e.preventDefault();
useSessionStore().login(login)
.then(x => {
error.value = "";
useRouter().replace("/dashboard");
})
.catch(x => error.value = "The entered credentials are invalid!");
// TODO display invalidCredentials
// TODO redirect to dashboard on success
}
</script> </script>
<template> <template>
<div> <div>
<input type="text" v-model="login.user" placeholder="Username" class="border-2 border-black rounded-lg block px-2 my-2 w-48" /> <input
<input type="password" v-model="login.password" placeholder="Password" class="border-2 border-black rounded-lg block px-2 my-2 w-48" /> type="text"
</div> v-model="login.user"
<div>{{ error }}</div> placeholder="Username"
<button type="submit" @click="formSubmit" class="bg-blue-300 rounded-lg p-2 w-48">Login</button> class="border-2 border-black rounded-lg block px-2 my-2 w-48"
<p> />
New user? <router-link to="/register">Register</router-link> instead! <input
</p> type="password"
v-model="login.password"
placeholder="Password"
class="border-2 border-black rounded-lg block px-2 my-2 w-48"
/>
</div>
<div>{{ error }}</div>
<button type="submit" @click="formSubmit" class="bg-blue-300 rounded-lg p-2 w-48">Login</button>
<p>
New user?
<router-link to="/register">Register</router-link>instead!
</p>
</template> </template>

View File

@ -1,31 +1,20 @@
<script lang="ts"> <script lang="ts" setup>
import { defineComponent } from 'vue'; import { ref } from 'vue';
import { useSessionStore } from '../stores/session'; import { useSessionStore } from '../stores/session';
export default defineComponent({ const error = ref("");
data() { const login = ref({ email: "", password: "", name: "" });
return { const showPassword = ref(false);
showPassword: false,
error: "",
login: {
email: "",
password: "",
name: "",
}
}
},
methods: {
formSubmit (e : FormDataEvent) {
e.preventDefault();
useSessionStore().register(this.$data.login)
.then(() => this.$data.error = "")
.catch(() => this.$data.error = "Something went wrong!");
// TODO display invalidCredentials function formSubmit(e: FormDataEvent) {
// TODO redirect to dashboard on success e.preventDefault();
} useSessionStore().register(login)
} .then(() => error.value = "")
}) .catch(() => error.value = "Something went wrong!");
// TODO display invalidCredentials
// TODO redirect to dashboard on success
}
</script> </script>
<template> <template>
@ -38,30 +27,35 @@ export default defineComponent({
<v-text-field v-model="login.name" type="text" label="Name" /> <v-text-field v-model="login.name" type="text" label="Name" />
</v-col> </v-col>
<v-col cols="6"> <v-col cols="6">
<v-text-field v-model="login.password" label="Password" <v-text-field
v-model="login.password"
label="Password"
:append-icon="showPassword ? 'mdi-eye' : 'mdi-eye-off'" :append-icon="showPassword ? 'mdi-eye' : 'mdi-eye-off'"
:type="showPassword ? 'text' : 'password'" :type="showPassword ? 'text' : 'password'"
@click:append="showPassword = showPassword" @click:append="showPassword = showPassword"
:error-message="error" :error-message="error"
error-count="2" error-count="2"
error /> error
/>
</v-col> </v-col>
<v-col cols="6"> <v-col cols="6">
<v-text-field v-model="login.password" label="Repeat password" <v-text-field
v-model="login.password"
label="Repeat password"
:append-icon="showPassword ? 'mdi-eye' : 'mdi-eye-off'" :append-icon="showPassword ? 'mdi-eye' : 'mdi-eye-off'"
:type="showPassword ? 'text' : 'password'" :type="showPassword ? 'text' : 'password'"
@click:append="showPassword = showPassword" @click:append="showPassword = showPassword"
:error-message="error" :error-message="error"
error-count="2" error-count="2"
error /> error
/>
</v-col> </v-col>
</v-row> </v-row>
<div class="form-group"> <div class="form-group">{{ error }}</div>
{{ error }}
</div>
<v-btn type="submit" @click="formSubmit">Register</v-btn> <v-btn type="submit" @click="formSubmit">Register</v-btn>
<p> <p>
Existing user? <router-link to="/login">Login</router-link> instead! Existing user?
</p> <router-link to="/login">Login</router-link>instead!
</p>
</v-container> </v-container>
</template> </template>

View File

@ -1,65 +1,56 @@
<script lang="ts"> <script lang="ts" setup>
import { defineComponent } from "vue" import { computed, defineComponent, onMounted, ref } from "vue"
import { useRouter } from "vue-router";
import { DELETE, POST } from "../api"; import { DELETE, POST } from "../api";
import { useBudgetsStore } from "../stores/budget"; import { useBudgetsStore } from "../stores/budget";
import { useSessionStore } from "../stores/session"; import { useSessionStore } from "../stores/session";
export default defineComponent({ const transactionsFile = ref<File | undefined>(undefined);
data() { const assignmentsFile = ref<File | undefined>(undefined);
return {
transactionsFile: undefined as File | undefined,
assignmentsFile: undefined as File | undefined
}
},
computed: {
filesIncomplete() : boolean {
return this.$data.transactionsFile == undefined || this.$data.assignmentsFile == undefined;
}
},
mounted() {
document.title = "Budgeteer - Settings";
},
methods: {
gotAssignments(e : Event) {
const input = (<HTMLInputElement>e.target);
if(input.files != null)
this.$data.assignmentsFile = input.files[0];
},
gotTransactions(e : Event) {
const input = (<HTMLInputElement>e.target);
if(input.files != null)
this.$data.transactionsFile = input.files[0];
},
deleteBudget() {
const currentBudgetID = useBudgetsStore().CurrentBudgetID;
if (currentBudgetID == null)
return;
DELETE("/budget/" + currentBudgetID); const filesIncomplete = computed(() => transactionsFile.value == undefined || assignmentsFile.value == undefined);
onMounted(() => {
document.title = "Budgeteer - Settings";
});
const budgetStore = useSessionStore(); function gotAssignments(e: Event) {
budgetStore.Budgets.delete(currentBudgetID); const input = (<HTMLInputElement>e.target);
this.$router.push("/") if (input.files != null)
}, assignmentsFile.value = input.files[0];
clearBudget() { }
const currentBudgetID = useBudgetsStore().CurrentBudgetID; function gotTransactions(e: Event) {
POST("/budget/" + currentBudgetID + "/settings/clear", null) const input = (<HTMLInputElement>e.target);
}, if (input.files != null)
cleanNegative() { transactionsFile.value = input.files[0];
// <a href="/budget/{{.Budget.ID}}/settings/clean-negative">Fix all historic negative category-balances</a> };
}, function deleteBudget() {
ynabImport() { const currentBudgetID = useBudgetsStore().CurrentBudgetID;
if (this.$data.transactionsFile == undefined || this.$data.assignmentsFile == undefined) if (currentBudgetID == null)
return return;
let formData = new FormData(); DELETE("/budget/" + currentBudgetID);
formData.append("transactions", this.$data.transactionsFile);
formData.append("assignments", this.$data.assignmentsFile); const budgetStore = useSessionStore();
const budgetStore = useBudgetsStore(); budgetStore.Budgets.delete(currentBudgetID);
budgetStore.ImportYNAB(formData); useRouter().push("/")
} };
} function clearBudget() {
}) const currentBudgetID = useBudgetsStore().CurrentBudgetID;
POST("/budget/" + currentBudgetID + "/settings/clear", null)
};
function cleanNegative() {
// <a href="/budget/{{.Budget.ID}}/settings/clean-negative">Fix all historic negative category-balances</a>
};
function ynabImport() {
if (transactionsFile.value == undefined || assignmentsFile.value == undefined)
return
let formData = new FormData();
formData.append("transactions", transactionsFile.value);
formData.append("assignments", assignmentsFile.value);
const budgetStore = useBudgetsStore();
budgetStore.ImportYNAB(formData);
};
</script> </script>
<template> <template>
@ -124,10 +115,7 @@ export default defineComponent({
</label> </label>
<v-card-actions class="justify-center"> <v-card-actions class="justify-center">
<v-btn <v-btn :disabled="filesIncomplete" @click="ynabImport">Importieren</v-btn>
:disabled="filesIncomplete"
@click="ynabImport"
>Importieren</v-btn>
</v-card-actions> </v-card-actions>
</v-card> </v-card>
</v-col> </v-col>