Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[legacy-framework] Add useParams and useRouterQuery hooks #574

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
610976d
add `useRouterParams` and `useRouterQuery` hooks
anteprimorac May 28, 2020
0ee91a4
add `url` dependency to `@blitzjs/core`
anteprimorac May 28, 2020
8aed343
rewrite `useRouterParams` to use `useRouterQuery` and `router.query`
anteprimorac May 28, 2020
ba730b1
use new hooks `useRouterParams` and `useRouterQuery` in store example
anteprimorac May 28, 2020
cba9f21
add test for showing ascending order of store example `admin/products`
anteprimorac May 28, 2020
d6b2055
Merge remote-tracking branch 'upstream/canary' into use-router-params…
anteprimorac May 28, 2020
01ec156
Merge remote-tracking branch 'upstream/canary' into use-router-params…
anteprimorac May 29, 2020
368ac15
fix wrong prettier config used in `examples/store`
anteprimorac May 29, 2020
505e922
implement `exportRouterParams` and fix issue with matching `string[]`…
anteprimorac May 29, 2020
9a33837
add unit test for `useRouterParams`
anteprimorac May 29, 2020
4300a6e
another fix for using wrong prettier config
anteprimorac May 29, 2020
16adee0
Merge remote-tracking branch 'upstream/canary' into use-router-params…
anteprimorac May 29, 2020
f6418c4
Merge remote-tracking branch 'upstream/canary' into use-router-params…
anteprimorac May 30, 2020
e4d0bd6
pin `url` package to `0.11.0` version
anteprimorac May 30, 2020
b6ba015
rename `exportRouterParams` to `extractRouterParams`
anteprimorac May 30, 2020
a22a209
add additional edge case for `extractRouterParams` test
anteprimorac May 30, 2020
973310c
Merge remote-tracking branch 'upstream/canary' into use-router-params…
anteprimorac Jun 1, 2020
0ade3f2
Merge remote-tracking branch 'upstream/canary' into use-router-params…
anteprimorac Jun 3, 2020
afb2c58
rename `useRouterParams` to `useParams`
anteprimorac Jun 3, 2020
809a8a0
Merge branch 'canary' into use-router-params-and-query-hooks
flybayer Jun 4, 2020
4929ec4
fix failed merge
flybayer Jun 4, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions examples/store/app/admin/pages/admin/products/[id].tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {Suspense} from 'react'
import {Link, useRouter, useQuery} from 'blitz'
import getProduct from 'app/products/queries/getProduct'
import ProductForm from 'app/products/components/ProductForm'
import { Suspense } from "react"
import { Link, useRouter, useQuery, useRouterParams } from "blitz"
import getProduct from "app/products/queries/getProduct"
import ProductForm from "app/products/components/ProductForm"

function Product() {
const router = useRouter()
const id = parseInt(router?.query.id as string)
const [product] = useQuery(getProduct, {where: {id}})
const { id } = useRouterParams()
const [product] = useQuery(getProduct, { where: { id: Number(id) } })

return <ProductForm product={product} onSuccess={() => router.push('/admin/products')} />
return <ProductForm product={product} onSuccess={() => router.push("/admin/products")} />
}

function EditProductPage() {
Expand Down
10 changes: 8 additions & 2 deletions examples/store/app/admin/pages/admin/products/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { Suspense } from "react"
import { useQuery, Link } from "blitz"
import { useQuery, Link, useRouterQuery } from "blitz"
import getProducts from "app/products/queries/getProducts"

function ProductsList() {
const [products] = useQuery(getProducts, { orderBy: { id: "desc" } })
const { orderby = "id", order = "desc" } = useRouterQuery()

const [products] = useQuery(getProducts, {
orderBy: {
[Array.isArray(orderby) ? orderby[0] : orderby]: order,
},
})

return (
<ul>
Expand Down
37 changes: 24 additions & 13 deletions examples/store/cypress/integration/admin/products/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
import {insert} from './helper'
import { insert } from "./helper"

describe('admin/products page', () => {
describe("admin/products page", () => {
beforeEach(() => {
cy.visit('/admin/products')
cy.visit("/admin/products")
})

it('Has H1', () => {
cy.contains('h1', 'Products')
it("Has H1", () => {
cy.contains("h1", "Products")
})

it('goes to new product page', () => {
cy.get('p > a').first().contains('Create Product').click()
cy.location('pathname').should('equal', '/admin/products/new')
it("goes to new product page", () => {
cy.get("p > a").first().contains("Create Product").click()
cy.location("pathname").should("equal", "/admin/products/new")
})

it('goes to bas product page', () => {
cy.get('p > a').last().contains('Admin').click()
cy.location('pathname').should('equal', '/admin')
it("goes to bas product page", () => {
cy.get("p > a").last().contains("Admin").click()
cy.location("pathname").should("equal", "/admin")
})

it("shows ascending order", () => {
cy.get("ul > li")
.first()
.then(($li) => {
const title = $li.find("> a").text()

cy.visit("/admin/products?order=asc")
cy.get("ul > li").last().contains("a", title)
})
})

// This is kind of redundant because this logic is handled in insert()
it('shows latest created product', () => {
it("shows latest created product", () => {
const name = insert()
cy.get('ul > li').contains(name)
cy.get("ul > li").contains(name)
})
})

Expand Down
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"dependencies": {
"pretty-ms": "6.0.1",
"react-query": "1.3.3",
"serialize-error": "6.0.0"
"serialize-error": "6.0.0",
"url": "^0.11.0"
anteprimorac marked this conversation as resolved.
Show resolved Hide resolved
},
"devDependencies": {
"@rollup/pluginutils": "3.0.8",
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export * from './use-query'
export * from './use-paginated-query'
export * from './use-router-params'
export * from './use-router-query'
export * from './ssr-query'
export * from './rpc'

Expand Down
13 changes: 13 additions & 0 deletions packages/core/src/use-router-params.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {useRouter} from 'next/router'
import {useRouterQuery} from './use-router-query'

export function useRouterParams() {
const router = useRouter()
const query = useRouterQuery()

return Object.fromEntries(
Object.entries(router.query).filter(
([key, value]) => typeof query[key] === 'undefined' || query[key] !== value,
),
)
anteprimorac marked this conversation as resolved.
Show resolved Hide resolved
}
8 changes: 8 additions & 0 deletions packages/core/src/use-router-query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {useRouter} from 'next/router'
import {parse} from 'url'
anteprimorac marked this conversation as resolved.
Show resolved Hide resolved

export function useRouterQuery() {
const router = useRouter()
const {query} = parse(router.asPath, true)
return query
}