Skip to content

Commit

Permalink
hooked up form
Browse files Browse the repository at this point in the history
  • Loading branch information
dantownsend committed Jul 2, 2023
1 parent 3b5cd41 commit 96fde3d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
50 changes: 45 additions & 5 deletions admin_ui/src/components/Chart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,70 @@
v-else-if="chartConfig.chart_type == 'Area'"
:data="chartData"
></area-chart>

<form @submit.prevent="handleSubmit($event)" v-if="chartSchema">
<NewForm :schema="chartSchema"></NewForm>
<button>Apply</button>
</form>
</div>
</template>

<script lang="ts">
import Vue from "vue"
import NewForm from "./NewForm.vue"
import { convertFormValue } from "@/utils"
export default Vue.extend({
data() {
return { chartData: [], chartConfig: null, chartSchema: null }
return {
chartData: [],
chartConfig: null,
chartSchema: null,
chartSlug: null
}
},
components: { NewForm },
methods: {
async handleSubmit(event) {
const form = new FormData(event.target)
const json = {}
for (const i of form.entries()) {
const key = i[0]
let value = i[1]
json[key] = convertFormValue({
key,
value,
schema: this.chartSchema
})
}
this.chartData = (
await this.$store.dispatch("fetchChartData", {
chartSlug: this.chartSlug,
data: json
})
).data
}
},
async mounted() {
const chartSlug = this.$router.currentRoute.params.chartSlug
this.chartSlug = this.$router.currentRoute.params.chartSlug
this.chartData = (
await this.$store.dispatch("fetchChartData", chartSlug)
await this.$store.dispatch("fetchChartData", {
chartSlug: this.chartSlug,
data: {}
})
).data
this.chartConfig = (
await this.$store.dispatch("fetchChartConfig", chartSlug)
await this.$store.dispatch("fetchChartConfig", this.chartSlug)
).data
if (this.chartConfig.has_form) {
this.chartSchema = (
await this.$store.dispatch("fetchChartSchema", chartSlug)
await this.$store.dispatch("fetchChartSchema", this.chartSlug)
).data
}
}
Expand Down
10 changes: 8 additions & 2 deletions admin_ui/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,14 @@ export default new Vuex.Store({
async fetchChartConfig(context, chartSlug: string) {
return await axios.get(`${BASE_URL}charts/${chartSlug}/`)
},
async fetchChartData(context, chartSlug: string) {
return await axios.post(`${BASE_URL}charts/${chartSlug}/data/`, {})
async fetchChartData(
context,
config: { chartSlug: string; data: object }
) {
return await axios.post(
`${BASE_URL}charts/${config.chartSlug}/data/`,
config.data
)
},
async fetchChartSchema(context, chartSlug: string) {
return await axios.get(`${BASE_URL}charts/${chartSlug}/schema/`)
Expand Down
2 changes: 1 addition & 1 deletion admin_ui/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export function convertFormValue(params: {
} else if (schema?.properties[key].type == "number" && value == "") {
value = null
} else if (
schema?.properties[key].extra.foreign_key == true &&
schema?.properties[key].extra?.foreign_key == true &&
value == ""
) {
value = null
Expand Down
6 changes: 3 additions & 3 deletions piccolo_admin/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,9 @@ def booking_endpoint(request: Request, data: BookingModel) -> str:
async def get_director_movie_count():
movies = (
await Movie.select(
Movie.director.name.as_alias("director"), CountAgg(Movie.id)
Movie.director.name.as_alias("director"), CountAgg()
)
.group_by(Movie.director)
.group_by(Movie.director.name)
.order_by(Movie.director.name)
)

Expand Down Expand Up @@ -474,7 +474,7 @@ async def get_movie_count_per_year(model: MovieCountModel):
GROUP BY EXTRACT(year FROM release_date)
"""

movies = await Movie.raw(query, model.start_date)
movies = await Movie.raw(query, model.start_date.year)

movies_per_year = {i["year"]: i["count"] for i in movies}

Expand Down

0 comments on commit 96fde3d

Please sign in to comment.