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

[pull] main from SpaceyaTech:main #4

Merged
merged 7 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ tsconfig.node.tsbuildinfo
/blob-report/
/playwright/.cache/
tsconfig.app.tsbuildinfo
codealike.json
32 changes: 32 additions & 0 deletions docs/TANSTACK-QUERY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# tips

- prefer `useSuspenseQuery` over `useQuery`
- prefer defining `queryOptions` over inlinignthem into the query hooks

```tsx
import { queryOptions,useSuspenseQuery } from "@tanstack/react-query";
//users/queryOptions.ts
// this is re-usable and decltters your components
export const userQueryOptions = queryOptions({
queryKey: ["user"],
queryFn: () => {
...
}
})
// users.tsx
const query = useSuspenseQuery(userQueryOptions);
```
- wrap the data fetching components with `Suspense` component

```tsx
// users page
<div>
<header>Users page</headers>
<Suspense fallback={<div>Loading users</div>}>
<Users />
</Suspense>
</div>
```

run `p/mpm run page users` to scafold a page with these best practices

108 changes: 108 additions & 0 deletions docs/TANSTACK-ROUTER.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
- anything under the src/routes folder is a route

ex

- `src/routes/index.tsx` ->` /`
- `src/routes/profile.tsx` or `src/routes/profile/index.tsx` -> `/profile/`

Dynamic routes

- `src/routes/users/$oneuser/index.tsx` -> `/users/$oneuser`

inside here you'll have access to the `oneuser` param

```tsx
// src/routes/users/$user/index.tsx
import { createFileRoute, useParams } from '@tanstack/react-router'

export const Route = createFileRoute('/users/$user')({
component:OneUserComponent,
})
functon OneUserComponent() {
const {user} = useParams({
from:"/users/$user"
})
return (
<div>
<h1>{user}</h1>
</div>
)
}
```

if you nest multiple dynamic routes then you can access them in the same way

```tsx
// src/routes/users/$user/friends/$friend/index.tsx
import { createFileRoute, useParams } from '@tanstack/react-router'

export const Route = createFileRoute('/users/$user/friends/$friend/')({
component: RouteComponent,
})

function RouteComponent() {
const { friend,user } = useParams({
from: "/users/$user/friends/$friend/",
});
return (
<div>
<h1>{user}</h1>
<h1>{friend}</h1>
</div>
);
}


```

you can also define search params ,validate them and auth guard

```tsx
import {
createFileRoute,
redirect,
useNavigate,
useSearch,
} from "@tanstack/react-router";
import { z } from "zod";
const searchparams = z.object({
// make it optional if it won't always be used
page: z.number().optional(),
});

export const Route = createFileRoute("/users/")({
component: RouteComponent,
// declare and validate your search params here
// this page should alwatys have /users?page=1
validateSearch: (search) => searchparams.parse(search),
// this is how you auth guard routes (only allow logged in users here )
async beforeLoad(ctx) {
const viewer = ctx.context?.viewer;
if (!viewer?.record) {
throw redirect({
to: "/auth",
// this search params will be used to redirect you back to this page once you log in
search: { returnTo: ctx.location.pathname },
});
}
},
});

function RouteComponent() {
const { page } = useSearch({
from: "/users/",
});
const navigate = useNavigate({
from: "/users",
});
return (
<div>
{page}
<button onClick={() => navigate({ search: { page: (page ?? 0) + 1 } })}>
next page
</button>
</div>
);
}

```
6 changes: 6 additions & 0 deletions docs/TESTING.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Testing

>[!NOTE]
> Prefer e2e (playwright) fro testing the ui instead of unit tests. and uase the tooling like recording of tests and locators.
> use unit tests for units of logic and functions only



```sh
npm run test
```
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@
"@tanstack/react-query-devtools": "^5.59.19",
"@tanstack/router-devtools": "^1.79.0",
"@tanstack/router-plugin": "^1.79.0",
"@testing-library/dom": "^10.4.0",
"@testing-library/jest-dom": "^6.6.3",
"@testing-library/react": "^16.1.0",
"@testing-library/user-event": "^14.5.2",
"@types/node": "^22.10.1",
"@types/react": "^18.3.11",
"@types/react-dom": "^18.3.1",
Expand All @@ -89,6 +93,7 @@
"eslint-plugin-react-hooks": "^5.0.0",
"eslint-plugin-react-refresh": "^0.4.13",
"globals": "^15.11.0",
"jsdom": "^25.0.1",
"postcss": "^8.4.47",
"prettier": "^3.3.2",
"prettier-plugin-tailwindcss": "^0.6.5",
Expand Down
Loading
Loading