Skip to content

Commit

Permalink
Add loadJob and loadForm
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenklar committed May 21, 2024
1 parent 335ff27 commit 5368288
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 15 deletions.
28 changes: 21 additions & 7 deletions daiquiri/query/assets/js/query/components/App.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
import React from 'react'
import React, { useState } from 'react'

import { parseLocation } from '../utils/location'
import { parseLocation, updateLocation } from '../utils/location'

import Form from './Form'
import Forms from './Forms'
import Job from './Job'
import Jobs from './Jobs'
import Status from './Status'

const App = () => {
const { jobId, formKey } = parseLocation()
const location = parseLocation()

const [state, setState] = useState(location)

const loadJob = (jobId) => {
updateLocation({ jobId })
setState({ jobId })
}

const loadForm = (formKey) => {
updateLocation({ formKey })
setState({ formKey })
}

return (
<div className="container">
Expand All @@ -16,15 +30,15 @@ const App = () => {
<div className="row">
<div className="col-sm-4">
<Status />
<Forms />
<Jobs />
<Forms loadForm={loadForm} />
<Jobs loadJob={loadJob} />
</div>
<div className="col-sm-8">
{
jobId && <pre>{jobId}</pre>
state.jobId && <Job jobId={state.jobId} />
}
{
formKey && <pre>{formKey}</pre>
state.formKey && <Form formKey={state.formKey} />
}
</div>
</div>
Expand Down
16 changes: 16 additions & 0 deletions daiquiri/query/assets/js/query/components/Form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react'
import PropTypes from 'prop-types'

const Form = ({ formKey }) => {
return (
<div className="form">
<h2>{formKey}</h2>
</div>
)
}

Form.propTypes = {
formKey: PropTypes.string.isRequired
}

export default Form
23 changes: 20 additions & 3 deletions daiquiri/query/assets/js/query/components/Forms.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import React from 'react'
import PropTypes from 'prop-types'
import { isNil } from 'lodash'

import { useFormsQuery } from '../hooks/query'
import { basePath } from '../utils/location'

const Forms = () => {
import Loading from './Loading'

const Forms = ({ loadForm }) => {
const { data: forms } = useFormsQuery()

const handleLoadForm = (event, form) => {
event.preventDefault()
loadForm(form.key)
}

return (
<div className="card mb-3">
<div className="card-header">
Expand All @@ -14,13 +23,17 @@ const Forms = () => {
{
isNil(forms) ? (
<div className="card-body">
<p>Loading ...</p>
<Loading />
</div>
) : (
<ul className="list-group list-group-flush">
{
forms.map((form) => (
<li key={form.key} className="list-group-item">{form.key}</li>
<li key={form.key} className="list-group-item">
<a href={`${basePath}/${form.key}/`} onClick={(event) => handleLoadForm(event, form)}>
{form.key}
</a>
</li>
))
}
</ul>
Expand All @@ -30,4 +43,8 @@ const Forms = () => {
)
}

Forms.propTypes = {
loadForm: PropTypes.func.isRequired
}

export default Forms
24 changes: 24 additions & 0 deletions daiquiri/query/assets/js/query/components/Job.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from 'react'
import PropTypes from 'prop-types'
import { isNil } from 'lodash'
import { useJobQuery } from '../hooks/query'

const Job = ({ jobId }) => {
const { data: job } = useJobQuery(jobId)

return isNil(job) ? (
<span>{gettext('Loading ...')}</span>
) : (
<div className="job">
<h2>{job.table_name}</h2>
<pre>{job.id}</pre>
<pre>{job.query}</pre>
</div>
)
}

Job.propTypes = {
jobId: PropTypes.string.isRequired
}

export default Job
23 changes: 20 additions & 3 deletions daiquiri/query/assets/js/query/components/Jobs.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import React from 'react'
import PropTypes from 'prop-types'
import { isNil } from 'lodash'

import { useJobsQuery } from '../hooks/query'
import { basePath } from '../utils/location'

const Jobs = () => {
import Loading from './Loading'

const Jobs = ({ loadJob }) => {
const { data: jobs } = useJobsQuery()

const handleLoadJob = (event, job) => {
event.preventDefault()
loadJob(job.id)
}

return (
<div className="card mb-3">
<div className="card-header">
Expand All @@ -14,13 +23,17 @@ const Jobs = () => {
{
isNil(jobs) ? (
<div className="card-body">
<p>Loading ...</p>
<Loading />
</div>
) : (
<ul className="list-group list-group-flush">
{
jobs.map((job) => (
<li key={job.id} className="list-group-item">{job.table_name}</li>
<li key={job.id} className="list-group-item">
<a href={`${basePath}/${job.id}/`} onClick={(event) => handleLoadJob(event, job)}>
{job.table_name}
</a>
</li>
))
}
</ul>
Expand All @@ -30,4 +43,8 @@ const Jobs = () => {
)
}

Jobs.propTypes = {
loadJob: PropTypes.func.isRequired
}

export default Jobs
18 changes: 18 additions & 0 deletions daiquiri/query/assets/js/query/components/Loading.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react'
import PropTypes from 'prop-types'

const Loading = ({ show }) => {
return show && (
<span>{gettext('Loading ...')}</span>
)
}

Loading.defaultProps = {
show: true
}

Loading.propTypes = {
show: PropTypes.bool.isRequired
}

export default Loading
7 changes: 7 additions & 0 deletions daiquiri/query/assets/js/query/hooks/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,10 @@ export const useUserSchema = () => {
refetchInterval: refetchInterval
})
}

export const useJobQuery = (jobId) => {
return useQuery({
queryKey: ['job', jobId],
queryFn: () => QueryApi.fetchJob(jobId)
})
}
4 changes: 2 additions & 2 deletions daiquiri/query/assets/js/query/utils/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isEmpty, isNil, trim } from 'lodash'

import { baseUrl } from '../../../../../core/assets/js/utils/location'

const basePath = `${baseUrl}query/new/`
const basePath = `${baseUrl}/query/new/`

const parseLocation = () => {
const path = trim(window.location.pathname.replace(basePath, ''), '/')
Expand Down Expand Up @@ -40,4 +40,4 @@ const buildPath = ({ jobId, formKey }) => {
return path
}

export { parseLocation, updateLocation, buildPath }
export { basePath, parseLocation, updateLocation, buildPath }

0 comments on commit 5368288

Please sign in to comment.