Compare commits

...

81 Commits

Author SHA1 Message Date
Sebastián Ramírez 2eb8f5b624 🔖 Release version 0.8.0 2025-02-19 13:18:57 +01:00
github-actions 558b466d43 📝 Update release notes
[skip ci]
2025-02-19 12:00:29 +00:00
Alejandra 496c7090b3 ♻️ Redirect the user to login if we get 401/403 (#1501) 2025-02-19 12:00:08 +00:00
github-actions 50d2a3bfe1 📝 Update release notes
[skip ci]
2025-02-19 07:37:36 +00:00
Alejandra 1523c20cae 🐛 Refactor reset password test to create normal user instead of using super user (#1499) 2025-02-19 08:37:16 +01:00
github-actions f8e35a5249 📝 Update release notes
[skip ci]
2025-02-18 16:54:48 +00:00
Joel Pérez 9f6f8f088b ♻️ Replace email types from str to EmailStr in config.py (#1492) 2025-02-18 17:53:38 +01:00
github-actions ff5079079b 📝 Update release notes
[skip ci]
2025-02-18 15:03:12 +00:00
Alejandra 7b05fc1f6d 🔧 Remove unused context from router creation (#1498) 2025-02-18 15:02:50 +00:00
github-actions 86ef743226 📝 Update release notes
[skip ci]
2025-02-17 19:55:39 +00:00
Alejandra ed34e5e170 🔧 Configure path alias for cleaner imports (#1497) 2025-02-17 20:55:20 +01:00
github-actions 322975dd6e 📝 Update release notes
[skip ci]
2025-02-17 19:33:16 +00:00
Alejandra 55df823739 🛂 Migrate to Chakra UI v3 (#1496)
Co-authored-by: github-actions <github-actions@github.com>
2025-02-17 20:33:00 +01:00
github-actions 74e2604faf 📝 Update release notes
[skip ci]
2025-02-17 18:04:07 +00:00
Chris Handy 631980a876 📝 Removing deprecated manual client SDK step (#1494) 2025-02-17 18:03:49 +00:00
github-actions f178e58bfe 📝 Update release notes
[skip ci]
2025-02-10 19:26:55 +00:00
Christopher Markus aee08490e5 📝 Update Frontend README.md (#1462)
Fix incorrect script name which was generate-frontend-client.sh and now is generate-client.sh
2025-02-10 19:26:37 +00:00
github-actions 42fd54e06b 📝 Update release notes
[skip ci]
2025-02-10 19:26:00 +00:00
dependabot[bot] 16997dabf0 Bump vite from 5.0.13 to 5.4.14 in /frontend (#1469)
Bumps [vite](https://github.com/vitejs/vite/tree/HEAD/packages/vite) from 5.0.13 to 5.4.14.
- [Release notes](https://github.com/vitejs/vite/releases)
- [Changelog](https://github.com/vitejs/vite/blob/v5.4.14/packages/vite/CHANGELOG.md)
- [Commits](https://github.com/vitejs/vite/commits/v5.4.14/packages/vite)

---
updated-dependencies:
- dependency-name: vite
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-02-10 19:25:42 +00:00
github-actions 7cffa83649 📝 Update release notes
[skip ci]
2025-02-10 19:23:23 +00:00
Nauanbek 15f41f755b ♻️ Remove redundant item deletion code leveraging cascade delete (#1481) 2025-02-10 19:23:04 +00:00
github-actions f2eb1b5c5f 📝 Update release notes
[skip ci]
2025-02-10 15:14:40 +00:00
Robert (Jamie) Munro 6042eda4bc ✏️ Fix a couple of spelling mistakes (#1485) 2025-02-10 15:14:19 +00:00
github-actions ba1706b489 📝 Update release notes
[skip ci]
2025-01-06 11:33:15 +00:00
Boriss Jerjomkin c754a0058b 🧑‍🔧 Replace correct value for 'htmlFor' (#1456) 2025-01-06 11:32:57 +00:00
github-actions 2d553dda53 📝 Update release notes
[skip ci]
2025-01-05 14:58:03 +00:00
Alejandra 846dd9e81a ⬆️ Update Dockerfile to use uv version 0.5.11 (#1454) 2025-01-05 14:57:42 +00:00
github-actions d4df197c59 📝 Update release notes
[skip ci]
2024-12-21 11:01:03 +00:00
Ayoub Benaissa 87afb074a3 📝 Update frontend/README.md to also remove Playwright when removing Frontend (#1452) 2024-12-21 11:00:44 +00:00
github-actions 98a2d7ddcf 📝 Update release notes
[skip ci]
2024-12-21 10:59:48 +00:00
dependabot[bot] b95dea6d26 ⬆ Bump astral-sh/setup-uv from 4 to 5 (#1453)
Bumps [astral-sh/setup-uv](https://github.com/astral-sh/setup-uv) from 4 to 5.
- [Release notes](https://github.com/astral-sh/setup-uv/releases)
- [Commits](https://github.com/astral-sh/setup-uv/compare/v4...v5)

---
updated-dependencies:
- dependency-name: astral-sh/setup-uv
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-12-21 10:59:31 +00:00
github-actions 88c83cc06c 📝 Update release notes
[skip ci]
2024-12-10 09:59:57 +00:00
dependabot[bot] 3e6ed0157e ⬆ Bump astral-sh/setup-uv from 3 to 4 (#1433)
Bumps [astral-sh/setup-uv](https://github.com/astral-sh/setup-uv) from 3 to 4.
- [Release notes](https://github.com/astral-sh/setup-uv/releases)
- [Commits](https://github.com/astral-sh/setup-uv/compare/v3...v4)

---
updated-dependencies:
- dependency-name: astral-sh/setup-uv
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-12-10 09:59:36 +00:00
github-actions b0f6195827 📝 Update release notes
[skip ci]
2024-12-02 12:04:22 +00:00
Patrick Arminio 30832076f5 🎨 Move prefix and tags to routers (#1439) 2024-12-02 12:04:03 +00:00
github-actions aa71abedcc 📝 Update release notes
[skip ci]
2024-12-02 11:58:11 +00:00
Patrick Arminio 14129f7a50 ✨ Add private, local only, API for usage in E2E tests (#1429)
Co-authored-by: github-actions <github-actions@github.com>
2024-12-02 12:57:53 +01:00
github-actions 414864d848 📝 Update release notes
[skip ci]
2024-11-27 11:43:08 +00:00
Patrick Arminio 953e9e5416 ♻️ Remove modify id script in favor of openapi-ts config (#1434) 2024-11-27 12:42:44 +01:00
github-actions b7d2df2f81 📝 Update release notes
[skip ci]
2024-11-27 11:07:21 +00:00
Patrick Arminio a21c57ab2b ✨ Migrate to latest openapi-ts (#1430) 2024-11-27 12:06:59 +01:00
github-actions e34527325b 📝 Update release notes
[skip ci]
2024-11-08 22:02:26 +00:00
dependabot[bot] 19764d5307 ⬆ Bump tiangolo/latest-changes from 0.3.1 to 0.3.2 (#1418)
Bumps [tiangolo/latest-changes](https://github.com/tiangolo/latest-changes) from 0.3.1 to 0.3.2.
- [Release notes](https://github.com/tiangolo/latest-changes/releases)
- [Commits](https://github.com/tiangolo/latest-changes/compare/0.3.1...0.3.2)

---
updated-dependencies:
- dependency-name: tiangolo/latest-changes
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-11-08 22:02:06 +00:00
github-actions 3aad3fb877 📝 Update release notes 2024-11-01 15:29:47 +00:00
Sebastián Ramírez 61fa9431b7 📝 Update deployment.md, instructions to install GitHub Runner in non-root VMs (#1412) 2024-11-01 15:29:29 +00:00
github-actions 24c4f13629 📝 Update release notes 2024-10-25 21:56:53 +00:00
Sebastián Ramírez e684f3c8d6 👷 Improve Playwright CI speed: sharding (paralel runs), run in Docker to use cache, use env vars (#1405) 2024-10-25 21:56:34 +00:00
github-actions d3d370cad0 📝 Update release notes 2024-10-14 15:36:28 +00:00
Alejandra c23c91bd4e 👷 Update issue manager workflow (#1398) 2024-10-14 17:35:47 +02:00
github-actions 6768359244 📝 Update release notes 2024-10-12 13:55:29 +00:00
Sebastián Ramírez a2e3a8ae11 👷 Fix smokeshow, checkout files on CI (#1395) 2024-10-12 15:54:30 +02:00
github-actions f5612ed7cd 📝 Update release notes 2024-10-12 12:38:47 +00:00
Tobias 12a13b4108 📝 Add MailCatcher to development.md (#1387) 2024-10-12 14:38:29 +02:00
github-actions db7ea2cbbe 📝 Update release notes 2024-10-07 20:34:33 +00:00
Sebastián Ramírez 86d2fcc2e8 👷 Update labeler.yml (#1388) 2024-10-07 20:33:45 +00:00
github-actions 67e3f35491 📝 Update release notes 2024-10-03 18:35:51 +00:00
Justin Perdok 1c3e464acd 🔧 Add .auth playwright folder to .gitignore (#1383) 2024-10-03 20:35:15 +02:00
github-actions 372c9f8f91 📝 Update release notes 2024-10-03 18:33:05 +00:00
Saltie 8d52d018b4 ♻️ Add PaginationFooter component (#1381) 2024-10-03 20:32:46 +02:00
github-actions 88e1a607b0 📝 Update release notes 2024-09-30 13:39:42 +00:00
dependabot[bot] 6047623ad1 ⬆️ Bump rollup from 4.6.1 to 4.22.5 in /frontend (#1379)
Bumps [rollup](https://github.com/rollup/rollup) from 4.6.1 to 4.22.5.
- [Release notes](https://github.com/rollup/rollup/releases)
- [Changelog](https://github.com/rollup/rollup/blob/master/CHANGELOG.md)
- [Commits](https://github.com/rollup/rollup/compare/v4.6.1...v4.22.5)

---
updated-dependencies:
- dependency-name: rollup
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-09-30 15:39:17 +02:00
github-actions 0a5f1025d6 📝 Update release notes 2024-09-27 16:42:55 +00:00
Muhammad Sameer Amin 172bfd9349 ♻️ Refactored code to use encryption algorithm name from settings for consistency (#1160)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
2024-09-27 18:42:36 +02:00
github-actions 79883c879f 📝 Update release notes 2024-09-27 16:40:03 +00:00
github-actions 721a44ab2f 📝 Update release notes 2024-09-27 16:39:41 +00:00
Hmily ddb4235bfe 🔊 Enable logging for email utils by default (#1374)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
2024-09-27 16:39:40 +00:00
dependabot[bot] 2939b7723b ⬆ Bump astral-sh/setup-uv from 2 to 3 (#1364)
Bumps [astral-sh/setup-uv](https://github.com/astral-sh/setup-uv) from 2 to 3.
- [Release notes](https://github.com/astral-sh/setup-uv/releases)
- [Commits](https://github.com/astral-sh/setup-uv/compare/v2...v3)

---
updated-dependencies:
- dependency-name: astral-sh/setup-uv
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-09-27 18:39:19 +02:00
github-actions 4d5b0a065c 📝 Update release notes 2024-09-27 16:30:27 +00:00
Chris 3793150fe4 👷 Update pre-commit end-of-file-fixer hook to exclude email-templates (#1296)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
2024-09-27 16:30:09 +00:00
github-actions ab0d1bb12f 📝 Update release notes 2024-09-27 16:28:02 +00:00
dependabot[bot] 3136437bcb ⬆ Bump tiangolo/issue-manager from 0.5.0 to 0.5.1 (#1332)
Bumps [tiangolo/issue-manager](https://github.com/tiangolo/issue-manager) from 0.5.0 to 0.5.1.
- [Release notes](https://github.com/tiangolo/issue-manager/releases)
- [Commits](https://github.com/tiangolo/issue-manager/compare/0.5.0...0.5.1)

---
updated-dependencies:
- dependency-name: tiangolo/issue-manager
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-09-27 18:27:40 +02:00
github-actions b9ec1943eb 📝 Update release notes 2024-09-27 16:11:18 +00:00
Sebastián Ramírez fe7f4f7949 🔧 Add ENV PYTHONUNBUFFERED=1 to log output directly to Docker (#1378) 2024-09-27 16:10:59 +00:00
github-actions b67fb44e5b 📝 Update release notes 2024-09-27 16:00:15 +00:00
Waket Zheng 945fbc7699 🔧 Run task by the same Python environment used to run Copier (#1157)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
2024-09-27 17:59:54 +02:00
github-actions 109a7454c7 📝 Update release notes 2024-09-27 15:53:52 +00:00
Sebastián Ramírez 38843cbe28 👷 Tweak generate client to error out if there are errors (#1377) 2024-09-27 15:53:31 +00:00
github-actions 1030118247 📝 Update release notes 2024-09-27 15:41:36 +00:00
Hani Sabsoob 08dd87d0f2 💡 Remove unnecessary comment (#1260)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
2024-09-27 17:41:17 +02:00
github-actions 6a8fc240b3 📝 Update release notes 2024-09-27 14:23:43 +00:00
Sebastián Ramírez c297e9ee9e 👷 Generate and commit client only on same repo PRs, on forks, show the error (#1376) 2024-09-27 16:23:23 +02:00
110 changed files with 7346 additions and 5442 deletions
+19 -2
View File
@@ -12,7 +12,11 @@ jobs:
contents: write contents: write
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
# For PRs from forks
- uses: actions/checkout@v4 - uses: actions/checkout@v4
# For PRs from the same repo
- uses: actions/checkout@v4
if: ( github.event_name != 'pull_request' || github.secret_source == 'Actions' )
with: with:
ref: ${{ github.head_ref }} ref: ${{ github.head_ref }}
token: ${{ secrets.FULL_STACK_FASTAPI_TEMPLATE_REPO_TOKEN }} token: ${{ secrets.FULL_STACK_FASTAPI_TEMPLATE_REPO_TOKEN }}
@@ -23,7 +27,7 @@ jobs:
with: with:
python-version: "3.10" python-version: "3.10"
- name: Install uv - name: Install uv
uses: astral-sh/setup-uv@v2 uses: astral-sh/setup-uv@v5
with: with:
version: "0.4.15" version: "0.4.15"
enable-cache: true enable-cache: true
@@ -35,10 +39,23 @@ jobs:
- run: uv run bash scripts/generate-client.sh - run: uv run bash scripts/generate-client.sh
env: env:
VIRTUAL_ENV: backend/.venv VIRTUAL_ENV: backend/.venv
- name: Commit changes ENVIRONMENT: production
SECRET_KEY: just-for-generating-client
POSTGRES_PASSWORD: just-for-generating-client
FIRST_SUPERUSER_PASSWORD: just-for-generating-client
- name: Add changes to git
run: | run: |
git config --local user.email "github-actions@github.com" git config --local user.email "github-actions@github.com"
git config --local user.name "github-actions" git config --local user.name "github-actions"
git add frontend/src/client git add frontend/src/client
# Same repo PRs
- name: Push changes
if: ( github.event_name != 'pull_request' || github.secret_source == 'Actions' )
run: |
git diff --staged --quiet || git commit -m "✨ Autogenerate frontend client" git diff --staged --quiet || git commit -m "✨ Autogenerate frontend client"
git push git push
# Fork PRs
- name: Check changes
if: ( github.event_name == 'pull_request' && github.secret_source != 'Actions' )
run: |
git diff --staged --quiet || (echo "Changes detected in generated client, run scripts/generate-client.sh and commit the changes" && exit 1)
+5 -1
View File
@@ -27,7 +27,7 @@ jobs:
env: env:
GITHUB_CONTEXT: ${{ toJson(github) }} GITHUB_CONTEXT: ${{ toJson(github) }}
run: echo "$GITHUB_CONTEXT" run: echo "$GITHUB_CONTEXT"
- uses: tiangolo/issue-manager@0.5.0 - uses: tiangolo/issue-manager@0.5.1
with: with:
token: ${{ secrets.GITHUB_TOKEN }} token: ${{ secrets.GITHUB_TOKEN }}
config: > config: >
@@ -39,5 +39,9 @@ jobs:
"waiting": { "waiting": {
"delay": 2628000, "delay": 2628000,
"message": "As this PR has been waiting for the original user for a while but seems to be inactive, it's now going to be closed. But if there's anyone interested, feel free to create a new PR." "message": "As this PR has been waiting for the original user for a while but seems to be inactive, it's now going to be closed. But if there's anyone interested, feel free to create a new PR."
},
"invalid": {
"delay": 0,
"message": "This was marked as invalid and will be closed now. If this is an error, please provide additional details."
} }
} }
+2
View File
@@ -17,6 +17,8 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/labeler@v5 - uses: actions/labeler@v5
if: ${{ github.event.action != 'labeled' && github.event.action != 'unlabeled' }}
- run: echo "Done adding labels"
# Run this after labeler applied labels # Run this after labeler applied labels
check-labels: check-labels:
needs: needs:
+1 -1
View File
@@ -30,7 +30,7 @@ jobs:
with: with:
# To allow latest-changes to commit to the main branch # To allow latest-changes to commit to the main branch
token: ${{ secrets.LATEST_CHANGES }} token: ${{ secrets.LATEST_CHANGES }}
- uses: tiangolo/latest-changes@0.3.1 - uses: tiangolo/latest-changes@0.3.2
with: with:
token: ${{ secrets.GITHUB_TOKEN }} token: ${{ secrets.GITHUB_TOKEN }}
latest_changes_file: ./release-notes.md latest_changes_file: ./release-notes.md
+1 -1
View File
@@ -20,7 +20,7 @@ jobs:
with: with:
python-version: "3.10" python-version: "3.10"
- name: Install uv - name: Install uv
uses: astral-sh/setup-uv@v2 uses: astral-sh/setup-uv@v5
with: with:
version: "0.4.15" version: "0.4.15"
enable-cache: true enable-cache: true
+81 -17
View File
@@ -16,10 +16,36 @@ on:
default: 'false' default: 'false'
jobs: jobs:
changes:
runs-on: ubuntu-latest
# Set job outputs to values from filter step
outputs:
changed: ${{ steps.filter.outputs.changed }}
steps:
- uses: actions/checkout@v4
# For pull requests it's not necessary to checkout the code but for the main branch it is
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
changed:
- backend/**
- frontend/**
- .env
- docker-compose*.yml
- .github/workflows/playwright.yml
test: test-playwright:
needs:
- changes
if: ${{ needs.changes.outputs.changed == 'true' }}
timeout-minutes: 60 timeout-minutes: 60
runs-on: ubuntu-latest runs-on: ubuntu-latest
strategy:
matrix:
shardIndex: [1, 2, 3, 4]
shardTotal: [4]
fail-fast: false
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- uses: actions/setup-node@v4 - uses: actions/setup-node@v4
@@ -33,35 +59,73 @@ jobs:
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug_enabled == 'true' }} if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug_enabled == 'true' }}
with: with:
limit-access-to-actor: true limit-access-to-actor: true
- name: Install uv
uses: astral-sh/setup-uv@v5
with:
version: "0.4.15"
enable-cache: true
- run: uv sync
working-directory: backend
- run: npm ci
working-directory: frontend
- run: uv run bash scripts/generate-client.sh
env:
VIRTUAL_ENV: backend/.venv
- run: docker compose build
- run: docker compose down -v --remove-orphans
- name: Run Playwright tests
run: docker compose run --rm playwright npx playwright test --fail-on-flaky-tests --trace=retain-on-failure --shard=${{ matrix.shardIndex }}/${{ matrix.shardTotal }}
- run: docker compose down -v --remove-orphans
- name: Upload blob report to GitHub Actions Artifacts
if: ${{ !cancelled() }}
uses: actions/upload-artifact@v4
with:
name: blob-report-${{ matrix.shardIndex }}
path: frontend/blob-report
include-hidden-files: true
retention-days: 1
merge-playwright-reports:
needs:
- test-playwright
- changes
# Merge reports after playwright-tests, even if some shards have failed
if: ${{ !cancelled() && needs.changes.outputs.changed == 'true' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install dependencies - name: Install dependencies
run: npm ci run: npm ci
working-directory: frontend working-directory: frontend
- name: Install Playwright Browsers - name: Download blob reports from GitHub Actions Artifacts
run: npx playwright install --with-deps uses: actions/download-artifact@v4
working-directory: frontend
- run: docker compose build
- run: docker compose down -v --remove-orphans
- run: docker compose up -d --wait backend mailcatcher
- name: Run Playwright tests
run: npx playwright test --fail-on-flaky-tests --trace=retain-on-failure
working-directory: frontend
- run: docker compose down -v --remove-orphans
- uses: actions/upload-artifact@v4
if: always()
with: with:
name: playwright-report path: frontend/all-blob-reports
path: frontend/playwright-report/ pattern: blob-report-*
merge-multiple: true
- name: Merge into HTML Report
run: npx playwright merge-reports --reporter html ./all-blob-reports
working-directory: frontend
- name: Upload HTML report
uses: actions/upload-artifact@v4
with:
name: html-report--attempt-${{ github.run_attempt }}
path: frontend/playwright-report
retention-days: 30 retention-days: 30
include-hidden-files: true include-hidden-files: true
# https://github.com/marketplace/actions/alls-green#why # https://github.com/marketplace/actions/alls-green#why
e2e-alls-green: # This job does nothing and is only used for the branch protection alls-green-playwright: # This job does nothing and is only used for the branch protection
if: always() if: always()
needs: needs:
- test - test-playwright
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Decide whether the needed jobs succeeded or failed - name: Decide whether the needed jobs succeeded or failed
uses: re-actors/alls-green@release/v1 uses: re-actors/alls-green@release/v1
with: with:
jobs: ${{ toJSON(needs) }} jobs: ${{ toJSON(needs) }}
allowed-skips: test-playwright
+1
View File
@@ -14,6 +14,7 @@ jobs:
statuses: write statuses: write
steps: steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5 - uses: actions/setup-python@v5
with: with:
python-version: "3.10" python-version: "3.10"
+1 -1
View File
@@ -20,7 +20,7 @@ jobs:
with: with:
python-version: "3.10" python-version: "3.10"
- name: Install uv - name: Install uv
uses: astral-sh/setup-uv@v2 uses: astral-sh/setup-uv@v5
with: with:
version: "0.4.15" version: "0.4.15"
enable-cache: true enable-cache: true
+1 -1
View File
@@ -18,7 +18,7 @@ jobs:
uses: actions/checkout@v4 uses: actions/checkout@v4
- run: docker compose build - run: docker compose build
- run: docker compose down -v --remove-orphans - run: docker compose down -v --remove-orphans
- run: docker compose up -d --wait - run: docker compose up -d --wait backend frontend adminer
- name: Test backend is up - name: Test backend is up
run: curl http://localhost:8000/api/v1/utils/health-check run: curl http://localhost:8000/api/v1/utils/health-check
- name: Test frontend is up - name: Test frontend is up
+5 -1
View File
@@ -10,7 +10,11 @@ repos:
args: args:
- --unsafe - --unsafe
- id: end-of-file-fixer - id: end-of-file-fixer
exclude: ^frontend/src/client/.* exclude: |
(?x)^(
frontend/src/client/.*|
backend/app/email-templates/build/.*
)$
- id: trailing-whitespace - id: trailing-whitespace
exclude: ^frontend/src/client/.* exclude: ^frontend/src/client/.*
- repo: https://github.com/charliermarsh/ruff-pre-commit - repo: https://github.com/charliermarsh/ruff-pre-commit
+3 -1
View File
@@ -1,10 +1,12 @@
FROM python:3.10 FROM python:3.10
ENV PYTHONUNBUFFERED=1
WORKDIR /app/ WORKDIR /app/
# Install uv # Install uv
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#installing-uv # Ref: https://docs.astral.sh/uv/guides/integration/docker/#installing-uv
COPY --from=ghcr.io/astral-sh/uv:0.4.15 /uv /bin/uv COPY --from=ghcr.io/astral-sh/uv:0.5.11 /uv /uvx /bin/
# Place executables in the environment at the front of the path # Place executables in the environment at the front of the path
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#using-the-environment # Ref: https://docs.astral.sh/uv/guides/integration/docker/#using-the-environment
+10 -5
View File
@@ -1,9 +1,14 @@
from fastapi import APIRouter from fastapi import APIRouter
from app.api.routes import items, login, users, utils from app.api.routes import items, login, private, users, utils
from app.core.config import settings
api_router = APIRouter() api_router = APIRouter()
api_router.include_router(login.router, tags=["login"]) api_router.include_router(login.router)
api_router.include_router(users.router, prefix="/users", tags=["users"]) api_router.include_router(users.router)
api_router.include_router(utils.router, prefix="/utils", tags=["utils"]) api_router.include_router(utils.router)
api_router.include_router(items.router, prefix="/items", tags=["items"]) api_router.include_router(items.router)
if settings.ENVIRONMENT == "local":
api_router.include_router(private.router)
+1 -1
View File
@@ -7,7 +7,7 @@ from sqlmodel import func, select
from app.api.deps import CurrentUser, SessionDep from app.api.deps import CurrentUser, SessionDep
from app.models import Item, ItemCreate, ItemPublic, ItemsPublic, ItemUpdate, Message from app.models import Item, ItemCreate, ItemPublic, ItemsPublic, ItemUpdate, Message
router = APIRouter() router = APIRouter(prefix="/items", tags=["items"])
@router.get("/", response_model=ItemsPublic) @router.get("/", response_model=ItemsPublic)
+1 -1
View File
@@ -18,7 +18,7 @@ from app.utils import (
verify_password_reset_token, verify_password_reset_token,
) )
router = APIRouter() router = APIRouter(tags=["login"])
@router.post("/login/access-token") @router.post("/login/access-token")
+38
View File
@@ -0,0 +1,38 @@
from typing import Any
from fastapi import APIRouter
from pydantic import BaseModel
from app.api.deps import SessionDep
from app.core.security import get_password_hash
from app.models import (
User,
UserPublic,
)
router = APIRouter(tags=["private"], prefix="/private")
class PrivateUserCreate(BaseModel):
email: str
password: str
full_name: str
is_verified: bool = False
@router.post("/users/", response_model=UserPublic)
def create_user(user_in: PrivateUserCreate, session: SessionDep) -> Any:
"""
Create a new user.
"""
user = User(
email=user_in.email,
full_name=user_in.full_name,
hashed_password=get_password_hash(user_in.password),
)
session.add(user)
session.commit()
return user
+1 -3
View File
@@ -26,7 +26,7 @@ from app.models import (
) )
from app.utils import generate_new_account_email, send_email from app.utils import generate_new_account_email, send_email
router = APIRouter() router = APIRouter(prefix="/users", tags=["users"])
@router.get( @router.get(
@@ -134,8 +134,6 @@ def delete_user_me(session: SessionDep, current_user: CurrentUser) -> Any:
raise HTTPException( raise HTTPException(
status_code=403, detail="Super users are not allowed to delete themselves" status_code=403, detail="Super users are not allowed to delete themselves"
) )
statement = delete(Item).where(col(Item.owner_id) == current_user.id)
session.exec(statement) # type: ignore
session.delete(current_user) session.delete(current_user)
session.commit() session.commit()
return Message(message="User deleted successfully") return Message(message="User deleted successfully")
+1 -1
View File
@@ -5,7 +5,7 @@ from app.api.deps import get_current_active_superuser
from app.models import Message from app.models import Message
from app.utils import generate_test_email, send_email from app.utils import generate_test_email, send_email
router = APIRouter() router = APIRouter(prefix="/utils", tags=["utils"])
@router.post( @router.post(
+5 -7
View File
@@ -5,6 +5,7 @@ from typing import Annotated, Any, Literal
from pydantic import ( from pydantic import (
AnyUrl, AnyUrl,
BeforeValidator, BeforeValidator,
EmailStr,
HttpUrl, HttpUrl,
PostgresDsn, PostgresDsn,
computed_field, computed_field,
@@ -74,9 +75,8 @@ class Settings(BaseSettings):
SMTP_HOST: str | None = None SMTP_HOST: str | None = None
SMTP_USER: str | None = None SMTP_USER: str | None = None
SMTP_PASSWORD: str | None = None SMTP_PASSWORD: str | None = None
# TODO: update type to EmailStr when sqlmodel supports it EMAILS_FROM_EMAIL: EmailStr | None = None
EMAILS_FROM_EMAIL: str | None = None EMAILS_FROM_NAME: EmailStr | None = None
EMAILS_FROM_NAME: str | None = None
@model_validator(mode="after") @model_validator(mode="after")
def _set_default_emails_from(self) -> Self: def _set_default_emails_from(self) -> Self:
@@ -91,10 +91,8 @@ class Settings(BaseSettings):
def emails_enabled(self) -> bool: def emails_enabled(self) -> bool:
return bool(self.SMTP_HOST and self.EMAILS_FROM_EMAIL) return bool(self.SMTP_HOST and self.EMAILS_FROM_EMAIL)
# TODO: update type to EmailStr when sqlmodel supports it EMAIL_TEST_USER: EmailStr = "test@example.com"
EMAIL_TEST_USER: str = "test@example.com" FIRST_SUPERUSER: EmailStr
# TODO: update type to EmailStr when sqlmodel supports it
FIRST_SUPERUSER: str
FIRST_SUPERUSER_PASSWORD: str FIRST_SUPERUSER_PASSWORD: str
def _check_default_secret(self, var_name: str, value: str | None) -> None: def _check_default_secret(self, var_name: str, value: str | None) -> None:
-1
View File
@@ -18,7 +18,6 @@ def init_db(session: Session) -> None:
# the tables un-commenting the next lines # the tables un-commenting the next lines
# from sqlmodel import SQLModel # from sqlmodel import SQLModel
# from app.core.engine import engine
# This works because the models are already imported and registered from app.models # This works because the models are already imported and registered from app.models
# SQLModel.metadata.create_all(engine) # SQLModel.metadata.create_all(engine)
+26 -12
View File
@@ -1,11 +1,14 @@
from unittest.mock import patch from unittest.mock import patch
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from sqlmodel import Session, select from sqlmodel import Session
from app.core.config import settings from app.core.config import settings
from app.core.security import verify_password from app.core.security import verify_password
from app.models import User from app.crud import create_user
from app.models import UserCreate
from app.tests.utils.user import user_authentication_headers
from app.tests.utils.utils import random_email, random_lower_string
from app.utils import generate_password_reset_token from app.utils import generate_password_reset_token
@@ -69,23 +72,34 @@ def test_recovery_password_user_not_exits(
assert r.status_code == 404 assert r.status_code == 404
def test_reset_password( def test_reset_password(client: TestClient, db: Session) -> None:
client: TestClient, superuser_token_headers: dict[str, str], db: Session email = random_email()
) -> None: password = random_lower_string()
token = generate_password_reset_token(email=settings.FIRST_SUPERUSER) new_password = random_lower_string()
data = {"new_password": "changethis", "token": token}
user_create = UserCreate(
email=email,
full_name="Test User",
password=password,
is_active=True,
is_superuser=False,
)
user = create_user(session=db, user_create=user_create)
token = generate_password_reset_token(email=email)
headers = user_authentication_headers(client=client, email=email, password=password)
data = {"new_password": new_password, "token": token}
r = client.post( r = client.post(
f"{settings.API_V1_STR}/reset-password/", f"{settings.API_V1_STR}/reset-password/",
headers=superuser_token_headers, headers=headers,
json=data, json=data,
) )
assert r.status_code == 200 assert r.status_code == 200
assert r.json() == {"message": "Password updated successfully"} assert r.json() == {"message": "Password updated successfully"}
user_query = select(User).where(User.email == settings.FIRST_SUPERUSER) db.refresh(user)
user = db.exec(user_query).first() assert verify_password(new_password, user.hashed_password)
assert user
assert verify_password(data["new_password"], user.hashed_password)
def test_reset_password_invalid_token( def test_reset_password_invalid_token(
@@ -0,0 +1,26 @@
from fastapi.testclient import TestClient
from sqlmodel import Session, select
from app.core.config import settings
from app.models import User
def test_create_user(client: TestClient, db: Session) -> None:
r = client.post(
f"{settings.API_V1_STR}/private/users/",
json={
"email": "pollo@listo.com",
"password": "password123",
"full_name": "Pollo Listo",
},
)
assert r.status_code == 200
data = r.json()
user = db.exec(select(User).where(User.id == data["id"])).first()
assert user
assert user.email == "pollo@listo.com"
assert user.full_name == "Pollo Listo"
+9 -3
View File
@@ -9,8 +9,12 @@ import jwt
from jinja2 import Template from jinja2 import Template
from jwt.exceptions import InvalidTokenError from jwt.exceptions import InvalidTokenError
from app.core import security
from app.core.config import settings from app.core.config import settings
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
@dataclass @dataclass
class EmailData: class EmailData:
@@ -48,7 +52,7 @@ def send_email(
if settings.SMTP_PASSWORD: if settings.SMTP_PASSWORD:
smtp_options["password"] = settings.SMTP_PASSWORD smtp_options["password"] = settings.SMTP_PASSWORD
response = message.send(to=email_to, smtp=smtp_options) response = message.send(to=email_to, smtp=smtp_options)
logging.info(f"send email result: {response}") logger.info(f"send email result: {response}")
def generate_test_email(email_to: str) -> EmailData: def generate_test_email(email_to: str) -> EmailData:
@@ -104,14 +108,16 @@ def generate_password_reset_token(email: str) -> str:
encoded_jwt = jwt.encode( encoded_jwt = jwt.encode(
{"exp": exp, "nbf": now, "sub": email}, {"exp": exp, "nbf": now, "sub": email},
settings.SECRET_KEY, settings.SECRET_KEY,
algorithm="HS256", algorithm=security.ALGORITHM,
) )
return encoded_jwt return encoded_jwt
def verify_password_reset_token(token: str) -> str | None: def verify_password_reset_token(token: str) -> str | None:
try: try:
decoded_token = jwt.decode(token, settings.SECRET_KEY, algorithms=["HS256"]) decoded_token = jwt.decode(
token, settings.SECRET_KEY, algorithms=[security.ALGORITHM]
)
return str(decoded_token["sub"]) return str(decoded_token["sub"])
except InvalidTokenError: except InvalidTokenError:
return None return None
+1 -1
View File
@@ -97,4 +97,4 @@ _exclude:
_answers_file: .copier/.copier-answers.yml _answers_file: .copier/.copier-answers.yml
_tasks: _tasks:
- "python .copier/update_dotenv.py" - ["{{ _copier_python }}", .copier/update_dotenv.py]
+12 -6
View File
@@ -183,22 +183,22 @@ There are already two environments configured, `staging` and `production`. 🚀
### Install GitHub Actions Runner ### Install GitHub Actions Runner
* On your remote server, if you are running as the `root` user, create a user for your GitHub Actions: * On your remote server, create a user for your GitHub Actions:
```bash ```bash
adduser github sudo adduser github
``` ```
* Add Docker permissions to the `github` user: * Add Docker permissions to the `github` user:
```bash ```bash
usermod -aG docker github sudo usermod -aG docker github
``` ```
* Temporarily switch to the `github` user: * Temporarily switch to the `github` user:
```bash ```bash
su - github sudo su - github
``` ```
* Go to the `github` user's home directory: * Go to the `github` user's home directory:
@@ -219,9 +219,15 @@ To make sure it runs on startup and continues running, you can install it as a s
exit exit
``` ```
After you do it, you would be on the `root` user again. And you will be on the previous directory, belonging to the `root` user. After you do it, you will be on the previous user again. And you will be on the previous directory, belonging to that user.
* Go to the `actions-runner` directory inside of the `github` user's home directory: Before being able to go the `github` user directory, you need to become the `root` user (you might already be):
```bash
sudo su
```
* As the `root` user, go to the `actions-runner` directory inside of the `github` user's home directory:
```bash ```bash
cd /home/github/actions-runner cd /home/github/actions-runner
+6 -2
View File
@@ -186,6 +186,8 @@ Adminer: http://localhost:8080
Traefik UI: http://localhost:8090 Traefik UI: http://localhost:8090
MailCatcher: http://localhost:1080
### Development URLs with `localhost.tiangolo.com` Configured ### Development URLs with `localhost.tiangolo.com` Configured
Development URLs, for local development. Development URLs, for local development.
@@ -194,10 +196,12 @@ Frontend: http://dashboard.localhost.tiangolo.com
Backend: http://api.localhost.tiangolo.com Backend: http://api.localhost.tiangolo.com
Automatic Interactive Docs (Swagger UI): http://api.localhost.tiangolo.comdocs Automatic Interactive Docs (Swagger UI): http://api.localhost.tiangolo.com/docs
Automatic Alternative Docs (ReDoc): http://api.localhost.tiangolo.comredoc Automatic Alternative Docs (ReDoc): http://api.localhost.tiangolo.com/redoc
Adminer: http://localhost.tiangolo.com:8080 Adminer: http://localhost.tiangolo.com:8080
Traefik UI: http://localhost.tiangolo.com:8090 Traefik UI: http://localhost.tiangolo.com:8090
MailCatcher: http://localhost.tiangolo.com:1080
+25
View File
@@ -102,6 +102,31 @@ services:
- VITE_API_URL=http://localhost:8000 - VITE_API_URL=http://localhost:8000
- NODE_ENV=development - NODE_ENV=development
playwright:
build:
context: ./frontend
dockerfile: Dockerfile.playwright
args:
- VITE_API_URL=http://backend:8000
- NODE_ENV=production
ipc: host
depends_on:
- backend
- mailcatcher
env_file:
- .env
environment:
- VITE_API_URL=http://backend:8000
- MAILCATCHER_HOST=http://mailcatcher:1080
# For the reports when run locally
- PLAYWRIGHT_HTML_HOST=0.0.0.0
- CI=${CI}
volumes:
- ./frontend/blob-report:/app/blob-report
- ./frontend/test-results:/app/test-results
ports:
- 9323:9323
networks: networks:
traefik-public: traefik-public:
# For local dev, don't expect an external Traefik network # For local dev, don't expect an external Traefik network
+1
View File
@@ -1 +1,2 @@
VITE_API_URL=http://localhost:8000 VITE_API_URL=http://localhost:8000
MAILCATCHER_HOST=http://localhost:1080
+1
View File
@@ -27,3 +27,4 @@ openapi.json
/playwright-report/ /playwright-report/
/blob-report/ /blob-report/
/playwright/.cache/ /playwright/.cache/
/playwright/.auth/
+13
View File
@@ -0,0 +1,13 @@
FROM node:20
WORKDIR /app
COPY package*.json /app/
RUN npm install
RUN npx -y playwright install --with-deps
COPY ./ /app/
ARG VITE_API_URL=${VITE_API_URL}
+2 -8
View File
@@ -59,7 +59,7 @@ If you are developing an API-only app and want to remove the frontend, you can d
* In the `docker-compose.yml` file, remove the whole service / section `frontend`. * In the `docker-compose.yml` file, remove the whole service / section `frontend`.
* In the `docker-compose.override.yml` file, remove the whole service / section `frontend`. * In the `docker-compose.override.yml` file, remove the whole service / section `frontend` and `playwright`.
Done, you have a frontend-less (api-only) app. 🤓 Done, you have a frontend-less (api-only) app. 🤓
@@ -80,7 +80,7 @@ But it would be only to clean them up, leaving them won't really have any effect
* From the top level project directory, run the script: * From the top level project directory, run the script:
```bash ```bash
./scripts/generate-frontend-client.sh ./scripts/generate-client.sh
``` ```
* Commit the changes. * Commit the changes.
@@ -91,12 +91,6 @@ But it would be only to clean them up, leaving them won't really have any effect
* Download the OpenAPI JSON file from `http://localhost/api/v1/openapi.json` and copy it to a new file `openapi.json` at the root of the `frontend` directory. * Download the OpenAPI JSON file from `http://localhost/api/v1/openapi.json` and copy it to a new file `openapi.json` at the root of the `frontend` directory.
* To simplify the names in the generated frontend client code, modify the `openapi.json` file by running the following script:
```bash
node modify-openapi-operationids.js
```
* To generate the frontend client, run: * To generate the frontend client, run:
```bash ```bash
-36
View File
@@ -1,36 +0,0 @@
import * as fs from "node:fs"
async function modifyOpenAPIFile(filePath) {
try {
const data = await fs.promises.readFile(filePath)
const openapiContent = JSON.parse(data)
const paths = openapiContent.paths
for (const pathKey of Object.keys(paths)) {
const pathData = paths[pathKey]
for (const method of Object.keys(pathData)) {
const operation = pathData[method]
if (operation.tags && operation.tags.length > 0) {
const tag = operation.tags[0]
const operationId = operation.operationId
const toRemove = `${tag}-`
if (operationId.startsWith(toRemove)) {
const newOperationId = operationId.substring(toRemove.length)
operation.operationId = newOperationId
}
}
}
}
await fs.promises.writeFile(
filePath,
JSON.stringify(openapiContent, null, 2),
)
console.log("File successfully modified")
} catch (err) {
console.error("Error:", err)
}
}
const filePath = "./openapi.json"
modifyOpenAPIFile(filePath)
+28
View File
@@ -0,0 +1,28 @@
import { defineConfig } from "@hey-api/openapi-ts"
export default defineConfig({
client: "legacy/axios",
input: "./openapi.json",
output: "./src/client",
// exportSchemas: true,
plugins: [
{
name: "@hey-api/sdk",
// NOTE: this doesn't allow tree-shaking
asClass: true,
operationId: true,
methodNameBuilder: (operation) => {
// @ts-ignore
let name: string = operation.name
// @ts-ignore
const service: string = operation.service
if (service && name.toLowerCase().startsWith(service.toLowerCase())) {
name = name.slice(service.length)
}
return name.charAt(0).toLowerCase() + name.slice(1)
},
},
],
})
+3395 -3141
View File
File diff suppressed because it is too large Load Diff
+8 -10
View File
@@ -5,31 +5,29 @@
"type": "module", "type": "module",
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",
"build": "tsc && vite build", "build": "tsc -p tsconfig.build.json && vite build",
"lint": "biome check --apply-unsafe --no-errors-on-unmatched --files-ignore-unknown=true ./", "lint": "biome check --apply-unsafe --no-errors-on-unmatched --files-ignore-unknown=true ./",
"preview": "vite preview", "preview": "vite preview",
"generate-client": "openapi-ts --input ./openapi.json --output ./src/client --client axios --exportSchemas true" "generate-client": "openapi-ts"
}, },
"dependencies": { "dependencies": {
"@chakra-ui/icons": "2.1.1", "@chakra-ui/react": "^3.8.0",
"@chakra-ui/react": "2.8.2", "@emotion/react": "^11.14.0",
"@emotion/react": "11.11.3",
"@emotion/styled": "11.11.0",
"@tanstack/react-query": "^5.28.14", "@tanstack/react-query": "^5.28.14",
"@tanstack/react-query-devtools": "^5.28.14", "@tanstack/react-query-devtools": "^5.28.14",
"@tanstack/react-router": "1.19.1", "@tanstack/react-router": "1.19.1",
"axios": "1.7.4", "axios": "1.7.4",
"form-data": "4.0.0", "form-data": "4.0.0",
"framer-motion": "10.16.16", "next-themes": "^0.4.4",
"react": "^18.2.0", "react": "^18.2.0",
"react-dom": "^18.2.0", "react-dom": "^18.2.0",
"react-error-boundary": "^4.0.13", "react-error-boundary": "^4.0.13",
"react-hook-form": "7.49.3", "react-hook-form": "7.49.3",
"react-icons": "5.0.1" "react-icons": "^5.4.0"
}, },
"devDependencies": { "devDependencies": {
"@biomejs/biome": "1.6.1", "@biomejs/biome": "1.6.1",
"@hey-api/openapi-ts": "^0.34.1", "@hey-api/openapi-ts": "^0.57.0",
"@playwright/test": "^1.45.2", "@playwright/test": "^1.45.2",
"@tanstack/router-devtools": "1.19.1", "@tanstack/router-devtools": "1.19.1",
"@tanstack/router-vite-plugin": "1.19.0", "@tanstack/router-vite-plugin": "1.19.0",
@@ -39,6 +37,6 @@
"@vitejs/plugin-react-swc": "^3.5.0", "@vitejs/plugin-react-swc": "^3.5.0",
"dotenv": "^16.4.5", "dotenv": "^16.4.5",
"typescript": "^5.2.2", "typescript": "^5.2.2",
"vite": "^5.0.13" "vite": "^5.4.14"
} }
} }
+2 -3
View File
@@ -1,11 +1,10 @@
import { defineConfig, devices } from '@playwright/test'; import { defineConfig, devices } from '@playwright/test';
import 'dotenv/config'
/** /**
* Read environment variables from file. * Read environment variables from file.
* https://github.com/motdotla/dotenv * https://github.com/motdotla/dotenv
*/ */
// require('dotenv').config();
/** /**
* See https://playwright.dev/docs/test-configuration. * See https://playwright.dev/docs/test-configuration.
@@ -21,7 +20,7 @@ export default defineConfig({
/* Opt out of parallel tests on CI. */ /* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined, workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */ /* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: 'html', reporter: process.env.CI ? 'blob' : 'html',
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: { use: {
/* Base URL to use in actions like `await page.goto('/')`. */ /* Base URL to use in actions like `await page.goto('/')`. */
+17 -16
View File
@@ -1,20 +1,21 @@
export type ApiRequestOptions = { export type ApiRequestOptions<T = unknown> = {
readonly method:
| "GET"
| "PUT"
| "POST"
| "DELETE"
| "OPTIONS"
| "HEAD"
| "PATCH"
readonly url: string
readonly path?: Record<string, unknown>
readonly cookies?: Record<string, unknown>
readonly headers?: Record<string, unknown>
readonly query?: Record<string, unknown>
readonly formData?: Record<string, unknown>
readonly body?: any readonly body?: any
readonly cookies?: Record<string, unknown>
readonly errors?: Record<number | string, string>
readonly formData?: Record<string, unknown> | any[] | Blob | File
readonly headers?: Record<string, unknown>
readonly mediaType?: string readonly mediaType?: string
readonly method:
| "DELETE"
| "GET"
| "HEAD"
| "OPTIONS"
| "PATCH"
| "POST"
| "PUT"
readonly path?: Record<string, unknown>
readonly query?: Record<string, unknown>
readonly responseHeader?: string readonly responseHeader?: string
readonly errors?: Record<number, string> readonly responseTransformer?: (data: unknown) => Promise<T>
readonly url: string
} }
+7 -7
View File
@@ -1,10 +1,9 @@
import type { AxiosRequestConfig, AxiosResponse } from "axios" import type { AxiosRequestConfig, AxiosResponse } from "axios"
import type { ApiRequestOptions } from "./ApiRequestOptions" import type { ApiRequestOptions } from "./ApiRequestOptions"
import type { TResult } from "./types"
type Headers = Record<string, string> type Headers = Record<string, string>
type Middleware<T> = (value: T) => T | Promise<T> type Middleware<T> = (value: T) => T | Promise<T>
type Resolver<T> = (options: ApiRequestOptions) => Promise<T> type Resolver<T> = (options: ApiRequestOptions<T>) => Promise<T>
export class Interceptors<T> { export class Interceptors<T> {
_fns: Middleware<T>[] _fns: Middleware<T>[]
@@ -13,14 +12,14 @@ export class Interceptors<T> {
this._fns = [] this._fns = []
} }
eject(fn: Middleware<T>) { eject(fn: Middleware<T>): void {
const index = this._fns.indexOf(fn) const index = this._fns.indexOf(fn)
if (index !== -1) { if (index !== -1) {
this._fns = [...this._fns.slice(0, index), ...this._fns.slice(index + 1)] this._fns = [...this._fns.slice(0, index), ...this._fns.slice(index + 1)]
} }
} }
use(fn: Middleware<T>) { use(fn: Middleware<T>): void {
this._fns = [...this._fns, fn] this._fns = [...this._fns, fn]
} }
} }
@@ -31,7 +30,6 @@ export type OpenAPIConfig = {
ENCODE_PATH?: ((path: string) => string) | undefined ENCODE_PATH?: ((path: string) => string) | undefined
HEADERS?: Headers | Resolver<Headers> | undefined HEADERS?: Headers | Resolver<Headers> | undefined
PASSWORD?: string | Resolver<string> | undefined PASSWORD?: string | Resolver<string> | undefined
RESULT?: TResult
TOKEN?: string | Resolver<string> | undefined TOKEN?: string | Resolver<string> | undefined
USERNAME?: string | Resolver<string> | undefined USERNAME?: string | Resolver<string> | undefined
VERSION: string VERSION: string
@@ -48,10 +46,12 @@ export const OpenAPI: OpenAPIConfig = {
ENCODE_PATH: undefined, ENCODE_PATH: undefined,
HEADERS: undefined, HEADERS: undefined,
PASSWORD: undefined, PASSWORD: undefined,
RESULT: "body",
TOKEN: undefined, TOKEN: undefined,
USERNAME: undefined, USERNAME: undefined,
VERSION: "0.1.0", VERSION: "0.1.0",
WITH_CREDENTIALS: false, WITH_CREDENTIALS: false,
interceptors: { request: new Interceptors(), response: new Interceptors() }, interceptors: {
request: new Interceptors(),
response: new Interceptors(),
},
} }
+19 -8
View File
@@ -54,7 +54,9 @@ export const getQueryString = (params: Record<string, unknown>): string => {
return return
} }
if (Array.isArray(value)) { if (value instanceof Date) {
append(key, value.toISOString())
} else if (Array.isArray(value)) {
value.forEach((v) => encodePair(key, v)) value.forEach((v) => encodePair(key, v))
} else if (typeof value === "object") { } else if (typeof value === "object") {
Object.entries(value).forEach(([k, v]) => encodePair(`${key}[${k}]`, v)) Object.entries(value).forEach(([k, v]) => encodePair(`${key}[${k}]`, v))
@@ -113,10 +115,10 @@ export const getFormData = (
return undefined return undefined
} }
type Resolver<T> = (options: ApiRequestOptions) => Promise<T> type Resolver<T> = (options: ApiRequestOptions<T>) => Promise<T>
export const resolve = async <T>( export const resolve = async <T>(
options: ApiRequestOptions, options: ApiRequestOptions<T>,
resolver?: T | Resolver<T>, resolver?: T | Resolver<T>,
): Promise<T | undefined> => { ): Promise<T | undefined> => {
if (typeof resolver === "function") { if (typeof resolver === "function") {
@@ -125,14 +127,18 @@ export const resolve = async <T>(
return resolver return resolver
} }
export const getHeaders = async ( export const getHeaders = async <T>(
config: OpenAPIConfig, config: OpenAPIConfig,
options: ApiRequestOptions, options: ApiRequestOptions<T>,
): Promise<Record<string, string>> => { ): Promise<Record<string, string>> => {
const [token, username, password, additionalHeaders] = await Promise.all([ const [token, username, password, additionalHeaders] = await Promise.all([
// @ts-ignore
resolve(options, config.TOKEN), resolve(options, config.TOKEN),
// @ts-ignore
resolve(options, config.USERNAME), resolve(options, config.USERNAME),
// @ts-ignore
resolve(options, config.PASSWORD), resolve(options, config.PASSWORD),
// @ts-ignore
resolve(options, config.HEADERS), resolve(options, config.HEADERS),
]) ])
@@ -187,7 +193,7 @@ export const getRequestBody = (options: ApiRequestOptions): unknown => {
export const sendRequest = async <T>( export const sendRequest = async <T>(
config: OpenAPIConfig, config: OpenAPIConfig,
options: ApiRequestOptions, options: ApiRequestOptions<T>,
url: string, url: string,
body: unknown, body: unknown,
formData: FormData | undefined, formData: FormData | undefined,
@@ -325,7 +331,7 @@ export const catchErrorCodes = (
*/ */
export const request = <T>( export const request = <T>(
config: OpenAPIConfig, config: OpenAPIConfig,
options: ApiRequestOptions, options: ApiRequestOptions<T>,
axiosClient: AxiosInstance = axios, axiosClient: AxiosInstance = axios,
): CancelablePromise<T> => { ): CancelablePromise<T> => {
return new CancelablePromise(async (resolve, reject, onCancel) => { return new CancelablePromise(async (resolve, reject, onCancel) => {
@@ -357,12 +363,17 @@ export const request = <T>(
options.responseHeader, options.responseHeader,
) )
let transformedBody = responseBody
if (options.responseTransformer && isSuccess(response.status)) {
transformedBody = await options.responseTransformer(responseBody)
}
const result: ApiResult = { const result: ApiResult = {
url, url,
ok: isSuccess(response.status), ok: isSuccess(response.status),
status: response.status, status: response.status,
statusText: response.statusText, statusText: response.statusText,
body: responseHeader ?? responseBody, body: responseHeader ?? transformedBody,
} }
catchErrorCodes(options, result) catchErrorCodes(options, result)
-14
View File
@@ -1,14 +0,0 @@
import type { ApiResult } from "./ApiResult"
export type TResult = "body" | "raw"
export type TApiResponse<T extends TResult, TData> = Exclude<
T,
"raw"
> extends never
? ApiResult<TData>
: ApiResult<TData>["body"]
export type TConfig<T extends TResult> = {
_result?: T
}
+4 -6
View File
@@ -1,8 +1,6 @@
// This file is auto-generated by @hey-api/openapi-ts
export { ApiError } from "./core/ApiError" export { ApiError } from "./core/ApiError"
export { CancelablePromise, CancelError } from "./core/CancelablePromise" export { CancelablePromise, CancelError } from "./core/CancelablePromise"
export { OpenAPI } from "./core/OpenAPI" export { OpenAPI, type OpenAPIConfig } from "./core/OpenAPI"
export type { OpenAPIConfig } from "./core/OpenAPI" export * from "./sdk.gen"
export * from "./types.gen"
export * from "./models"
export * from "./schemas"
export * from "./services"
-99
View File
@@ -1,99 +0,0 @@
export type Body_login_login_access_token = {
grant_type?: string | null
username: string
password: string
scope?: string
client_id?: string | null
client_secret?: string | null
}
export type HTTPValidationError = {
detail?: Array<ValidationError>
}
export type ItemCreate = {
title: string
description?: string | null
}
export type ItemPublic = {
title: string
description?: string | null
id: string
owner_id: string
}
export type ItemUpdate = {
title?: string | null
description?: string | null
}
export type ItemsPublic = {
data: Array<ItemPublic>
count: number
}
export type Message = {
message: string
}
export type NewPassword = {
token: string
new_password: string
}
export type Token = {
access_token: string
token_type?: string
}
export type UpdatePassword = {
current_password: string
new_password: string
}
export type UserCreate = {
email: string
is_active?: boolean
is_superuser?: boolean
full_name?: string | null
password: string
}
export type UserPublic = {
email: string
is_active?: boolean
is_superuser?: boolean
full_name?: string | null
id: string
}
export type UserRegister = {
email: string
password: string
full_name?: string | null
}
export type UserUpdate = {
email?: string | null
is_active?: boolean
is_superuser?: boolean
full_name?: string | null
password?: string | null
}
export type UserUpdateMe = {
full_name?: string | null
email?: string | null
}
export type UsersPublic = {
data: Array<UserPublic>
count: number
}
export type ValidationError = {
loc: Array<string | number>
msg: string
type: string
}
@@ -1,8 +1,9 @@
export const $Body_login_login_access_token = { // This file is auto-generated by @hey-api/openapi-ts
export const Body_login_login_access_tokenSchema = {
properties: { properties: {
grant_type: { grant_type: {
type: "any-of", anyOf: [
contains: [
{ {
type: "string", type: "string",
pattern: "password", pattern: "password",
@@ -11,22 +12,23 @@ export const $Body_login_login_access_token = {
type: "null", type: "null",
}, },
], ],
title: "Grant Type",
}, },
username: { username: {
type: "string", type: "string",
isRequired: true, title: "Username",
}, },
password: { password: {
type: "string", type: "string",
isRequired: true, title: "Password",
}, },
scope: { scope: {
type: "string", type: "string",
title: "Scope",
default: "", default: "",
}, },
client_id: { client_id: {
type: "any-of", anyOf: [
contains: [
{ {
type: "string", type: "string",
}, },
@@ -34,10 +36,10 @@ export const $Body_login_login_access_token = {
type: "null", type: "null",
}, },
], ],
title: "Client Id",
}, },
client_secret: { client_secret: {
type: "any-of", anyOf: [
contains: [
{ {
type: "string", type: "string",
}, },
@@ -45,32 +47,38 @@ export const $Body_login_login_access_token = {
type: "null", type: "null",
}, },
], ],
title: "Client Secret",
}, },
}, },
type: "object",
required: ["username", "password"],
title: "Body_login-login_access_token",
} as const } as const
export const $HTTPValidationError = { export const HTTPValidationErrorSchema = {
properties: { properties: {
detail: { detail: {
items: {
$ref: "#/components/schemas/ValidationError",
},
type: "array", type: "array",
contains: { title: "Detail",
type: "ValidationError",
},
}, },
}, },
type: "object",
title: "HTTPValidationError",
} as const } as const
export const $ItemCreate = { export const ItemCreateSchema = {
properties: { properties: {
title: { title: {
type: "string", type: "string",
isRequired: true,
maxLength: 255, maxLength: 255,
minLength: 1, minLength: 1,
title: "Title",
}, },
description: { description: {
type: "any-of", anyOf: [
contains: [
{ {
type: "string", type: "string",
maxLength: 255, maxLength: 255,
@@ -79,21 +87,24 @@ export const $ItemCreate = {
type: "null", type: "null",
}, },
], ],
title: "Description",
}, },
}, },
type: "object",
required: ["title"],
title: "ItemCreate",
} as const } as const
export const $ItemPublic = { export const ItemPublicSchema = {
properties: { properties: {
title: { title: {
type: "string", type: "string",
isRequired: true,
maxLength: 255, maxLength: 255,
minLength: 1, minLength: 1,
title: "Title",
}, },
description: { description: {
type: "any-of", anyOf: [
contains: [
{ {
type: "string", type: "string",
maxLength: 255, maxLength: 255,
@@ -102,25 +113,28 @@ export const $ItemPublic = {
type: "null", type: "null",
}, },
], ],
title: "Description",
}, },
id: { id: {
type: "string", type: "string",
isRequired: true,
format: "uuid", format: "uuid",
title: "Id",
}, },
owner_id: { owner_id: {
type: "string", type: "string",
isRequired: true,
format: "uuid", format: "uuid",
title: "Owner Id",
}, },
}, },
type: "object",
required: ["title", "id", "owner_id"],
title: "ItemPublic",
} as const } as const
export const $ItemUpdate = { export const ItemUpdateSchema = {
properties: { properties: {
title: { title: {
type: "any-of", anyOf: [
contains: [
{ {
type: "string", type: "string",
maxLength: 255, maxLength: 255,
@@ -130,10 +144,10 @@ export const $ItemUpdate = {
type: "null", type: "null",
}, },
], ],
title: "Title",
}, },
description: { description: {
type: "any-of", anyOf: [
contains: [
{ {
type: "string", type: "string",
maxLength: 255, maxLength: 255,
@@ -142,99 +156,119 @@ export const $ItemUpdate = {
type: "null", type: "null",
}, },
], ],
title: "Description",
}, },
}, },
type: "object",
title: "ItemUpdate",
} as const } as const
export const $ItemsPublic = { export const ItemsPublicSchema = {
properties: { properties: {
data: { data: {
type: "array", items: {
contains: { $ref: "#/components/schemas/ItemPublic",
type: "ItemPublic",
}, },
isRequired: true, type: "array",
title: "Data",
}, },
count: { count: {
type: "number", type: "integer",
isRequired: true, title: "Count",
}, },
}, },
type: "object",
required: ["data", "count"],
title: "ItemsPublic",
} as const } as const
export const $Message = { export const MessageSchema = {
properties: { properties: {
message: { message: {
type: "string", type: "string",
isRequired: true, title: "Message",
}, },
}, },
type: "object",
required: ["message"],
title: "Message",
} as const } as const
export const $NewPassword = { export const NewPasswordSchema = {
properties: { properties: {
token: { token: {
type: "string", type: "string",
isRequired: true, title: "Token",
}, },
new_password: { new_password: {
type: "string", type: "string",
isRequired: true,
maxLength: 40, maxLength: 40,
minLength: 8, minLength: 8,
title: "New Password",
}, },
}, },
type: "object",
required: ["token", "new_password"],
title: "NewPassword",
} as const } as const
export const $Token = { export const TokenSchema = {
properties: { properties: {
access_token: { access_token: {
type: "string", type: "string",
isRequired: true, title: "Access Token",
}, },
token_type: { token_type: {
type: "string", type: "string",
title: "Token Type",
default: "bearer", default: "bearer",
}, },
}, },
type: "object",
required: ["access_token"],
title: "Token",
} as const } as const
export const $UpdatePassword = { export const UpdatePasswordSchema = {
properties: { properties: {
current_password: { current_password: {
type: "string", type: "string",
isRequired: true,
maxLength: 40, maxLength: 40,
minLength: 8, minLength: 8,
title: "Current Password",
}, },
new_password: { new_password: {
type: "string", type: "string",
isRequired: true,
maxLength: 40, maxLength: 40,
minLength: 8, minLength: 8,
title: "New Password",
}, },
}, },
type: "object",
required: ["current_password", "new_password"],
title: "UpdatePassword",
} as const } as const
export const $UserCreate = { export const UserCreateSchema = {
properties: { properties: {
email: { email: {
type: "string", type: "string",
isRequired: true,
format: "email",
maxLength: 255, maxLength: 255,
format: "email",
title: "Email",
}, },
is_active: { is_active: {
type: "boolean", type: "boolean",
title: "Is Active",
default: true, default: true,
}, },
is_superuser: { is_superuser: {
type: "boolean", type: "boolean",
title: "Is Superuser",
default: false, default: false,
}, },
full_name: { full_name: {
type: "any-of", anyOf: [
contains: [
{ {
type: "string", type: "string",
maxLength: 255, maxLength: 255,
@@ -243,35 +277,40 @@ export const $UserCreate = {
type: "null", type: "null",
}, },
], ],
title: "Full Name",
}, },
password: { password: {
type: "string", type: "string",
isRequired: true,
maxLength: 40, maxLength: 40,
minLength: 8, minLength: 8,
title: "Password",
}, },
}, },
type: "object",
required: ["email", "password"],
title: "UserCreate",
} as const } as const
export const $UserPublic = { export const UserPublicSchema = {
properties: { properties: {
email: { email: {
type: "string", type: "string",
isRequired: true,
format: "email",
maxLength: 255, maxLength: 255,
format: "email",
title: "Email",
}, },
is_active: { is_active: {
type: "boolean", type: "boolean",
title: "Is Active",
default: true, default: true,
}, },
is_superuser: { is_superuser: {
type: "boolean", type: "boolean",
title: "Is Superuser",
default: false, default: false,
}, },
full_name: { full_name: {
type: "any-of", anyOf: [
contains: [
{ {
type: "string", type: "string",
maxLength: 255, maxLength: 255,
@@ -280,32 +319,35 @@ export const $UserPublic = {
type: "null", type: "null",
}, },
], ],
title: "Full Name",
}, },
id: { id: {
type: "string", type: "string",
isRequired: true,
format: "uuid", format: "uuid",
title: "Id",
}, },
}, },
type: "object",
required: ["email", "id"],
title: "UserPublic",
} as const } as const
export const $UserRegister = { export const UserRegisterSchema = {
properties: { properties: {
email: { email: {
type: "string", type: "string",
isRequired: true,
format: "email",
maxLength: 255, maxLength: 255,
format: "email",
title: "Email",
}, },
password: { password: {
type: "string", type: "string",
isRequired: true,
maxLength: 40, maxLength: 40,
minLength: 8, minLength: 8,
title: "Password",
}, },
full_name: { full_name: {
type: "any-of", anyOf: [
contains: [
{ {
type: "string", type: "string",
maxLength: 255, maxLength: 255,
@@ -314,36 +356,41 @@ export const $UserRegister = {
type: "null", type: "null",
}, },
], ],
title: "Full Name",
}, },
}, },
type: "object",
required: ["email", "password"],
title: "UserRegister",
} as const } as const
export const $UserUpdate = { export const UserUpdateSchema = {
properties: { properties: {
email: { email: {
type: "any-of", anyOf: [
contains: [
{ {
type: "string", type: "string",
format: "email",
maxLength: 255, maxLength: 255,
format: "email",
}, },
{ {
type: "null", type: "null",
}, },
], ],
title: "Email",
}, },
is_active: { is_active: {
type: "boolean", type: "boolean",
title: "Is Active",
default: true, default: true,
}, },
is_superuser: { is_superuser: {
type: "boolean", type: "boolean",
title: "Is Superuser",
default: false, default: false,
}, },
full_name: { full_name: {
type: "any-of", anyOf: [
contains: [
{ {
type: "string", type: "string",
maxLength: 255, maxLength: 255,
@@ -352,10 +399,10 @@ export const $UserUpdate = {
type: "null", type: "null",
}, },
], ],
title: "Full Name",
}, },
password: { password: {
type: "any-of", anyOf: [
contains: [
{ {
type: "string", type: "string",
maxLength: 40, maxLength: 40,
@@ -365,15 +412,17 @@ export const $UserUpdate = {
type: "null", type: "null",
}, },
], ],
title: "Password",
}, },
}, },
type: "object",
title: "UserUpdate",
} as const } as const
export const $UserUpdateMe = { export const UserUpdateMeSchema = {
properties: { properties: {
full_name: { full_name: {
type: "any-of", anyOf: [
contains: [
{ {
type: "string", type: "string",
maxLength: 255, maxLength: 255,
@@ -382,63 +431,71 @@ export const $UserUpdateMe = {
type: "null", type: "null",
}, },
], ],
title: "Full Name",
}, },
email: { email: {
type: "any-of", anyOf: [
contains: [
{ {
type: "string", type: "string",
format: "email",
maxLength: 255, maxLength: 255,
format: "email",
}, },
{ {
type: "null", type: "null",
}, },
], ],
title: "Email",
}, },
}, },
type: "object",
title: "UserUpdateMe",
} as const } as const
export const $UsersPublic = { export const UsersPublicSchema = {
properties: { properties: {
data: { data: {
type: "array", items: {
contains: { $ref: "#/components/schemas/UserPublic",
type: "UserPublic",
}, },
isRequired: true, type: "array",
title: "Data",
}, },
count: { count: {
type: "number", type: "integer",
isRequired: true, title: "Count",
}, },
}, },
type: "object",
required: ["data", "count"],
title: "UsersPublic",
} as const } as const
export const $ValidationError = { export const ValidationErrorSchema = {
properties: { properties: {
loc: { loc: {
type: "array", items: {
contains: { anyOf: [
type: "any-of",
contains: [
{ {
type: "string", type: "string",
}, },
{ {
type: "number", type: "integer",
}, },
], ],
}, },
isRequired: true, type: "array",
title: "Location",
}, },
msg: { msg: {
type: "string", type: "string",
isRequired: true, title: "Message",
}, },
type: { type: {
type: "string", type: "string",
isRequired: true, title: "Error Type",
}, },
}, },
type: "object",
required: ["loc", "msg", "type"],
title: "ValidationError",
} as const } as const
@@ -1,56 +1,191 @@
// This file is auto-generated by @hey-api/openapi-ts
import type { CancelablePromise } from "./core/CancelablePromise" import type { CancelablePromise } from "./core/CancelablePromise"
import { OpenAPI } from "./core/OpenAPI" import { OpenAPI } from "./core/OpenAPI"
import { request as __request } from "./core/request" import { request as __request } from "./core/request"
import type { import type {
Body_login_login_access_token, ItemsReadItemsData,
Message, ItemsReadItemsResponse,
NewPassword, ItemsCreateItemData,
Token, ItemsCreateItemResponse,
UserPublic, ItemsReadItemData,
UpdatePassword, ItemsReadItemResponse,
UserCreate, ItemsUpdateItemData,
UserRegister, ItemsUpdateItemResponse,
UsersPublic, ItemsDeleteItemData,
UserUpdate, ItemsDeleteItemResponse,
UserUpdateMe, LoginLoginAccessTokenData,
ItemCreate, LoginLoginAccessTokenResponse,
ItemPublic, LoginTestTokenResponse,
ItemsPublic, LoginRecoverPasswordData,
ItemUpdate, LoginRecoverPasswordResponse,
} from "./models" LoginResetPasswordData,
LoginResetPasswordResponse,
LoginRecoverPasswordHtmlContentData,
LoginRecoverPasswordHtmlContentResponse,
UsersReadUsersData,
UsersReadUsersResponse,
UsersCreateUserData,
UsersCreateUserResponse,
UsersReadUserMeResponse,
UsersDeleteUserMeResponse,
UsersUpdateUserMeData,
UsersUpdateUserMeResponse,
UsersUpdatePasswordMeData,
UsersUpdatePasswordMeResponse,
UsersRegisterUserData,
UsersRegisterUserResponse,
UsersReadUserByIdData,
UsersReadUserByIdResponse,
UsersUpdateUserData,
UsersUpdateUserResponse,
UsersDeleteUserData,
UsersDeleteUserResponse,
UtilsTestEmailData,
UtilsTestEmailResponse,
UtilsHealthCheckResponse,
} from "./types.gen"
export type TDataLoginAccessToken = { export class ItemsService {
formData: Body_login_login_access_token /**
} * Read Items
export type TDataRecoverPassword = { * Retrieve items.
email: string * @param data The data for the request.
} * @param data.skip
export type TDataResetPassword = { * @param data.limit
requestBody: NewPassword * @returns ItemsPublic Successful Response
} * @throws ApiError
export type TDataRecoverPasswordHtmlContent = { */
email: string public static readItems(
data: ItemsReadItemsData = {},
): CancelablePromise<ItemsReadItemsResponse> {
return __request(OpenAPI, {
method: "GET",
url: "/api/v1/items/",
query: {
skip: data.skip,
limit: data.limit,
},
errors: {
422: "Validation Error",
},
})
}
/**
* Create Item
* Create new item.
* @param data The data for the request.
* @param data.requestBody
* @returns ItemPublic Successful Response
* @throws ApiError
*/
public static createItem(
data: ItemsCreateItemData,
): CancelablePromise<ItemsCreateItemResponse> {
return __request(OpenAPI, {
method: "POST",
url: "/api/v1/items/",
body: data.requestBody,
mediaType: "application/json",
errors: {
422: "Validation Error",
},
})
}
/**
* Read Item
* Get item by ID.
* @param data The data for the request.
* @param data.id
* @returns ItemPublic Successful Response
* @throws ApiError
*/
public static readItem(
data: ItemsReadItemData,
): CancelablePromise<ItemsReadItemResponse> {
return __request(OpenAPI, {
method: "GET",
url: "/api/v1/items/{id}",
path: {
id: data.id,
},
errors: {
422: "Validation Error",
},
})
}
/**
* Update Item
* Update an item.
* @param data The data for the request.
* @param data.id
* @param data.requestBody
* @returns ItemPublic Successful Response
* @throws ApiError
*/
public static updateItem(
data: ItemsUpdateItemData,
): CancelablePromise<ItemsUpdateItemResponse> {
return __request(OpenAPI, {
method: "PUT",
url: "/api/v1/items/{id}",
path: {
id: data.id,
},
body: data.requestBody,
mediaType: "application/json",
errors: {
422: "Validation Error",
},
})
}
/**
* Delete Item
* Delete an item.
* @param data The data for the request.
* @param data.id
* @returns Message Successful Response
* @throws ApiError
*/
public static deleteItem(
data: ItemsDeleteItemData,
): CancelablePromise<ItemsDeleteItemResponse> {
return __request(OpenAPI, {
method: "DELETE",
url: "/api/v1/items/{id}",
path: {
id: data.id,
},
errors: {
422: "Validation Error",
},
})
}
} }
export class LoginService { export class LoginService {
/** /**
* Login Access Token * Login Access Token
* OAuth2 compatible token login, get an access token for future requests * OAuth2 compatible token login, get an access token for future requests
* @param data The data for the request.
* @param data.formData
* @returns Token Successful Response * @returns Token Successful Response
* @throws ApiError * @throws ApiError
*/ */
public static loginAccessToken( public static loginAccessToken(
data: TDataLoginAccessToken, data: LoginLoginAccessTokenData,
): CancelablePromise<Token> { ): CancelablePromise<LoginLoginAccessTokenResponse> {
const { formData } = data
return __request(OpenAPI, { return __request(OpenAPI, {
method: "POST", method: "POST",
url: "/api/v1/login/access-token", url: "/api/v1/login/access-token",
formData: formData, formData: data.formData,
mediaType: "application/x-www-form-urlencoded", mediaType: "application/x-www-form-urlencoded",
errors: { errors: {
422: `Validation Error`, 422: "Validation Error",
}, },
}) })
} }
@@ -61,7 +196,7 @@ export class LoginService {
* @returns UserPublic Successful Response * @returns UserPublic Successful Response
* @throws ApiError * @throws ApiError
*/ */
public static testToken(): CancelablePromise<UserPublic> { public static testToken(): CancelablePromise<LoginTestTokenResponse> {
return __request(OpenAPI, { return __request(OpenAPI, {
method: "POST", method: "POST",
url: "/api/v1/login/test-token", url: "/api/v1/login/test-token",
@@ -71,21 +206,22 @@ export class LoginService {
/** /**
* Recover Password * Recover Password
* Password Recovery * Password Recovery
* @param data The data for the request.
* @param data.email
* @returns Message Successful Response * @returns Message Successful Response
* @throws ApiError * @throws ApiError
*/ */
public static recoverPassword( public static recoverPassword(
data: TDataRecoverPassword, data: LoginRecoverPasswordData,
): CancelablePromise<Message> { ): CancelablePromise<LoginRecoverPasswordResponse> {
const { email } = data
return __request(OpenAPI, { return __request(OpenAPI, {
method: "POST", method: "POST",
url: "/api/v1/password-recovery/{email}", url: "/api/v1/password-recovery/{email}",
path: { path: {
email, email: data.email,
}, },
errors: { errors: {
422: `Validation Error`, 422: "Validation Error",
}, },
}) })
} }
@@ -93,20 +229,21 @@ export class LoginService {
/** /**
* Reset Password * Reset Password
* Reset password * Reset password
* @param data The data for the request.
* @param data.requestBody
* @returns Message Successful Response * @returns Message Successful Response
* @throws ApiError * @throws ApiError
*/ */
public static resetPassword( public static resetPassword(
data: TDataResetPassword, data: LoginResetPasswordData,
): CancelablePromise<Message> { ): CancelablePromise<LoginResetPasswordResponse> {
const { requestBody } = data
return __request(OpenAPI, { return __request(OpenAPI, {
method: "POST", method: "POST",
url: "/api/v1/reset-password/", url: "/api/v1/reset-password/",
body: requestBody, body: data.requestBody,
mediaType: "application/json", mediaType: "application/json",
errors: { errors: {
422: `Validation Error`, 422: "Validation Error",
}, },
}) })
} }
@@ -114,73 +251,49 @@ export class LoginService {
/** /**
* Recover Password Html Content * Recover Password Html Content
* HTML Content for Password Recovery * HTML Content for Password Recovery
* @param data The data for the request.
* @param data.email
* @returns string Successful Response * @returns string Successful Response
* @throws ApiError * @throws ApiError
*/ */
public static recoverPasswordHtmlContent( public static recoverPasswordHtmlContent(
data: TDataRecoverPasswordHtmlContent, data: LoginRecoverPasswordHtmlContentData,
): CancelablePromise<string> { ): CancelablePromise<LoginRecoverPasswordHtmlContentResponse> {
const { email } = data
return __request(OpenAPI, { return __request(OpenAPI, {
method: "POST", method: "POST",
url: "/api/v1/password-recovery-html-content/{email}", url: "/api/v1/password-recovery-html-content/{email}",
path: { path: {
email, email: data.email,
}, },
errors: { errors: {
422: `Validation Error`, 422: "Validation Error",
}, },
}) })
} }
} }
export type TDataReadUsers = {
limit?: number
skip?: number
}
export type TDataCreateUser = {
requestBody: UserCreate
}
export type TDataUpdateUserMe = {
requestBody: UserUpdateMe
}
export type TDataUpdatePasswordMe = {
requestBody: UpdatePassword
}
export type TDataRegisterUser = {
requestBody: UserRegister
}
export type TDataReadUserById = {
userId: string
}
export type TDataUpdateUser = {
requestBody: UserUpdate
userId: string
}
export type TDataDeleteUser = {
userId: string
}
export class UsersService { export class UsersService {
/** /**
* Read Users * Read Users
* Retrieve users. * Retrieve users.
* @param data The data for the request.
* @param data.skip
* @param data.limit
* @returns UsersPublic Successful Response * @returns UsersPublic Successful Response
* @throws ApiError * @throws ApiError
*/ */
public static readUsers( public static readUsers(
data: TDataReadUsers = {}, data: UsersReadUsersData = {},
): CancelablePromise<UsersPublic> { ): CancelablePromise<UsersReadUsersResponse> {
const { limit = 100, skip = 0 } = data
return __request(OpenAPI, { return __request(OpenAPI, {
method: "GET", method: "GET",
url: "/api/v1/users/", url: "/api/v1/users/",
query: { query: {
skip, skip: data.skip,
limit, limit: data.limit,
}, },
errors: { errors: {
422: `Validation Error`, 422: "Validation Error",
}, },
}) })
} }
@@ -188,20 +301,21 @@ export class UsersService {
/** /**
* Create User * Create User
* Create new user. * Create new user.
* @param data The data for the request.
* @param data.requestBody
* @returns UserPublic Successful Response * @returns UserPublic Successful Response
* @throws ApiError * @throws ApiError
*/ */
public static createUser( public static createUser(
data: TDataCreateUser, data: UsersCreateUserData,
): CancelablePromise<UserPublic> { ): CancelablePromise<UsersCreateUserResponse> {
const { requestBody } = data
return __request(OpenAPI, { return __request(OpenAPI, {
method: "POST", method: "POST",
url: "/api/v1/users/", url: "/api/v1/users/",
body: requestBody, body: data.requestBody,
mediaType: "application/json", mediaType: "application/json",
errors: { errors: {
422: `Validation Error`, 422: "Validation Error",
}, },
}) })
} }
@@ -212,7 +326,7 @@ export class UsersService {
* @returns UserPublic Successful Response * @returns UserPublic Successful Response
* @throws ApiError * @throws ApiError
*/ */
public static readUserMe(): CancelablePromise<UserPublic> { public static readUserMe(): CancelablePromise<UsersReadUserMeResponse> {
return __request(OpenAPI, { return __request(OpenAPI, {
method: "GET", method: "GET",
url: "/api/v1/users/me", url: "/api/v1/users/me",
@@ -225,7 +339,7 @@ export class UsersService {
* @returns Message Successful Response * @returns Message Successful Response
* @throws ApiError * @throws ApiError
*/ */
public static deleteUserMe(): CancelablePromise<Message> { public static deleteUserMe(): CancelablePromise<UsersDeleteUserMeResponse> {
return __request(OpenAPI, { return __request(OpenAPI, {
method: "DELETE", method: "DELETE",
url: "/api/v1/users/me", url: "/api/v1/users/me",
@@ -235,20 +349,21 @@ export class UsersService {
/** /**
* Update User Me * Update User Me
* Update own user. * Update own user.
* @param data The data for the request.
* @param data.requestBody
* @returns UserPublic Successful Response * @returns UserPublic Successful Response
* @throws ApiError * @throws ApiError
*/ */
public static updateUserMe( public static updateUserMe(
data: TDataUpdateUserMe, data: UsersUpdateUserMeData,
): CancelablePromise<UserPublic> { ): CancelablePromise<UsersUpdateUserMeResponse> {
const { requestBody } = data
return __request(OpenAPI, { return __request(OpenAPI, {
method: "PATCH", method: "PATCH",
url: "/api/v1/users/me", url: "/api/v1/users/me",
body: requestBody, body: data.requestBody,
mediaType: "application/json", mediaType: "application/json",
errors: { errors: {
422: `Validation Error`, 422: "Validation Error",
}, },
}) })
} }
@@ -256,20 +371,21 @@ export class UsersService {
/** /**
* Update Password Me * Update Password Me
* Update own password. * Update own password.
* @param data The data for the request.
* @param data.requestBody
* @returns Message Successful Response * @returns Message Successful Response
* @throws ApiError * @throws ApiError
*/ */
public static updatePasswordMe( public static updatePasswordMe(
data: TDataUpdatePasswordMe, data: UsersUpdatePasswordMeData,
): CancelablePromise<Message> { ): CancelablePromise<UsersUpdatePasswordMeResponse> {
const { requestBody } = data
return __request(OpenAPI, { return __request(OpenAPI, {
method: "PATCH", method: "PATCH",
url: "/api/v1/users/me/password", url: "/api/v1/users/me/password",
body: requestBody, body: data.requestBody,
mediaType: "application/json", mediaType: "application/json",
errors: { errors: {
422: `Validation Error`, 422: "Validation Error",
}, },
}) })
} }
@@ -277,20 +393,21 @@ export class UsersService {
/** /**
* Register User * Register User
* Create new user without the need to be logged in. * Create new user without the need to be logged in.
* @param data The data for the request.
* @param data.requestBody
* @returns UserPublic Successful Response * @returns UserPublic Successful Response
* @throws ApiError * @throws ApiError
*/ */
public static registerUser( public static registerUser(
data: TDataRegisterUser, data: UsersRegisterUserData,
): CancelablePromise<UserPublic> { ): CancelablePromise<UsersRegisterUserResponse> {
const { requestBody } = data
return __request(OpenAPI, { return __request(OpenAPI, {
method: "POST", method: "POST",
url: "/api/v1/users/signup", url: "/api/v1/users/signup",
body: requestBody, body: data.requestBody,
mediaType: "application/json", mediaType: "application/json",
errors: { errors: {
422: `Validation Error`, 422: "Validation Error",
}, },
}) })
} }
@@ -298,21 +415,22 @@ export class UsersService {
/** /**
* Read User By Id * Read User By Id
* Get a specific user by id. * Get a specific user by id.
* @param data The data for the request.
* @param data.userId
* @returns UserPublic Successful Response * @returns UserPublic Successful Response
* @throws ApiError * @throws ApiError
*/ */
public static readUserById( public static readUserById(
data: TDataReadUserById, data: UsersReadUserByIdData,
): CancelablePromise<UserPublic> { ): CancelablePromise<UsersReadUserByIdResponse> {
const { userId } = data
return __request(OpenAPI, { return __request(OpenAPI, {
method: "GET", method: "GET",
url: "/api/v1/users/{user_id}", url: "/api/v1/users/{user_id}",
path: { path: {
user_id: userId, user_id: data.userId,
}, },
errors: { errors: {
422: `Validation Error`, 422: "Validation Error",
}, },
}) })
} }
@@ -320,23 +438,25 @@ export class UsersService {
/** /**
* Update User * Update User
* Update a user. * Update a user.
* @param data The data for the request.
* @param data.userId
* @param data.requestBody
* @returns UserPublic Successful Response * @returns UserPublic Successful Response
* @throws ApiError * @throws ApiError
*/ */
public static updateUser( public static updateUser(
data: TDataUpdateUser, data: UsersUpdateUserData,
): CancelablePromise<UserPublic> { ): CancelablePromise<UsersUpdateUserResponse> {
const { requestBody, userId } = data
return __request(OpenAPI, { return __request(OpenAPI, {
method: "PATCH", method: "PATCH",
url: "/api/v1/users/{user_id}", url: "/api/v1/users/{user_id}",
path: { path: {
user_id: userId, user_id: data.userId,
}, },
body: requestBody, body: data.requestBody,
mediaType: "application/json", mediaType: "application/json",
errors: { errors: {
422: `Validation Error`, 422: "Validation Error",
}, },
}) })
} }
@@ -344,45 +464,47 @@ export class UsersService {
/** /**
* Delete User * Delete User
* Delete a user. * Delete a user.
* @param data The data for the request.
* @param data.userId
* @returns Message Successful Response * @returns Message Successful Response
* @throws ApiError * @throws ApiError
*/ */
public static deleteUser(data: TDataDeleteUser): CancelablePromise<Message> { public static deleteUser(
const { userId } = data data: UsersDeleteUserData,
): CancelablePromise<UsersDeleteUserResponse> {
return __request(OpenAPI, { return __request(OpenAPI, {
method: "DELETE", method: "DELETE",
url: "/api/v1/users/{user_id}", url: "/api/v1/users/{user_id}",
path: { path: {
user_id: userId, user_id: data.userId,
}, },
errors: { errors: {
422: `Validation Error`, 422: "Validation Error",
}, },
}) })
} }
} }
export type TDataTestEmail = {
emailTo: string
}
export class UtilsService { export class UtilsService {
/** /**
* Test Email * Test Email
* Test emails. * Test emails.
* @param data The data for the request.
* @param data.emailTo
* @returns Message Successful Response * @returns Message Successful Response
* @throws ApiError * @throws ApiError
*/ */
public static testEmail(data: TDataTestEmail): CancelablePromise<Message> { public static testEmail(
const { emailTo } = data data: UtilsTestEmailData,
): CancelablePromise<UtilsTestEmailResponse> {
return __request(OpenAPI, { return __request(OpenAPI, {
method: "POST", method: "POST",
url: "/api/v1/utils/test-email/", url: "/api/v1/utils/test-email/",
query: { query: {
email_to: emailTo, email_to: data.emailTo,
}, },
errors: { errors: {
422: `Validation Error`, 422: "Validation Error",
}, },
}) })
} }
@@ -392,138 +514,10 @@ export class UtilsService {
* @returns boolean Successful Response * @returns boolean Successful Response
* @throws ApiError * @throws ApiError
*/ */
public static healthCheck(): CancelablePromise<boolean> { public static healthCheck(): CancelablePromise<UtilsHealthCheckResponse> {
return __request(OpenAPI, { return __request(OpenAPI, {
method: "GET", method: "GET",
url: "/api/v1/utils/health-check/", url: "/api/v1/utils/health-check/",
}) })
} }
} }
export type TDataReadItems = {
limit?: number
skip?: number
}
export type TDataCreateItem = {
requestBody: ItemCreate
}
export type TDataReadItem = {
id: string
}
export type TDataUpdateItem = {
id: string
requestBody: ItemUpdate
}
export type TDataDeleteItem = {
id: string
}
export class ItemsService {
/**
* Read Items
* Retrieve items.
* @returns ItemsPublic Successful Response
* @throws ApiError
*/
public static readItems(
data: TDataReadItems = {},
): CancelablePromise<ItemsPublic> {
const { limit = 100, skip = 0 } = data
return __request(OpenAPI, {
method: "GET",
url: "/api/v1/items/",
query: {
skip,
limit,
},
errors: {
422: `Validation Error`,
},
})
}
/**
* Create Item
* Create new item.
* @returns ItemPublic Successful Response
* @throws ApiError
*/
public static createItem(
data: TDataCreateItem,
): CancelablePromise<ItemPublic> {
const { requestBody } = data
return __request(OpenAPI, {
method: "POST",
url: "/api/v1/items/",
body: requestBody,
mediaType: "application/json",
errors: {
422: `Validation Error`,
},
})
}
/**
* Read Item
* Get item by ID.
* @returns ItemPublic Successful Response
* @throws ApiError
*/
public static readItem(data: TDataReadItem): CancelablePromise<ItemPublic> {
const { id } = data
return __request(OpenAPI, {
method: "GET",
url: "/api/v1/items/{id}",
path: {
id,
},
errors: {
422: `Validation Error`,
},
})
}
/**
* Update Item
* Update an item.
* @returns ItemPublic Successful Response
* @throws ApiError
*/
public static updateItem(
data: TDataUpdateItem,
): CancelablePromise<ItemPublic> {
const { id, requestBody } = data
return __request(OpenAPI, {
method: "PUT",
url: "/api/v1/items/{id}",
path: {
id,
},
body: requestBody,
mediaType: "application/json",
errors: {
422: `Validation Error`,
},
})
}
/**
* Delete Item
* Delete an item.
* @returns Message Successful Response
* @throws ApiError
*/
public static deleteItem(data: TDataDeleteItem): CancelablePromise<Message> {
const { id } = data
return __request(OpenAPI, {
method: "DELETE",
url: "/api/v1/items/{id}",
path: {
id,
},
errors: {
422: `Validation Error`,
},
})
}
}
+221
View File
@@ -0,0 +1,221 @@
// This file is auto-generated by @hey-api/openapi-ts
export type Body_login_login_access_token = {
grant_type?: string | null
username: string
password: string
scope?: string
client_id?: string | null
client_secret?: string | null
}
export type HTTPValidationError = {
detail?: Array<ValidationError>
}
export type ItemCreate = {
title: string
description?: string | null
}
export type ItemPublic = {
title: string
description?: string | null
id: string
owner_id: string
}
export type ItemsPublic = {
data: Array<ItemPublic>
count: number
}
export type ItemUpdate = {
title?: string | null
description?: string | null
}
export type Message = {
message: string
}
export type NewPassword = {
token: string
new_password: string
}
export type Token = {
access_token: string
token_type?: string
}
export type UpdatePassword = {
current_password: string
new_password: string
}
export type UserCreate = {
email: string
is_active?: boolean
is_superuser?: boolean
full_name?: string | null
password: string
}
export type UserPublic = {
email: string
is_active?: boolean
is_superuser?: boolean
full_name?: string | null
id: string
}
export type UserRegister = {
email: string
password: string
full_name?: string | null
}
export type UsersPublic = {
data: Array<UserPublic>
count: number
}
export type UserUpdate = {
email?: string | null
is_active?: boolean
is_superuser?: boolean
full_name?: string | null
password?: string | null
}
export type UserUpdateMe = {
full_name?: string | null
email?: string | null
}
export type ValidationError = {
loc: Array<string | number>
msg: string
type: string
}
export type ItemsReadItemsData = {
limit?: number
skip?: number
}
export type ItemsReadItemsResponse = ItemsPublic
export type ItemsCreateItemData = {
requestBody: ItemCreate
}
export type ItemsCreateItemResponse = ItemPublic
export type ItemsReadItemData = {
id: string
}
export type ItemsReadItemResponse = ItemPublic
export type ItemsUpdateItemData = {
id: string
requestBody: ItemUpdate
}
export type ItemsUpdateItemResponse = ItemPublic
export type ItemsDeleteItemData = {
id: string
}
export type ItemsDeleteItemResponse = Message
export type LoginLoginAccessTokenData = {
formData: Body_login_login_access_token
}
export type LoginLoginAccessTokenResponse = Token
export type LoginTestTokenResponse = UserPublic
export type LoginRecoverPasswordData = {
email: string
}
export type LoginRecoverPasswordResponse = Message
export type LoginResetPasswordData = {
requestBody: NewPassword
}
export type LoginResetPasswordResponse = Message
export type LoginRecoverPasswordHtmlContentData = {
email: string
}
export type LoginRecoverPasswordHtmlContentResponse = string
export type UsersReadUsersData = {
limit?: number
skip?: number
}
export type UsersReadUsersResponse = UsersPublic
export type UsersCreateUserData = {
requestBody: UserCreate
}
export type UsersCreateUserResponse = UserPublic
export type UsersReadUserMeResponse = UserPublic
export type UsersDeleteUserMeResponse = Message
export type UsersUpdateUserMeData = {
requestBody: UserUpdateMe
}
export type UsersUpdateUserMeResponse = UserPublic
export type UsersUpdatePasswordMeData = {
requestBody: UpdatePassword
}
export type UsersUpdatePasswordMeResponse = Message
export type UsersRegisterUserData = {
requestBody: UserRegister
}
export type UsersRegisterUserResponse = UserPublic
export type UsersReadUserByIdData = {
userId: string
}
export type UsersReadUserByIdResponse = UserPublic
export type UsersUpdateUserData = {
requestBody: UserUpdate
userId: string
}
export type UsersUpdateUserResponse = UserPublic
export type UsersDeleteUserData = {
userId: string
}
export type UsersDeleteUserResponse = Message
export type UtilsTestEmailData = {
emailTo: string
}
export type UtilsTestEmailResponse = Message
export type UtilsHealthCheckResponse = boolean
+132 -84
View File
@@ -1,45 +1,48 @@
import { useMutation, useQueryClient } from "@tanstack/react-query"
import { Controller, type SubmitHandler, useForm } from "react-hook-form"
import { type UserCreate, UsersService } from "@/client"
import type { ApiError } from "@/client/core/ApiError"
import useCustomToast from "@/hooks/useCustomToast"
import { emailPattern, handleError } from "@/utils"
import { import {
Button, Button,
Checkbox, DialogActionTrigger,
DialogTitle,
Flex, Flex,
FormControl,
FormErrorMessage,
FormLabel,
Input, Input,
Modal, Text,
ModalBody, VStack,
ModalCloseButton,
ModalContent,
ModalFooter,
ModalHeader,
ModalOverlay,
} from "@chakra-ui/react" } from "@chakra-ui/react"
import { useMutation, useQueryClient } from "@tanstack/react-query" import { useState } from "react"
import { type SubmitHandler, useForm } from "react-hook-form" import { FaPlus } from "react-icons/fa"
import { Checkbox } from "../ui/checkbox"
import { type UserCreate, UsersService } from "../../client" import {
import type { ApiError } from "../../client/core/ApiError" DialogBody,
import useCustomToast from "../../hooks/useCustomToast" DialogCloseTrigger,
import { emailPattern, handleError } from "../../utils" DialogContent,
DialogFooter,
interface AddUserProps { DialogHeader,
isOpen: boolean DialogRoot,
onClose: () => void DialogTrigger,
} } from "../ui/dialog"
import { Field } from "../ui/field"
interface UserCreateForm extends UserCreate { interface UserCreateForm extends UserCreate {
confirm_password: string confirm_password: string
} }
const AddUser = ({ isOpen, onClose }: AddUserProps) => { const AddUser = () => {
const [isOpen, setIsOpen] = useState(false)
const queryClient = useQueryClient() const queryClient = useQueryClient()
const showToast = useCustomToast() const { showSuccessToast } = useCustomToast()
const { const {
control,
register, register,
handleSubmit, handleSubmit,
reset, reset,
getValues, getValues,
formState: { errors, isSubmitting }, formState: { errors, isValid, isSubmitting },
} = useForm<UserCreateForm>({ } = useForm<UserCreateForm>({
mode: "onBlur", mode: "onBlur",
criteriaMode: "all", criteriaMode: "all",
@@ -57,12 +60,12 @@ const AddUser = ({ isOpen, onClose }: AddUserProps) => {
mutationFn: (data: UserCreate) => mutationFn: (data: UserCreate) =>
UsersService.createUser({ requestBody: data }), UsersService.createUser({ requestBody: data }),
onSuccess: () => { onSuccess: () => {
showToast("Success!", "User created successfully.", "success") showSuccessToast("User created successfully.")
reset() reset()
onClose() setIsOpen(false)
}, },
onError: (err: ApiError) => { onError: (err: ApiError) => {
handleError(err, showToast) handleError(err)
}, },
onSettled: () => { onSettled: () => {
queryClient.invalidateQueries({ queryKey: ["users"] }) queryClient.invalidateQueries({ queryKey: ["users"] })
@@ -74,20 +77,34 @@ const AddUser = ({ isOpen, onClose }: AddUserProps) => {
} }
return ( return (
<> <DialogRoot
<Modal size={{ base: "xs", md: "md" }}
isOpen={isOpen} placement="center"
onClose={onClose} open={isOpen}
size={{ base: "sm", md: "md" }} onOpenChange={({ open }) => setIsOpen(open)}
isCentered >
<DialogTrigger asChild>
<Button value="add-user" my={4}>
<FaPlus fontSize="16px" />
Add User
</Button>
</DialogTrigger>
<DialogContent>
<form onSubmit={handleSubmit(onSubmit)}>
<DialogHeader>
<DialogTitle>Add User</DialogTitle>
</DialogHeader>
<DialogBody>
<Text mb={4}>
Fill in the form below to add a new user to the system.
</Text>
<VStack gap={4}>
<Field
required
invalid={!!errors.email}
errorText={errors.email?.message}
label="Email"
> >
<ModalOverlay />
<ModalContent as="form" onSubmit={handleSubmit(onSubmit)}>
<ModalHeader>Add User</ModalHeader>
<ModalCloseButton />
<ModalBody pb={6}>
<FormControl isRequired isInvalid={!!errors.email}>
<FormLabel htmlFor="email">Email</FormLabel>
<Input <Input
id="email" id="email"
{...register("email", { {...register("email", {
@@ -97,24 +114,27 @@ const AddUser = ({ isOpen, onClose }: AddUserProps) => {
placeholder="Email" placeholder="Email"
type="email" type="email"
/> />
{errors.email && ( </Field>
<FormErrorMessage>{errors.email.message}</FormErrorMessage>
)} <Field
</FormControl> invalid={!!errors.full_name}
<FormControl mt={4} isInvalid={!!errors.full_name}> errorText={errors.full_name?.message}
<FormLabel htmlFor="name">Full name</FormLabel> label="Full Name"
>
<Input <Input
id="name" id="name"
{...register("full_name")} {...register("full_name")}
placeholder="Full name" placeholder="Full name"
type="text" type="text"
/> />
{errors.full_name && ( </Field>
<FormErrorMessage>{errors.full_name.message}</FormErrorMessage>
)} <Field
</FormControl> required
<FormControl mt={4} isRequired isInvalid={!!errors.password}> invalid={!!errors.password}
<FormLabel htmlFor="password">Set Password</FormLabel> errorText={errors.password?.message}
label="Set Password"
>
<Input <Input
id="password" id="password"
{...register("password", { {...register("password", {
@@ -127,16 +147,14 @@ const AddUser = ({ isOpen, onClose }: AddUserProps) => {
placeholder="Password" placeholder="Password"
type="password" type="password"
/> />
{errors.password && ( </Field>
<FormErrorMessage>{errors.password.message}</FormErrorMessage>
)} <Field
</FormControl> required
<FormControl invalid={!!errors.confirm_password}
mt={4} errorText={errors.confirm_password?.message}
isRequired label="Confirm Password"
isInvalid={!!errors.confirm_password}
> >
<FormLabel htmlFor="confirm_password">Confirm Password</FormLabel>
<Input <Input
id="confirm_password" id="confirm_password"
{...register("confirm_password", { {...register("confirm_password", {
@@ -148,34 +166,64 @@ const AddUser = ({ isOpen, onClose }: AddUserProps) => {
placeholder="Password" placeholder="Password"
type="password" type="password"
/> />
{errors.confirm_password && ( </Field>
<FormErrorMessage> </VStack>
{errors.confirm_password.message}
</FormErrorMessage> <Flex mt={4} direction="column" gap={4}>
)} <Controller
</FormControl> control={control}
<Flex mt={4}> name="is_superuser"
<FormControl> render={({ field }) => (
<Checkbox {...register("is_superuser")} colorScheme="teal"> <Field disabled={field.disabled} colorPalette="teal">
<Checkbox
checked={field.value}
onCheckedChange={({ checked }) => field.onChange(checked)}
>
Is superuser? Is superuser?
</Checkbox> </Checkbox>
</FormControl> </Field>
<FormControl> )}
<Checkbox {...register("is_active")} colorScheme="teal"> />
<Controller
control={control}
name="is_active"
render={({ field }) => (
<Field disabled={field.disabled} colorPalette="teal">
<Checkbox
checked={field.value}
onCheckedChange={({ checked }) => field.onChange(checked)}
>
Is active? Is active?
</Checkbox> </Checkbox>
</FormControl> </Field>
)}
/>
</Flex> </Flex>
</ModalBody> </DialogBody>
<ModalFooter gap={3}>
<Button variant="primary" type="submit" isLoading={isSubmitting}> <DialogFooter gap={2}>
<DialogActionTrigger asChild>
<Button
variant="subtle"
colorPalette="gray"
disabled={isSubmitting}
>
Cancel
</Button>
</DialogActionTrigger>
<Button
variant="solid"
type="submit"
disabled={!isValid}
loading={isSubmitting}
>
Save Save
</Button> </Button>
<Button onClick={onClose}>Cancel</Button> </DialogFooter>
</ModalFooter> </form>
</ModalContent> <DialogCloseTrigger />
</Modal> </DialogContent>
</> </DialogRoot>
) )
} }
@@ -0,0 +1,104 @@
import { Button, DialogTitle, Text } from "@chakra-ui/react"
import { useMutation, useQueryClient } from "@tanstack/react-query"
import { useState } from "react"
import { useForm } from "react-hook-form"
import { FiTrash2 } from "react-icons/fi"
import { UsersService } from "@/client"
import {
DialogActionTrigger,
DialogBody,
DialogCloseTrigger,
DialogContent,
DialogFooter,
DialogHeader,
DialogRoot,
DialogTrigger,
} from "@/components/ui/dialog"
import useCustomToast from "@/hooks/useCustomToast"
const DeleteUser = ({ id }: { id: string }) => {
const [isOpen, setIsOpen] = useState(false)
const queryClient = useQueryClient()
const { showSuccessToast, showErrorToast } = useCustomToast()
const {
handleSubmit,
formState: { isSubmitting },
} = useForm()
const deleteUser = async (id: string) => {
await UsersService.deleteUser({ userId: id })
}
const mutation = useMutation({
mutationFn: deleteUser,
onSuccess: () => {
showSuccessToast("The user was deleted successfully")
setIsOpen(false)
},
onError: () => {
showErrorToast("An error occurred while deleting the user")
},
onSettled: () => {
queryClient.invalidateQueries()
},
})
const onSubmit = async () => {
mutation.mutate(id)
}
return (
<DialogRoot
size={{ base: "xs", md: "md" }}
placement="center"
role="alertdialog"
open={isOpen}
onOpenChange={({ open }) => setIsOpen(open)}
>
<DialogTrigger asChild>
<Button variant="ghost" size="sm" colorPalette="red">
<FiTrash2 fontSize="16px" />
Delete User
</Button>
</DialogTrigger>
<DialogContent>
<form onSubmit={handleSubmit(onSubmit)}>
<DialogHeader>
<DialogTitle>Delete User</DialogTitle>
</DialogHeader>
<DialogBody>
<Text mb={4}>
All items associated with this user will also be{" "}
<strong>permanently deleted.</strong> Are you sure? You will not
be able to undo this action.
</Text>
</DialogBody>
<DialogFooter gap={2}>
<DialogActionTrigger asChild>
<Button
variant="subtle"
colorPalette="gray"
disabled={isSubmitting}
>
Cancel
</Button>
</DialogActionTrigger>
<Button
variant="solid"
colorPalette="red"
type="submit"
loading={isSubmitting}
>
Delete
</Button>
</DialogFooter>
<DialogCloseTrigger />
</form>
</DialogContent>
</DialogRoot>
)
}
export default DeleteUser
+134 -92
View File
@@ -1,51 +1,53 @@
import {
Button,
Checkbox,
Flex,
FormControl,
FormErrorMessage,
FormLabel,
Input,
Modal,
ModalBody,
ModalCloseButton,
ModalContent,
ModalFooter,
ModalHeader,
ModalOverlay,
} from "@chakra-ui/react"
import { useMutation, useQueryClient } from "@tanstack/react-query" import { useMutation, useQueryClient } from "@tanstack/react-query"
import { type SubmitHandler, useForm } from "react-hook-form" import { Controller, type SubmitHandler, useForm } from "react-hook-form"
import { import {
type ApiError, Button,
type UserPublic, DialogActionTrigger,
type UserUpdate, DialogRoot,
UsersService, DialogTrigger,
} from "../../client" Flex,
import useCustomToast from "../../hooks/useCustomToast" Input,
import { emailPattern, handleError } from "../../utils" Text,
VStack,
} from "@chakra-ui/react"
import { useState } from "react"
import { FaExchangeAlt } from "react-icons/fa"
import { type UserPublic, type UserUpdate, UsersService } from "@/client"
import type { ApiError } from "@/client/core/ApiError"
import useCustomToast from "@/hooks/useCustomToast"
import { emailPattern, handleError } from "@/utils"
import { Checkbox } from "../ui/checkbox"
import {
DialogBody,
DialogCloseTrigger,
DialogContent,
DialogFooter,
DialogHeader,
DialogTitle,
} from "../ui/dialog"
import { Field } from "../ui/field"
interface EditUserProps { interface EditUserProps {
user: UserPublic user: UserPublic
isOpen: boolean
onClose: () => void
} }
interface UserUpdateForm extends UserUpdate { interface UserUpdateForm extends UserUpdate {
confirm_password: string confirm_password?: string
} }
const EditUser = ({ user, isOpen, onClose }: EditUserProps) => { const EditUser = ({ user }: EditUserProps) => {
const [isOpen, setIsOpen] = useState(false)
const queryClient = useQueryClient() const queryClient = useQueryClient()
const showToast = useCustomToast() const { showSuccessToast } = useCustomToast()
const { const {
control,
register, register,
handleSubmit, handleSubmit,
reset, reset,
getValues, getValues,
formState: { errors, isSubmitting, isDirty }, formState: { errors, isSubmitting },
} = useForm<UserUpdateForm>({ } = useForm<UserUpdateForm>({
mode: "onBlur", mode: "onBlur",
criteriaMode: "all", criteriaMode: "all",
@@ -56,11 +58,12 @@ const EditUser = ({ user, isOpen, onClose }: EditUserProps) => {
mutationFn: (data: UserUpdateForm) => mutationFn: (data: UserUpdateForm) =>
UsersService.updateUser({ userId: user.id, requestBody: data }), UsersService.updateUser({ userId: user.id, requestBody: data }),
onSuccess: () => { onSuccess: () => {
showToast("Success!", "User updated successfully.", "success") showSuccessToast("User updated successfully.")
onClose() reset()
setIsOpen(false)
}, },
onError: (err: ApiError) => { onError: (err: ApiError) => {
handleError(err, showToast) handleError(err)
}, },
onSettled: () => { onSettled: () => {
queryClient.invalidateQueries({ queryKey: ["users"] }) queryClient.invalidateQueries({ queryKey: ["users"] })
@@ -74,26 +77,33 @@ const EditUser = ({ user, isOpen, onClose }: EditUserProps) => {
mutation.mutate(data) mutation.mutate(data)
} }
const onCancel = () => {
reset()
onClose()
}
return ( return (
<> <DialogRoot
<Modal size={{ base: "xs", md: "md" }}
isOpen={isOpen} placement="center"
onClose={onClose} open={isOpen}
size={{ base: "sm", md: "md" }} onOpenChange={({ open }) => setIsOpen(open)}
isCentered >
<DialogTrigger asChild>
<Button variant="ghost" size="sm">
<FaExchangeAlt fontSize="16px" />
Edit User
</Button>
</DialogTrigger>
<DialogContent>
<form onSubmit={handleSubmit(onSubmit)}>
<DialogHeader>
<DialogTitle>Edit User</DialogTitle>
</DialogHeader>
<DialogBody>
<Text mb={4}>Update the user details below.</Text>
<VStack gap={4}>
<Field
required
invalid={!!errors.email}
errorText={errors.email?.message}
label="Email"
> >
<ModalOverlay />
<ModalContent as="form" onSubmit={handleSubmit(onSubmit)}>
<ModalHeader>Edit User</ModalHeader>
<ModalCloseButton />
<ModalBody pb={6}>
<FormControl isInvalid={!!errors.email}>
<FormLabel htmlFor="email">Email</FormLabel>
<Input <Input
id="email" id="email"
{...register("email", { {...register("email", {
@@ -103,16 +113,27 @@ const EditUser = ({ user, isOpen, onClose }: EditUserProps) => {
placeholder="Email" placeholder="Email"
type="email" type="email"
/> />
{errors.email && ( </Field>
<FormErrorMessage>{errors.email.message}</FormErrorMessage>
)} <Field
</FormControl> invalid={!!errors.full_name}
<FormControl mt={4}> errorText={errors.full_name?.message}
<FormLabel htmlFor="name">Full name</FormLabel> label="Full Name"
<Input id="name" {...register("full_name")} type="text" /> >
</FormControl> <Input
<FormControl mt={4} isInvalid={!!errors.password}> id="name"
<FormLabel htmlFor="password">Set Password</FormLabel> {...register("full_name")}
placeholder="Full name"
type="text"
/>
</Field>
<Field
required
invalid={!!errors.password}
errorText={errors.password?.message}
label="Set Password"
>
<Input <Input
id="password" id="password"
{...register("password", { {...register("password", {
@@ -124,12 +145,14 @@ const EditUser = ({ user, isOpen, onClose }: EditUserProps) => {
placeholder="Password" placeholder="Password"
type="password" type="password"
/> />
{errors.password && ( </Field>
<FormErrorMessage>{errors.password.message}</FormErrorMessage>
)} <Field
</FormControl> required
<FormControl mt={4} isInvalid={!!errors.confirm_password}> invalid={!!errors.confirm_password}
<FormLabel htmlFor="confirm_password">Confirm Password</FormLabel> errorText={errors.confirm_password?.message}
label="Confirm Password"
>
<Input <Input
id="confirm_password" id="confirm_password"
{...register("confirm_password", { {...register("confirm_password", {
@@ -140,40 +163,59 @@ const EditUser = ({ user, isOpen, onClose }: EditUserProps) => {
placeholder="Password" placeholder="Password"
type="password" type="password"
/> />
{errors.confirm_password && ( </Field>
<FormErrorMessage> </VStack>
{errors.confirm_password.message}
</FormErrorMessage> <Flex mt={4} direction="column" gap={4}>
)} <Controller
</FormControl> control={control}
<Flex> name="is_superuser"
<FormControl mt={4}> render={({ field }) => (
<Checkbox {...register("is_superuser")} colorScheme="teal"> <Field disabled={field.disabled} colorPalette="teal">
<Checkbox
checked={field.value}
onCheckedChange={({ checked }) => field.onChange(checked)}
>
Is superuser? Is superuser?
</Checkbox> </Checkbox>
</FormControl> </Field>
<FormControl mt={4}> )}
<Checkbox {...register("is_active")} colorScheme="teal"> />
<Controller
control={control}
name="is_active"
render={({ field }) => (
<Field disabled={field.disabled} colorPalette="teal">
<Checkbox
checked={field.value}
onCheckedChange={({ checked }) => field.onChange(checked)}
>
Is active? Is active?
</Checkbox> </Checkbox>
</FormControl> </Field>
)}
/>
</Flex> </Flex>
</ModalBody> </DialogBody>
<ModalFooter gap={3}> <DialogFooter gap={2}>
<DialogActionTrigger asChild>
<Button <Button
variant="primary" variant="subtle"
type="submit" colorPalette="gray"
isLoading={isSubmitting} disabled={isSubmitting}
isDisabled={!isDirty}
> >
Cancel
</Button>
</DialogActionTrigger>
<Button variant="solid" type="submit" loading={isSubmitting}>
Save Save
</Button> </Button>
<Button onClick={onCancel}>Cancel</Button> </DialogFooter>
</ModalFooter> <DialogCloseTrigger />
</ModalContent> </form>
</Modal> </DialogContent>
</> </DialogRoot>
) )
} }
@@ -1,75 +0,0 @@
import {
Button,
Menu,
MenuButton,
MenuItem,
MenuList,
useDisclosure,
} from "@chakra-ui/react"
import { BsThreeDotsVertical } from "react-icons/bs"
import { FiEdit, FiTrash } from "react-icons/fi"
import type { ItemPublic, UserPublic } from "../../client"
import EditUser from "../Admin/EditUser"
import EditItem from "../Items/EditItem"
import Delete from "./DeleteAlert"
interface ActionsMenuProps {
type: string
value: ItemPublic | UserPublic
disabled?: boolean
}
const ActionsMenu = ({ type, value, disabled }: ActionsMenuProps) => {
const editUserModal = useDisclosure()
const deleteModal = useDisclosure()
return (
<>
<Menu>
<MenuButton
isDisabled={disabled}
as={Button}
rightIcon={<BsThreeDotsVertical />}
variant="unstyled"
/>
<MenuList>
<MenuItem
onClick={editUserModal.onOpen}
icon={<FiEdit fontSize="16px" />}
>
Edit {type}
</MenuItem>
<MenuItem
onClick={deleteModal.onOpen}
icon={<FiTrash fontSize="16px" />}
color="ui.danger"
>
Delete {type}
</MenuItem>
</MenuList>
{type === "User" ? (
<EditUser
user={value as UserPublic}
isOpen={editUserModal.isOpen}
onClose={editUserModal.onClose}
/>
) : (
<EditItem
item={value as ItemPublic}
isOpen={editUserModal.isOpen}
onClose={editUserModal.onClose}
/>
)}
<Delete
type={type}
id={value.id}
isOpen={deleteModal.isOpen}
onClose={deleteModal.onClose}
/>
</Menu>
</>
)
}
export default ActionsMenu
@@ -1,113 +0,0 @@
import {
AlertDialog,
AlertDialogBody,
AlertDialogContent,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogOverlay,
Button,
} from "@chakra-ui/react"
import { useMutation, useQueryClient } from "@tanstack/react-query"
import React from "react"
import { useForm } from "react-hook-form"
import { ItemsService, UsersService } from "../../client"
import useCustomToast from "../../hooks/useCustomToast"
interface DeleteProps {
type: string
id: string
isOpen: boolean
onClose: () => void
}
const Delete = ({ type, id, isOpen, onClose }: DeleteProps) => {
const queryClient = useQueryClient()
const showToast = useCustomToast()
const cancelRef = React.useRef<HTMLButtonElement | null>(null)
const {
handleSubmit,
formState: { isSubmitting },
} = useForm()
const deleteEntity = async (id: string) => {
if (type === "Item") {
await ItemsService.deleteItem({ id: id })
} else if (type === "User") {
await UsersService.deleteUser({ userId: id })
} else {
throw new Error(`Unexpected type: ${type}`)
}
}
const mutation = useMutation({
mutationFn: deleteEntity,
onSuccess: () => {
showToast(
"Success",
`The ${type.toLowerCase()} was deleted successfully.`,
"success",
)
onClose()
},
onError: () => {
showToast(
"An error occurred.",
`An error occurred while deleting the ${type.toLowerCase()}.`,
"error",
)
},
onSettled: () => {
queryClient.invalidateQueries({
queryKey: [type === "Item" ? "items" : "users"],
})
},
})
const onSubmit = async () => {
mutation.mutate(id)
}
return (
<>
<AlertDialog
isOpen={isOpen}
onClose={onClose}
leastDestructiveRef={cancelRef}
size={{ base: "sm", md: "md" }}
isCentered
>
<AlertDialogOverlay>
<AlertDialogContent as="form" onSubmit={handleSubmit(onSubmit)}>
<AlertDialogHeader>Delete {type}</AlertDialogHeader>
<AlertDialogBody>
{type === "User" && (
<span>
All items associated with this user will also be{" "}
<strong>permantly deleted. </strong>
</span>
)}
Are you sure? You will not be able to undo this action.
</AlertDialogBody>
<AlertDialogFooter gap={3}>
<Button variant="danger" type="submit" isLoading={isSubmitting}>
Delete
</Button>
<Button
ref={cancelRef}
onClick={onClose}
isDisabled={isSubmitting}
>
Cancel
</Button>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialogOverlay>
</AlertDialog>
</>
)
}
export default Delete
@@ -0,0 +1,27 @@
import { IconButton } from "@chakra-ui/react"
import { BsThreeDotsVertical } from "react-icons/bs"
import { MenuContent, MenuRoot, MenuTrigger } from "../ui/menu"
import type { ItemPublic } from "@/client"
import DeleteItem from "../Items/DeleteItem"
import EditItem from "../Items/EditItem"
interface ItemActionsMenuProps {
item: ItemPublic
}
export const ItemActionsMenu = ({ item }: ItemActionsMenuProps) => {
return (
<MenuRoot>
<MenuTrigger asChild>
<IconButton variant="ghost" color="inherit">
<BsThreeDotsVertical />
</IconButton>
</MenuTrigger>
<MenuContent>
<EditItem item={item} />
<DeleteItem id={item.id} />
</MenuContent>
</MenuRoot>
)
}
+22 -29
View File
@@ -1,38 +1,31 @@
import type { ComponentType, ElementType } from "react" import { Flex, Image, useBreakpointValue } from "@chakra-ui/react"
import { Link } from "@tanstack/react-router"
import { Button, Flex, Icon, useDisclosure } from "@chakra-ui/react" import Logo from "/assets/images/fastapi-logo.svg"
import { FaPlus } from "react-icons/fa" import UserMenu from "./UserMenu"
interface NavbarProps { function Navbar() {
type: string const display = useBreakpointValue({ base: "none", md: "flex" })
addModalAs: ComponentType | ElementType
}
const Navbar = ({ type, addModalAs }: NavbarProps) => {
const addModal = useDisclosure()
const AddModal = addModalAs
return ( return (
<> <Flex
<Flex py={8} gap={4}> display={display}
{/* TODO: Complete search functionality */} justify="space-between"
{/* <InputGroup w={{ base: '100%', md: 'auto' }}> position="sticky"
<InputLeftElement pointerEvents='none'> color="white"
<Icon as={FaSearch} color='ui.dim' /> align="center"
</InputLeftElement> bg="bg.muted"
<Input type='text' placeholder='Search' fontSize={{ base: 'sm', md: 'inherit' }} borderRadius='8px' /> w="100%"
</InputGroup> */} top={0}
<Button p={4}
variant="primary"
gap={1}
fontSize={{ base: "sm", md: "inherit" }}
onClick={addModal.onOpen}
> >
<Icon as={FaPlus} /> Add {type} <Link to="/">
</Button> <Image src={Logo} alt="Logo" w="180px" maxW="2xs" px={2} />
<AddModal isOpen={addModal.isOpen} onClose={addModal.onClose} /> </Link>
<Flex gap={2} alignItems="center">
<UserMenu />
</Flex>
</Flex> </Flex>
</>
) )
} }
+37 -21
View File
@@ -1,39 +1,55 @@
import { Button, Container, Text } from "@chakra-ui/react" import { Button, Center, Flex, Text } from "@chakra-ui/react"
import { Link } from "@tanstack/react-router" import { Link } from "@tanstack/react-router"
const NotFound = () => { const NotFound = () => {
return ( return (
<> <>
<Container <Flex
h="100vh" height="100vh"
alignItems="stretch" align="center"
justifyContent="center" justify="center"
textAlign="center" flexDir="column"
maxW="sm" data-testid="not-found"
centerContent p={4}
> >
<Flex alignItems="center" zIndex={1}>
<Flex flexDir="column" ml={4} align="center" justify="center" p={4}>
<Text <Text
fontSize="8xl" fontSize={{ base: "6xl", md: "8xl" }}
color="ui.main"
fontWeight="bold" fontWeight="bold"
lineHeight="1" lineHeight="1"
mb={4} mb={4}
> >
404 404
</Text> </Text>
<Text fontSize="md">Oops!</Text> <Text fontSize="2xl" fontWeight="bold" mb={2}>
<Text fontSize="md">Page not found.</Text> Oops!
<Button </Text>
as={Link} </Flex>
to="/" </Flex>
color="ui.main"
borderColor="ui.main" <Text
variant="outline" fontSize="lg"
mt={4} color="gray.600"
mb={4}
textAlign="center"
zIndex={1}
> >
Go back The page you are looking for was not found.
</Text>
<Center zIndex={1}>
<Link to="/">
<Button
variant="solid"
colorScheme="teal"
mt={4}
alignSelf="center"
>
Go Back
</Button> </Button>
</Container> </Link>
</Center>
</Flex>
</> </>
) )
} }
+47 -64
View File
@@ -1,33 +1,26 @@
import { import { Box, Flex, IconButton, Text } from "@chakra-ui/react"
Box,
Drawer,
DrawerBody,
DrawerCloseButton,
DrawerContent,
DrawerOverlay,
Flex,
IconButton,
Image,
Text,
useColorModeValue,
useDisclosure,
} from "@chakra-ui/react"
import { useQueryClient } from "@tanstack/react-query" import { useQueryClient } from "@tanstack/react-query"
import { FiLogOut, FiMenu } from "react-icons/fi" import { useState } from "react"
import { FaBars } from "react-icons/fa"
import { FiLogOut } from "react-icons/fi"
import Logo from "/assets/images/fastapi-logo.svg" import type { UserPublic } from "@/client"
import type { UserPublic } from "../../client" import useAuth from "@/hooks/useAuth"
import useAuth from "../../hooks/useAuth" import {
DrawerBackdrop,
DrawerBody,
DrawerCloseTrigger,
DrawerContent,
DrawerRoot,
DrawerTrigger,
} from "../ui/drawer"
import SidebarItems from "./SidebarItems" import SidebarItems from "./SidebarItems"
const Sidebar = () => { const Sidebar = () => {
const queryClient = useQueryClient() const queryClient = useQueryClient()
const bgColor = useColorModeValue("ui.light", "ui.dark")
const textColor = useColorModeValue("ui.dark", "ui.light")
const secBgColor = useColorModeValue("ui.secondary", "ui.darkSlate")
const currentUser = queryClient.getQueryData<UserPublic>(["currentUser"]) const currentUser = queryClient.getQueryData<UserPublic>(["currentUser"])
const { isOpen, onOpen, onClose } = useDisclosure()
const { logout } = useAuth() const { logout } = useAuth()
const [open, setOpen] = useState(false)
const handleLogout = async () => { const handleLogout = async () => {
logout() logout()
@@ -36,78 +29,68 @@ const Sidebar = () => {
return ( return (
<> <>
{/* Mobile */} {/* Mobile */}
<DrawerRoot
placement="start"
open={open}
onOpenChange={(e) => setOpen(e.open)}
>
<DrawerBackdrop />
<DrawerTrigger asChild>
<IconButton <IconButton
onClick={onOpen} variant="ghost"
color="inherit"
display={{ base: "flex", md: "none" }} display={{ base: "flex", md: "none" }}
aria-label="Open Menu" aria-label="Open Menu"
position="absolute" position="absolute"
fontSize="20px" zIndex="100"
m={4} m={4}
icon={<FiMenu />} >
/> <FaBars />
<Drawer isOpen={isOpen} placement="left" onClose={onClose}> </IconButton>
<DrawerOverlay /> </DrawerTrigger>
<DrawerContent maxW="250px"> <DrawerContent maxW="280px">
<DrawerCloseButton /> <DrawerCloseTrigger />
<DrawerBody py={8}> <DrawerBody>
<Flex flexDir="column" justify="space-between"> <Flex flexDir="column" justify="space-between">
<Box> <Box>
<Image src={Logo} alt="logo" p={6} /> <SidebarItems />
<SidebarItems onClose={onClose} />
<Flex <Flex
as="button" as="button"
onClick={handleLogout} onClick={handleLogout}
p={2}
color="ui.danger"
fontWeight="bold"
alignItems="center" alignItems="center"
gap={4}
px={4}
py={2}
> >
<FiLogOut /> <FiLogOut />
<Text ml={2}>Log out</Text> <Text>Log Out</Text>
</Flex> </Flex>
</Box> </Box>
{currentUser?.email && ( {currentUser?.email && (
<Text color={textColor} noOfLines={2} fontSize="sm" p={2}> <Text fontSize="sm" p={2}>
Logged in as: {currentUser.email} Logged in as: {currentUser.email}
</Text> </Text>
)} )}
</Flex> </Flex>
</DrawerBody> </DrawerBody>
<DrawerCloseTrigger />
</DrawerContent> </DrawerContent>
</Drawer> </DrawerRoot>
{/* Desktop */} {/* Desktop */}
<Box <Box
bg={bgColor}
p={3}
h="100vh"
position="sticky"
top="0"
display={{ base: "none", md: "flex" }} display={{ base: "none", md: "flex" }}
> position="sticky"
<Flex bg="bg.subtle"
flexDir="column" top={0}
justify="space-between" minW="280px"
bg={secBgColor} h="100vh"
p={4} p={4}
borderRadius={12}
> >
<Box> <Box w="100%">
<Image src={Logo} alt="Logo" w="180px" maxW="2xs" p={6} />
<SidebarItems /> <SidebarItems />
</Box> </Box>
{currentUser?.email && (
<Text
color={textColor}
noOfLines={2}
fontSize="sm"
p={2}
maxW="180px"
>
Logged in as: {currentUser.email}
</Text>
)}
</Flex>
</Box> </Box>
</> </>
) )
+23 -18
View File
@@ -1,9 +1,10 @@
import { Box, Flex, Icon, Text, useColorModeValue } from "@chakra-ui/react" import { Box, Flex, Icon, Text } from "@chakra-ui/react"
import { useQueryClient } from "@tanstack/react-query" import { useQueryClient } from "@tanstack/react-query"
import { Link } from "@tanstack/react-router" import { Link as RouterLink } from "@tanstack/react-router"
import { FiBriefcase, FiHome, FiSettings, FiUsers } from "react-icons/fi" import { FiBriefcase, FiHome, FiSettings, FiUsers } from "react-icons/fi"
import type { IconType } from "react-icons/lib"
import type { UserPublic } from "../../client" import type { UserPublic } from "@/client"
const items = [ const items = [
{ icon: FiHome, title: "Dashboard", path: "/" }, { icon: FiHome, title: "Dashboard", path: "/" },
@@ -15,39 +16,43 @@ interface SidebarItemsProps {
onClose?: () => void onClose?: () => void
} }
interface Item {
icon: IconType
title: string
path: string
}
const SidebarItems = ({ onClose }: SidebarItemsProps) => { const SidebarItems = ({ onClose }: SidebarItemsProps) => {
const queryClient = useQueryClient() const queryClient = useQueryClient()
const textColor = useColorModeValue("ui.main", "ui.light")
const bgActive = useColorModeValue("#E2E8F0", "#4A5568")
const currentUser = queryClient.getQueryData<UserPublic>(["currentUser"]) const currentUser = queryClient.getQueryData<UserPublic>(["currentUser"])
const finalItems = currentUser?.is_superuser const finalItems: Item[] = currentUser?.is_superuser
? [...items, { icon: FiUsers, title: "Admin", path: "/admin" }] ? [...items, { icon: FiUsers, title: "Admin", path: "/admin" }]
: items : items
const listItems = finalItems.map(({ icon, title, path }) => ( const listItems = finalItems.map(({ icon, title, path }) => (
<RouterLink key={title} to={path} onClick={onClose}>
<Flex <Flex
as={Link} gap={4}
to={path} px={4}
w="100%" py={2}
p={2} _hover={{
key={title} background: "gray.subtle",
activeProps={{
style: {
background: bgActive,
borderRadius: "12px",
},
}} }}
color={textColor} alignItems="center"
onClick={onClose} fontSize="sm"
> >
<Icon as={icon} alignSelf="center" /> <Icon as={icon} alignSelf="center" />
<Text ml={2}>{title}</Text> <Text ml={2}>{title}</Text>
</Flex> </Flex>
</RouterLink>
)) ))
return ( return (
<> <>
<Text fontSize="xs" px={4} py={2} fontWeight="bold">
Menu
</Text>
<Box>{listItems}</Box> <Box>{listItems}</Box>
</> </>
) )
@@ -0,0 +1,28 @@
import { IconButton } from "@chakra-ui/react"
import { BsThreeDotsVertical } from "react-icons/bs"
import { MenuContent, MenuRoot, MenuTrigger } from "../ui/menu"
import type { UserPublic } from "@/client"
import DeleteUser from "../Admin/DeleteUser"
import EditUser from "../Admin/EditUser"
interface UserActionsMenuProps {
user: UserPublic
disabled?: boolean
}
export const UserActionsMenu = ({ user, disabled }: UserActionsMenuProps) => {
return (
<MenuRoot>
<MenuTrigger asChild>
<IconButton variant="ghost" color="inherit" disabled={disabled}>
<BsThreeDotsVertical />
</IconButton>
</MenuTrigger>
<MenuContent>
<EditUser user={user} />
<DeleteUser id={user.id} />
</MenuContent>
</MenuRoot>
)
}
+42 -37
View File
@@ -1,19 +1,13 @@
import { import { Box, Button, Flex, Text } from "@chakra-ui/react"
Box,
IconButton,
Menu,
MenuButton,
MenuItem,
MenuList,
} from "@chakra-ui/react"
import { Link } from "@tanstack/react-router" import { Link } from "@tanstack/react-router"
import { FaUserAstronaut } from "react-icons/fa" import { FaUserAstronaut } from "react-icons/fa"
import { FiLogOut, FiUser } from "react-icons/fi" import { FiLogOut, FiUser } from "react-icons/fi"
import useAuth from "../../hooks/useAuth" import useAuth from "@/hooks/useAuth"
import { MenuContent, MenuItem, MenuRoot, MenuTrigger } from "../ui/menu"
const UserMenu = () => { const UserMenu = () => {
const { logout } = useAuth() const { user, logout } = useAuth()
const handleLogout = async () => { const handleLogout = async () => {
logout() logout()
@@ -22,36 +16,47 @@ const UserMenu = () => {
return ( return (
<> <>
{/* Desktop */} {/* Desktop */}
<Box <Flex>
display={{ base: "none", md: "block" }} <MenuRoot>
position="fixed" <MenuTrigger asChild p={2}>
top={4} <Button
right={4}
>
<Menu>
<MenuButton
as={IconButton}
aria-label="Options"
icon={<FaUserAstronaut color="white" fontSize="18px" />}
bg="ui.main"
isRound
data-testid="user-menu" data-testid="user-menu"
/> variant="solid"
<MenuList> maxW="150px"
<MenuItem icon={<FiUser fontSize="18px" />} as={Link} to="settings"> truncate
My profile
</MenuItem>
<MenuItem
icon={<FiLogOut fontSize="18px" />}
onClick={handleLogout}
color="ui.danger"
fontWeight="bold"
> >
Log out <FaUserAstronaut fontSize="18" />
<Text>{user?.full_name || "User"}</Text>
</Button>
</MenuTrigger>
<MenuContent>
<Link to="settings">
<MenuItem
closeOnSelect
value="user-settings"
gap={2}
py={2}
style={{ cursor: "pointer" }}
>
<FiUser fontSize="18px" />
<Box flex="1">My Profile</Box>
</MenuItem> </MenuItem>
</MenuList> </Link>
</Menu>
</Box> <MenuItem
value="logout"
gap={2}
py={2}
onClick={handleLogout}
style={{ cursor: "pointer" }}
>
<FiLogOut />
Log Out
</MenuItem>
</MenuContent>
</MenuRoot>
</Flex>
</> </>
) )
} }
+87 -55
View File
@@ -1,37 +1,41 @@
import {
Button,
FormControl,
FormErrorMessage,
FormLabel,
Input,
Modal,
ModalBody,
ModalCloseButton,
ModalContent,
ModalFooter,
ModalHeader,
ModalOverlay,
} from "@chakra-ui/react"
import { useMutation, useQueryClient } from "@tanstack/react-query" import { useMutation, useQueryClient } from "@tanstack/react-query"
import { type SubmitHandler, useForm } from "react-hook-form" import { type SubmitHandler, useForm } from "react-hook-form"
import { type ApiError, type ItemCreate, ItemsService } from "../../client" import {
import useCustomToast from "../../hooks/useCustomToast" Button,
import { handleError } from "../../utils" DialogActionTrigger,
DialogTitle,
Input,
Text,
VStack,
} from "@chakra-ui/react"
import { useState } from "react"
import { FaPlus } from "react-icons/fa"
interface AddItemProps { import { type ItemCreate, ItemsService } from "@/client"
isOpen: boolean import type { ApiError } from "@/client/core/ApiError"
onClose: () => void import useCustomToast from "@/hooks/useCustomToast"
} import { handleError } from "@/utils"
import {
DialogBody,
DialogCloseTrigger,
DialogContent,
DialogFooter,
DialogHeader,
DialogRoot,
DialogTrigger,
} from "../ui/dialog"
import { Field } from "../ui/field"
const AddItem = ({ isOpen, onClose }: AddItemProps) => { const AddItem = () => {
const [isOpen, setIsOpen] = useState(false)
const queryClient = useQueryClient() const queryClient = useQueryClient()
const showToast = useCustomToast() const { showSuccessToast } = useCustomToast()
const { const {
register, register,
handleSubmit, handleSubmit,
reset, reset,
formState: { errors, isSubmitting }, formState: { errors, isValid, isSubmitting },
} = useForm<ItemCreate>({ } = useForm<ItemCreate>({
mode: "onBlur", mode: "onBlur",
criteriaMode: "all", criteriaMode: "all",
@@ -45,12 +49,12 @@ const AddItem = ({ isOpen, onClose }: AddItemProps) => {
mutationFn: (data: ItemCreate) => mutationFn: (data: ItemCreate) =>
ItemsService.createItem({ requestBody: data }), ItemsService.createItem({ requestBody: data }),
onSuccess: () => { onSuccess: () => {
showToast("Success!", "Item created successfully.", "success") showSuccessToast("Item created successfully.")
reset() reset()
onClose() setIsOpen(false)
}, },
onError: (err: ApiError) => { onError: (err: ApiError) => {
handleError(err, showToast) handleError(err)
}, },
onSettled: () => { onSettled: () => {
queryClient.invalidateQueries({ queryKey: ["items"] }) queryClient.invalidateQueries({ queryKey: ["items"] })
@@ -62,20 +66,32 @@ const AddItem = ({ isOpen, onClose }: AddItemProps) => {
} }
return ( return (
<> <DialogRoot
<Modal size={{ base: "xs", md: "md" }}
isOpen={isOpen} placement="center"
onClose={onClose} open={isOpen}
size={{ base: "sm", md: "md" }} onOpenChange={({ open }) => setIsOpen(open)}
isCentered >
<DialogTrigger asChild>
<Button value="add-item" my={4}>
<FaPlus fontSize="16px" />
Add Item
</Button>
</DialogTrigger>
<DialogContent>
<form onSubmit={handleSubmit(onSubmit)}>
<DialogHeader>
<DialogTitle>Add Item</DialogTitle>
</DialogHeader>
<DialogBody>
<Text mb={4}>Fill in the details to add a new item.</Text>
<VStack gap={4}>
<Field
required
invalid={!!errors.title}
errorText={errors.title?.message}
label="Title"
> >
<ModalOverlay />
<ModalContent as="form" onSubmit={handleSubmit(onSubmit)}>
<ModalHeader>Add Item</ModalHeader>
<ModalCloseButton />
<ModalBody pb={6}>
<FormControl isRequired isInvalid={!!errors.title}>
<FormLabel htmlFor="title">Title</FormLabel>
<Input <Input
id="title" id="title"
{...register("title", { {...register("title", {
@@ -84,30 +100,46 @@ const AddItem = ({ isOpen, onClose }: AddItemProps) => {
placeholder="Title" placeholder="Title"
type="text" type="text"
/> />
{errors.title && ( </Field>
<FormErrorMessage>{errors.title.message}</FormErrorMessage>
)} <Field
</FormControl> invalid={!!errors.description}
<FormControl mt={4}> errorText={errors.description?.message}
<FormLabel htmlFor="description">Description</FormLabel> label="Description"
>
<Input <Input
id="description" id="description"
{...register("description")} {...register("description")}
placeholder="Description" placeholder="Description"
type="text" type="text"
/> />
</FormControl> </Field>
</ModalBody> </VStack>
</DialogBody>
<ModalFooter gap={3}> <DialogFooter gap={2}>
<Button variant="primary" type="submit" isLoading={isSubmitting}> <DialogActionTrigger asChild>
<Button
variant="subtle"
colorPalette="gray"
disabled={isSubmitting}
>
Cancel
</Button>
</DialogActionTrigger>
<Button
variant="solid"
type="submit"
disabled={!isValid}
loading={isSubmitting}
>
Save Save
</Button> </Button>
<Button onClick={onClose}>Cancel</Button> </DialogFooter>
</ModalFooter> </form>
</ModalContent> <DialogCloseTrigger />
</Modal> </DialogContent>
</> </DialogRoot>
) )
} }
@@ -0,0 +1,104 @@
import { Button, DialogTitle, Text } from "@chakra-ui/react"
import { useMutation, useQueryClient } from "@tanstack/react-query"
import { useState } from "react"
import { useForm } from "react-hook-form"
import { FiTrash2 } from "react-icons/fi"
import { ItemsService } from "@/client"
import {
DialogActionTrigger,
DialogBody,
DialogCloseTrigger,
DialogContent,
DialogFooter,
DialogHeader,
DialogRoot,
DialogTrigger,
} from "@/components/ui/dialog"
import useCustomToast from "@/hooks/useCustomToast"
const DeleteItem = ({ id }: { id: string }) => {
const [isOpen, setIsOpen] = useState(false)
const queryClient = useQueryClient()
const { showSuccessToast, showErrorToast } = useCustomToast()
const {
handleSubmit,
formState: { isSubmitting },
} = useForm()
const deleteItem = async (id: string) => {
await ItemsService.deleteItem({ id: id })
}
const mutation = useMutation({
mutationFn: deleteItem,
onSuccess: () => {
showSuccessToast("The item was deleted successfully")
setIsOpen(false)
},
onError: () => {
showErrorToast("An error occurred while deleting the item")
},
onSettled: () => {
queryClient.invalidateQueries()
},
})
const onSubmit = async () => {
mutation.mutate(id)
}
return (
<DialogRoot
size={{ base: "xs", md: "md" }}
placement="center"
role="alertdialog"
open={isOpen}
onOpenChange={({ open }) => setIsOpen(open)}
>
<DialogTrigger asChild>
<Button variant="ghost" size="sm" colorPalette="red">
<FiTrash2 fontSize="16px" />
Delete Item
</Button>
</DialogTrigger>
<DialogContent>
<form onSubmit={handleSubmit(onSubmit)}>
<DialogCloseTrigger />
<DialogHeader>
<DialogTitle>Delete Item</DialogTitle>
</DialogHeader>
<DialogBody>
<Text mb={4}>
This item will be permanently deleted. Are you sure? You will not
be able to undo this action.
</Text>
</DialogBody>
<DialogFooter gap={2}>
<DialogActionTrigger asChild>
<Button
variant="subtle"
colorPalette="gray"
disabled={isSubmitting}
>
Cancel
</Button>
</DialogActionTrigger>
<Button
variant="solid"
colorPalette="red"
type="submit"
loading={isSubmitting}
>
Delete
</Button>
</DialogFooter>
</form>
</DialogContent>
</DialogRoot>
)
}
export default DeleteItem
+92 -65
View File
@@ -1,123 +1,150 @@
import { import {
Button, Button,
FormControl, ButtonGroup,
FormErrorMessage, DialogActionTrigger,
FormLabel,
Input, Input,
Modal, Text,
ModalBody, VStack,
ModalCloseButton,
ModalContent,
ModalFooter,
ModalHeader,
ModalOverlay,
} from "@chakra-ui/react" } from "@chakra-ui/react"
import { useMutation, useQueryClient } from "@tanstack/react-query" import { useMutation, useQueryClient } from "@tanstack/react-query"
import { useState } from "react"
import { type SubmitHandler, useForm } from "react-hook-form" import { type SubmitHandler, useForm } from "react-hook-form"
import { FaExchangeAlt } from "react-icons/fa"
import { type ApiError, type ItemPublic, ItemsService } from "@/client"
import useCustomToast from "@/hooks/useCustomToast"
import { handleError } from "@/utils"
import { import {
type ApiError, DialogBody,
type ItemPublic, DialogCloseTrigger,
type ItemUpdate, DialogContent,
ItemsService, DialogFooter,
} from "../../client" DialogHeader,
import useCustomToast from "../../hooks/useCustomToast" DialogRoot,
import { handleError } from "../../utils" DialogTitle,
DialogTrigger,
} from "../ui/dialog"
import { Field } from "../ui/field"
interface EditItemProps { interface EditItemProps {
item: ItemPublic item: ItemPublic
isOpen: boolean
onClose: () => void
} }
const EditItem = ({ item, isOpen, onClose }: EditItemProps) => { interface ItemUpdateForm {
title: string
description?: string
}
const EditItem = ({ item }: EditItemProps) => {
const [isOpen, setIsOpen] = useState(false)
const queryClient = useQueryClient() const queryClient = useQueryClient()
const showToast = useCustomToast() const { showSuccessToast } = useCustomToast()
const { const {
register, register,
handleSubmit, handleSubmit,
reset, reset,
formState: { isSubmitting, errors, isDirty }, formState: { errors, isSubmitting },
} = useForm<ItemUpdate>({ } = useForm<ItemUpdateForm>({
mode: "onBlur", mode: "onBlur",
criteriaMode: "all", criteriaMode: "all",
defaultValues: item, defaultValues: {
...item,
description: item.description ?? undefined,
},
}) })
const mutation = useMutation({ const mutation = useMutation({
mutationFn: (data: ItemUpdate) => mutationFn: (data: ItemUpdateForm) =>
ItemsService.updateItem({ id: item.id, requestBody: data }), ItemsService.updateItem({ id: item.id, requestBody: data }),
onSuccess: () => { onSuccess: () => {
showToast("Success!", "Item updated successfully.", "success") showSuccessToast("Item updated successfully.")
onClose() reset()
setIsOpen(false)
}, },
onError: (err: ApiError) => { onError: (err: ApiError) => {
handleError(err, showToast) handleError(err)
}, },
onSettled: () => { onSettled: () => {
queryClient.invalidateQueries({ queryKey: ["items"] }) queryClient.invalidateQueries({ queryKey: ["items"] })
}, },
}) })
const onSubmit: SubmitHandler<ItemUpdate> = async (data) => { const onSubmit: SubmitHandler<ItemUpdateForm> = async (data) => {
mutation.mutate(data) mutation.mutate(data)
} }
const onCancel = () => {
reset()
onClose()
}
return ( return (
<> <DialogRoot
<Modal size={{ base: "xs", md: "md" }}
isOpen={isOpen} placement="center"
onClose={onClose} open={isOpen}
size={{ base: "sm", md: "md" }} onOpenChange={({ open }) => setIsOpen(open)}
isCentered >
<DialogTrigger asChild>
<Button variant="ghost">
<FaExchangeAlt fontSize="16px" />
Edit Item
</Button>
</DialogTrigger>
<DialogContent>
<form onSubmit={handleSubmit(onSubmit)}>
<DialogHeader>
<DialogTitle>Edit Item</DialogTitle>
</DialogHeader>
<DialogBody>
<Text mb={4}>Update the item details below.</Text>
<VStack gap={4}>
<Field
required
invalid={!!errors.title}
errorText={errors.title?.message}
label="Title"
> >
<ModalOverlay />
<ModalContent as="form" onSubmit={handleSubmit(onSubmit)}>
<ModalHeader>Edit Item</ModalHeader>
<ModalCloseButton />
<ModalBody pb={6}>
<FormControl isInvalid={!!errors.title}>
<FormLabel htmlFor="title">Title</FormLabel>
<Input <Input
id="title" id="title"
{...register("title", { {...register("title", {
required: "Title is required", required: "Title is required",
})} })}
placeholder="Title"
type="text" type="text"
/> />
{errors.title && ( </Field>
<FormErrorMessage>{errors.title.message}</FormErrorMessage>
)} <Field
</FormControl> invalid={!!errors.description}
<FormControl mt={4}> errorText={errors.description?.message}
<FormLabel htmlFor="description">Description</FormLabel> label="Description"
>
<Input <Input
id="description" id="description"
{...register("description")} {...register("description")}
placeholder="Description" placeholder="Description"
type="text" type="text"
/> />
</FormControl> </Field>
</ModalBody> </VStack>
<ModalFooter gap={3}> </DialogBody>
<DialogFooter gap={2}>
<ButtonGroup>
<DialogActionTrigger asChild>
<Button <Button
variant="primary" variant="subtle"
type="submit" colorPalette="gray"
isLoading={isSubmitting} disabled={isSubmitting}
isDisabled={!isDirty}
> >
Cancel
</Button>
</DialogActionTrigger>
<Button variant="solid" type="submit" loading={isSubmitting}>
Save Save
</Button> </Button>
<Button onClick={onCancel}>Cancel</Button> </ButtonGroup>
</ModalFooter> </DialogFooter>
</ModalContent> </form>
</Modal> <DialogCloseTrigger />
</> </DialogContent>
</DialogRoot>
) )
} }
@@ -0,0 +1,34 @@
import { Skeleton, Table } from "@chakra-ui/react"
const PendingItems = () => (
<Table.Root size={{ base: "sm", md: "md" }}>
<Table.Header>
<Table.Row>
<Table.ColumnHeader w="30%">ID</Table.ColumnHeader>
<Table.ColumnHeader w="30%">Title</Table.ColumnHeader>
<Table.ColumnHeader w="30%">Description</Table.ColumnHeader>
<Table.ColumnHeader w="10%">Actions</Table.ColumnHeader>
</Table.Row>
</Table.Header>
<Table.Body>
{[...Array(5)].map((_, index) => (
<Table.Row key={index}>
<Table.Cell>
<Skeleton h="20px" />
</Table.Cell>
<Table.Cell>
<Skeleton h="20px" />
</Table.Cell>
<Table.Cell>
<Skeleton h="20px" />
</Table.Cell>
<Table.Cell>
<Skeleton h="20px" />
</Table.Cell>
</Table.Row>
))}
</Table.Body>
</Table.Root>
)
export default PendingItems
@@ -0,0 +1,38 @@
import { Skeleton, Table } from "@chakra-ui/react"
const PendingUsers = () => (
<Table.Root size={{ base: "sm", md: "md" }}>
<Table.Header>
<Table.Row>
<Table.ColumnHeader w="20%">Full name</Table.ColumnHeader>
<Table.ColumnHeader w="25%">Email</Table.ColumnHeader>
<Table.ColumnHeader w="15%">Role</Table.ColumnHeader>
<Table.ColumnHeader w="20%">Status</Table.ColumnHeader>
<Table.ColumnHeader w="20%">Actions</Table.ColumnHeader>
</Table.Row>
</Table.Header>
<Table.Body>
{[...Array(5)].map((_, index) => (
<Table.Row key={index}>
<Table.Cell>
<Skeleton h="20px" />
</Table.Cell>
<Table.Cell>
<Skeleton h="20px" />
</Table.Cell>
<Table.Cell>
<Skeleton h="20px" />
</Table.Cell>
<Table.Cell>
<Skeleton h="20px" />
</Table.Cell>
<Table.Cell>
<Skeleton h="20px" />
</Table.Cell>
</Table.Row>
))}
</Table.Body>
</Table.Root>
)
export default PendingUsers
@@ -1,15 +1,10 @@
import { import { Container, Heading, Stack } from "@chakra-ui/react"
Badge, import { useTheme } from "next-themes"
Container,
Heading, import { Radio, RadioGroup } from "@/components/ui/radio"
Radio,
RadioGroup,
Stack,
useColorMode,
} from "@chakra-ui/react"
const Appearance = () => { const Appearance = () => {
const { colorMode, toggleColorMode } = useColorMode() const { theme, setTheme } = useTheme()
return ( return (
<> <>
@@ -17,18 +12,16 @@ const Appearance = () => {
<Heading size="sm" py={4}> <Heading size="sm" py={4}>
Appearance Appearance
</Heading> </Heading>
<RadioGroup onChange={toggleColorMode} value={colorMode}>
<RadioGroup
onValueChange={(e) => setTheme(e.value)}
value={theme}
colorPalette="teal"
>
<Stack> <Stack>
{/* TODO: Add system default option */} <Radio value="system">System</Radio>
<Radio value="light" colorScheme="teal"> <Radio value="light">Light Mode</Radio>
Light Mode <Radio value="dark">Dark Mode</Radio>
<Badge ml="1" colorScheme="teal">
Default
</Badge>
</Radio>
<Radio value="dark" colorScheme="teal">
Dark Mode
</Radio>
</Stack> </Stack>
</RadioGroup> </RadioGroup>
</Container> </Container>
@@ -1,34 +1,25 @@
import { import { Box, Button, Container, Heading, VStack } from "@chakra-ui/react"
Box,
Button,
Container,
FormControl,
FormErrorMessage,
FormLabel,
Heading,
Input,
useColorModeValue,
} from "@chakra-ui/react"
import { useMutation } from "@tanstack/react-query" import { useMutation } from "@tanstack/react-query"
import { type SubmitHandler, useForm } from "react-hook-form" import { type SubmitHandler, useForm } from "react-hook-form"
import { FiLock } from "react-icons/fi"
import { type ApiError, type UpdatePassword, UsersService } from "../../client" import { type ApiError, type UpdatePassword, UsersService } from "@/client"
import useCustomToast from "../../hooks/useCustomToast" import useCustomToast from "@/hooks/useCustomToast"
import { confirmPasswordRules, handleError, passwordRules } from "../../utils" import { confirmPasswordRules, handleError, passwordRules } from "@/utils"
import { PasswordInput } from "../ui/password-input"
interface UpdatePasswordForm extends UpdatePassword { interface UpdatePasswordForm extends UpdatePassword {
confirm_password: string confirm_password: string
} }
const ChangePassword = () => { const ChangePassword = () => {
const color = useColorModeValue("inherit", "ui.light") const { showSuccessToast } = useCustomToast()
const showToast = useCustomToast()
const { const {
register, register,
handleSubmit, handleSubmit,
reset, reset,
getValues, getValues,
formState: { errors, isSubmitting }, formState: { errors, isValid, isSubmitting },
} = useForm<UpdatePasswordForm>({ } = useForm<UpdatePasswordForm>({
mode: "onBlur", mode: "onBlur",
criteriaMode: "all", criteriaMode: "all",
@@ -38,11 +29,11 @@ const ChangePassword = () => {
mutationFn: (data: UpdatePassword) => mutationFn: (data: UpdatePassword) =>
UsersService.updatePasswordMe({ requestBody: data }), UsersService.updatePasswordMe({ requestBody: data }),
onSuccess: () => { onSuccess: () => {
showToast("Success!", "Password updated successfully.", "success") showSuccessToast("Password updated successfully.")
reset() reset()
}, },
onError: (err: ApiError) => { onError: (err: ApiError) => {
handleError(err, showToast) handleError(err)
}, },
}) })
@@ -57,60 +48,39 @@ const ChangePassword = () => {
Change Password Change Password
</Heading> </Heading>
<Box <Box
w={{ sm: "full", md: "50%" }} w={{ sm: "full", md: "300px" }}
as="form" as="form"
onSubmit={handleSubmit(onSubmit)} onSubmit={handleSubmit(onSubmit)}
> >
<FormControl isRequired isInvalid={!!errors.current_password}> <VStack gap={4}>
<FormLabel color={color} htmlFor="current_password"> <PasswordInput
Current Password type="current_password"
</FormLabel> startElement={<FiLock />}
<Input {...register("current_password", passwordRules())}
id="current_password" placeholder="Current Password"
{...register("current_password")} errors={errors}
placeholder="Password"
type="password"
w="auto"
/> />
{errors.current_password && ( <PasswordInput
<FormErrorMessage> type="new_password"
{errors.current_password.message} startElement={<FiLock />}
</FormErrorMessage>
)}
</FormControl>
<FormControl mt={4} isRequired isInvalid={!!errors.new_password}>
<FormLabel htmlFor="password">Set Password</FormLabel>
<Input
id="password"
{...register("new_password", passwordRules())} {...register("new_password", passwordRules())}
placeholder="Password" placeholder="New Password"
type="password" errors={errors}
w="auto"
/> />
{errors.new_password && ( <PasswordInput
<FormErrorMessage>{errors.new_password.message}</FormErrorMessage> type="confirm_password"
)} startElement={<FiLock />}
</FormControl>
<FormControl mt={4} isRequired isInvalid={!!errors.confirm_password}>
<FormLabel htmlFor="confirm_password">Confirm Password</FormLabel>
<Input
id="confirm_password"
{...register("confirm_password", confirmPasswordRules(getValues))} {...register("confirm_password", confirmPasswordRules(getValues))}
placeholder="Password" placeholder="Confirm Password"
type="password" errors={errors}
w="auto"
/> />
{errors.confirm_password && ( </VStack>
<FormErrorMessage>
{errors.confirm_password.message}
</FormErrorMessage>
)}
</FormControl>
<Button <Button
variant="primary" variant="solid"
mt={4} mt={4}
type="submit" type="submit"
isLoading={isSubmitting} loading={isSubmitting}
disabled={!isValid}
> >
Save Save
</Button> </Button>
@@ -1,18 +1,9 @@
import { import { Container, Heading, Text } from "@chakra-ui/react"
Button,
Container,
Heading,
Text,
useDisclosure,
} from "@chakra-ui/react"
import DeleteConfirmation from "./DeleteConfirmation" import DeleteConfirmation from "./DeleteConfirmation"
const DeleteAccount = () => { const DeleteAccount = () => {
const confirmationModal = useDisclosure()
return ( return (
<>
<Container maxW="full"> <Container maxW="full">
<Heading size="sm" py={4}> <Heading size="sm" py={4}>
Delete Account Delete Account
@@ -21,15 +12,8 @@ const DeleteAccount = () => {
Permanently delete your data and everything associated with your Permanently delete your data and everything associated with your
account. account.
</Text> </Text>
<Button variant="danger" mt={4} onClick={confirmationModal.onOpen}> <DeleteConfirmation />
Delete
</Button>
<DeleteConfirmation
isOpen={confirmationModal.isOpen}
onClose={confirmationModal.onClose}
/>
</Container> </Container>
</>
) )
} }
export default DeleteAccount export default DeleteAccount
@@ -1,30 +1,28 @@
import { import { Button, ButtonGroup, Text } from "@chakra-ui/react"
AlertDialog,
AlertDialogBody,
AlertDialogContent,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogOverlay,
Button,
} from "@chakra-ui/react"
import { useMutation, useQueryClient } from "@tanstack/react-query" import { useMutation, useQueryClient } from "@tanstack/react-query"
import React from "react" import { useState } from "react"
import { useForm } from "react-hook-form" import { useForm } from "react-hook-form"
import { type ApiError, UsersService } from "../../client" import { type ApiError, UsersService } from "@/client"
import useAuth from "../../hooks/useAuth" import {
import useCustomToast from "../../hooks/useCustomToast" DialogActionTrigger,
import { handleError } from "../../utils" DialogBody,
DialogCloseTrigger,
DialogContent,
DialogFooter,
DialogHeader,
DialogRoot,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog"
import useAuth from "@/hooks/useAuth"
import useCustomToast from "@/hooks/useCustomToast"
import { handleError } from "@/utils"
interface DeleteProps { const DeleteConfirmation = () => {
isOpen: boolean const [isOpen, setIsOpen] = useState(false)
onClose: () => void
}
const DeleteConfirmation = ({ isOpen, onClose }: DeleteProps) => {
const queryClient = useQueryClient() const queryClient = useQueryClient()
const showToast = useCustomToast() const { showSuccessToast } = useCustomToast()
const cancelRef = React.useRef<HTMLButtonElement | null>(null)
const { const {
handleSubmit, handleSubmit,
formState: { isSubmitting }, formState: { isSubmitting },
@@ -34,16 +32,12 @@ const DeleteConfirmation = ({ isOpen, onClose }: DeleteProps) => {
const mutation = useMutation({ const mutation = useMutation({
mutationFn: () => UsersService.deleteUserMe(), mutationFn: () => UsersService.deleteUserMe(),
onSuccess: () => { onSuccess: () => {
showToast( showSuccessToast("Your account has been successfully deleted")
"Success", setIsOpen(false)
"Your account has been successfully deleted.",
"success",
)
logout() logout()
onClose()
}, },
onError: (err: ApiError) => { onError: (err: ApiError) => {
handleError(err, showToast) handleError(err)
}, },
onSettled: () => { onSettled: () => {
queryClient.invalidateQueries({ queryKey: ["currentUser"] }) queryClient.invalidateQueries({ queryKey: ["currentUser"] })
@@ -56,39 +50,58 @@ const DeleteConfirmation = ({ isOpen, onClose }: DeleteProps) => {
return ( return (
<> <>
<AlertDialog <DialogRoot
isOpen={isOpen} size={{ base: "xs", md: "md" }}
onClose={onClose} role="alertdialog"
leastDestructiveRef={cancelRef} placement="center"
size={{ base: "sm", md: "md" }} open={isOpen}
isCentered onOpenChange={({ open }) => setIsOpen(open)}
> >
<AlertDialogOverlay> <DialogTrigger asChild>
<AlertDialogContent as="form" onSubmit={handleSubmit(onSubmit)}> <Button variant="solid" colorPalette="red" mt={4}>
<AlertDialogHeader>Confirmation Required</AlertDialogHeader> Delete
</Button>
</DialogTrigger>
<AlertDialogBody> <DialogContent>
<form onSubmit={handleSubmit(onSubmit)}>
<DialogCloseTrigger />
<DialogHeader>
<DialogTitle>Confirmation Required</DialogTitle>
</DialogHeader>
<DialogBody>
<Text mb={4}>
All your account data will be{" "} All your account data will be{" "}
<strong>permanently deleted.</strong> If you are sure, please <strong>permanently deleted.</strong> If you are sure, please
click <strong>"Confirm"</strong> to proceed. This action cannot be click <strong>"Confirm"</strong> to proceed. This action cannot
undone. be undone.
</AlertDialogBody> </Text>
</DialogBody>
<AlertDialogFooter gap={3}> <DialogFooter gap={2}>
<Button variant="danger" type="submit" isLoading={isSubmitting}> <ButtonGroup>
Confirm <DialogActionTrigger asChild>
</Button>
<Button <Button
ref={cancelRef} variant="subtle"
onClick={onClose} colorPalette="gray"
isDisabled={isSubmitting} disabled={isSubmitting}
> >
Cancel Cancel
</Button> </Button>
</AlertDialogFooter> </DialogActionTrigger>
</AlertDialogContent> <Button
</AlertDialogOverlay> variant="solid"
</AlertDialog> colorPalette="red"
type="submit"
loading={isSubmitting}
>
Delete
</Button>
</ButtonGroup>
</DialogFooter>
</form>
</DialogContent>
</DialogRoot>
</> </>
) )
} }
@@ -3,13 +3,9 @@ import {
Button, Button,
Container, Container,
Flex, Flex,
FormControl,
FormErrorMessage,
FormLabel,
Heading, Heading,
Input, Input,
Text, Text,
useColorModeValue,
} from "@chakra-ui/react" } from "@chakra-ui/react"
import { useMutation, useQueryClient } from "@tanstack/react-query" import { useMutation, useQueryClient } from "@tanstack/react-query"
import { useState } from "react" import { useState } from "react"
@@ -20,15 +16,15 @@ import {
type UserPublic, type UserPublic,
type UserUpdateMe, type UserUpdateMe,
UsersService, UsersService,
} from "../../client" } from "@/client"
import useAuth from "../../hooks/useAuth" import useAuth from "@/hooks/useAuth"
import useCustomToast from "../../hooks/useCustomToast" import useCustomToast from "@/hooks/useCustomToast"
import { emailPattern, handleError } from "../../utils" import { emailPattern, handleError } from "@/utils"
import { Field } from "../ui/field"
const UserInformation = () => { const UserInformation = () => {
const queryClient = useQueryClient() const queryClient = useQueryClient()
const color = useColorModeValue("inherit", "ui.light") const { showSuccessToast } = useCustomToast()
const showToast = useCustomToast()
const [editMode, setEditMode] = useState(false) const [editMode, setEditMode] = useState(false)
const { user: currentUser } = useAuth() const { user: currentUser } = useAuth()
const { const {
@@ -54,10 +50,10 @@ const UserInformation = () => {
mutationFn: (data: UserUpdateMe) => mutationFn: (data: UserUpdateMe) =>
UsersService.updateUserMe({ requestBody: data }), UsersService.updateUserMe({ requestBody: data }),
onSuccess: () => { onSuccess: () => {
showToast("Success!", "User updated successfully.", "success") showSuccessToast("User updated successfully.")
}, },
onError: (err: ApiError) => { onError: (err: ApiError) => {
handleError(err, showToast) handleError(err)
}, },
onSettled: () => { onSettled: () => {
queryClient.invalidateQueries() queryClient.invalidateQueries()
@@ -84,13 +80,9 @@ const UserInformation = () => {
as="form" as="form"
onSubmit={handleSubmit(onSubmit)} onSubmit={handleSubmit(onSubmit)}
> >
<FormControl> <Field label="Full name">
<FormLabel color={color} htmlFor="name">
Full name
</FormLabel>
{editMode ? ( {editMode ? (
<Input <Input
id="name"
{...register("full_name", { maxLength: 30 })} {...register("full_name", { maxLength: 30 })}
type="text" type="text"
size="md" size="md"
@@ -98,23 +90,24 @@ const UserInformation = () => {
/> />
) : ( ) : (
<Text <Text
size="md" fontSize="md"
py={2} py={2}
color={!currentUser?.full_name ? "ui.dim" : "inherit"} color={!currentUser?.full_name ? "gray" : "inherit"}
isTruncated truncate
maxWidth="250px" maxWidth="250px"
> >
{currentUser?.full_name || "N/A"} {currentUser?.full_name || "N/A"}
</Text> </Text>
)} )}
</FormControl> </Field>
<FormControl mt={4} isInvalid={!!errors.email}> <Field
<FormLabel color={color} htmlFor="email"> mt={4}
Email label="Email"
</FormLabel> invalid={!!errors.email}
errorText={errors.email?.message}
>
{editMode ? ( {editMode ? (
<Input <Input
id="email"
{...register("email", { {...register("email", {
required: "Email is required", required: "Email is required",
pattern: emailPattern, pattern: emailPattern,
@@ -124,26 +117,28 @@ const UserInformation = () => {
w="auto" w="auto"
/> />
) : ( ) : (
<Text size="md" py={2} isTruncated maxWidth="250px"> <Text fontSize="md" py={2} truncate maxWidth="250px">
{currentUser?.email} {currentUser?.email}
</Text> </Text>
)} )}
{errors.email && ( </Field>
<FormErrorMessage>{errors.email.message}</FormErrorMessage>
)}
</FormControl>
<Flex mt={4} gap={3}> <Flex mt={4} gap={3}>
<Button <Button
variant="primary" variant="solid"
onClick={toggleEditMode} onClick={toggleEditMode}
type={editMode ? "button" : "submit"} type={editMode ? "button" : "submit"}
isLoading={editMode ? isSubmitting : false} loading={editMode ? isSubmitting : false}
isDisabled={editMode ? !isDirty || !getValues("email") : false} disabled={editMode ? !isDirty || !getValues("email") : false}
> >
{editMode ? "Save" : "Edit"} {editMode ? "Save" : "Edit"}
</Button> </Button>
{editMode && ( {editMode && (
<Button onClick={onCancel} isDisabled={isSubmitting}> <Button
variant="subtle"
colorPalette="gray"
onClick={onCancel}
disabled={isSubmitting}
>
Cancel Cancel
</Button> </Button>
)} )}
+40
View File
@@ -0,0 +1,40 @@
import type { ButtonProps as ChakraButtonProps } from "@chakra-ui/react"
import {
AbsoluteCenter,
Button as ChakraButton,
Span,
Spinner,
} from "@chakra-ui/react"
import * as React from "react"
interface ButtonLoadingProps {
loading?: boolean
loadingText?: React.ReactNode
}
export interface ButtonProps extends ChakraButtonProps, ButtonLoadingProps {}
export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
function Button(props, ref) {
const { loading, disabled, loadingText, children, ...rest } = props
return (
<ChakraButton disabled={loading || disabled} ref={ref} {...rest}>
{loading && !loadingText ? (
<>
<AbsoluteCenter display="inline-flex">
<Spinner size="inherit" color="inherit" />
</AbsoluteCenter>
<Span opacity={0}>{children}</Span>
</>
) : loading && loadingText ? (
<>
<Spinner size="inherit" color="inherit" />
{loadingText}
</>
) : (
children
)}
</ChakraButton>
)
},
)
+25
View File
@@ -0,0 +1,25 @@
import { Checkbox as ChakraCheckbox } from "@chakra-ui/react"
import * as React from "react"
export interface CheckboxProps extends ChakraCheckbox.RootProps {
icon?: React.ReactNode
inputProps?: React.InputHTMLAttributes<HTMLInputElement>
rootRef?: React.Ref<HTMLLabelElement>
}
export const Checkbox = React.forwardRef<HTMLInputElement, CheckboxProps>(
function Checkbox(props, ref) {
const { icon, children, inputProps, rootRef, ...rest } = props
return (
<ChakraCheckbox.Root ref={rootRef} {...rest}>
<ChakraCheckbox.HiddenInput ref={ref} {...inputProps} />
<ChakraCheckbox.Control>
{icon || <ChakraCheckbox.Indicator />}
</ChakraCheckbox.Control>
{children != null && (
<ChakraCheckbox.Label>{children}</ChakraCheckbox.Label>
)}
</ChakraCheckbox.Root>
)
},
)
@@ -0,0 +1,17 @@
import type { ButtonProps } from "@chakra-ui/react"
import { IconButton as ChakraIconButton } from "@chakra-ui/react"
import * as React from "react"
import { LuX } from "react-icons/lu"
export type CloseButtonProps = ButtonProps
export const CloseButton = React.forwardRef<
HTMLButtonElement,
CloseButtonProps
>(function CloseButton(props, ref) {
return (
<ChakraIconButton variant="ghost" aria-label="Close" ref={ref} {...props}>
{props.children ?? <LuX />}
</ChakraIconButton>
)
})
+107
View File
@@ -0,0 +1,107 @@
"use client"
import type { IconButtonProps, SpanProps } from "@chakra-ui/react"
import { ClientOnly, IconButton, Skeleton, Span } from "@chakra-ui/react"
import { ThemeProvider, useTheme } from "next-themes"
import type { ThemeProviderProps } from "next-themes"
import * as React from "react"
import { LuMoon, LuSun } from "react-icons/lu"
export interface ColorModeProviderProps extends ThemeProviderProps {}
export function ColorModeProvider(props: ColorModeProviderProps) {
return (
<ThemeProvider attribute="class" disableTransitionOnChange {...props} />
)
}
export type ColorMode = "light" | "dark"
export interface UseColorModeReturn {
colorMode: ColorMode
setColorMode: (colorMode: ColorMode) => void
toggleColorMode: () => void
}
export function useColorMode(): UseColorModeReturn {
const { resolvedTheme, setTheme } = useTheme()
const toggleColorMode = () => {
setTheme(resolvedTheme === "dark" ? "light" : "dark")
}
return {
colorMode: resolvedTheme as ColorMode,
setColorMode: setTheme,
toggleColorMode,
}
}
export function useColorModeValue<T>(light: T, dark: T) {
const { colorMode } = useColorMode()
return colorMode === "dark" ? dark : light
}
export function ColorModeIcon() {
const { colorMode } = useColorMode()
return colorMode === "dark" ? <LuMoon /> : <LuSun />
}
interface ColorModeButtonProps extends Omit<IconButtonProps, "aria-label"> {}
export const ColorModeButton = React.forwardRef<
HTMLButtonElement,
ColorModeButtonProps
>(function ColorModeButton(props, ref) {
const { toggleColorMode } = useColorMode()
return (
<ClientOnly fallback={<Skeleton boxSize="8" />}>
<IconButton
onClick={toggleColorMode}
variant="ghost"
aria-label="Toggle color mode"
size="sm"
ref={ref}
{...props}
css={{
_icon: {
width: "5",
height: "5",
},
}}
>
<ColorModeIcon />
</IconButton>
</ClientOnly>
)
})
export const LightMode = React.forwardRef<HTMLSpanElement, SpanProps>(
function LightMode(props, ref) {
return (
<Span
color="fg"
display="contents"
className="chakra-theme light"
colorPalette="gray"
colorScheme="light"
ref={ref}
{...props}
/>
)
},
)
export const DarkMode = React.forwardRef<HTMLSpanElement, SpanProps>(
function DarkMode(props, ref) {
return (
<Span
color="fg"
display="contents"
className="chakra-theme dark"
colorPalette="gray"
colorScheme="dark"
ref={ref}
{...props}
/>
)
},
)
+62
View File
@@ -0,0 +1,62 @@
import { Dialog as ChakraDialog, Portal } from "@chakra-ui/react"
import * as React from "react"
import { CloseButton } from "./close-button"
interface DialogContentProps extends ChakraDialog.ContentProps {
portalled?: boolean
portalRef?: React.RefObject<HTMLElement>
backdrop?: boolean
}
export const DialogContent = React.forwardRef<
HTMLDivElement,
DialogContentProps
>(function DialogContent(props, ref) {
const {
children,
portalled = true,
portalRef,
backdrop = true,
...rest
} = props
return (
<Portal disabled={!portalled} container={portalRef}>
{backdrop && <ChakraDialog.Backdrop />}
<ChakraDialog.Positioner>
<ChakraDialog.Content ref={ref} {...rest} asChild={false}>
{children}
</ChakraDialog.Content>
</ChakraDialog.Positioner>
</Portal>
)
})
export const DialogCloseTrigger = React.forwardRef<
HTMLButtonElement,
ChakraDialog.CloseTriggerProps
>(function DialogCloseTrigger(props, ref) {
return (
<ChakraDialog.CloseTrigger
position="absolute"
top="2"
insetEnd="2"
{...props}
asChild
>
<CloseButton size="sm" ref={ref}>
{props.children}
</CloseButton>
</ChakraDialog.CloseTrigger>
)
})
export const DialogRoot = ChakraDialog.Root
export const DialogFooter = ChakraDialog.Footer
export const DialogHeader = ChakraDialog.Header
export const DialogBody = ChakraDialog.Body
export const DialogBackdrop = ChakraDialog.Backdrop
export const DialogTitle = ChakraDialog.Title
export const DialogDescription = ChakraDialog.Description
export const DialogTrigger = ChakraDialog.Trigger
export const DialogActionTrigger = ChakraDialog.ActionTrigger
+52
View File
@@ -0,0 +1,52 @@
import { Drawer as ChakraDrawer, Portal } from "@chakra-ui/react"
import * as React from "react"
import { CloseButton } from "./close-button"
interface DrawerContentProps extends ChakraDrawer.ContentProps {
portalled?: boolean
portalRef?: React.RefObject<HTMLElement>
offset?: ChakraDrawer.ContentProps["padding"]
}
export const DrawerContent = React.forwardRef<
HTMLDivElement,
DrawerContentProps
>(function DrawerContent(props, ref) {
const { children, portalled = true, portalRef, offset, ...rest } = props
return (
<Portal disabled={!portalled} container={portalRef}>
<ChakraDrawer.Positioner padding={offset}>
<ChakraDrawer.Content ref={ref} {...rest} asChild={false}>
{children}
</ChakraDrawer.Content>
</ChakraDrawer.Positioner>
</Portal>
)
})
export const DrawerCloseTrigger = React.forwardRef<
HTMLButtonElement,
ChakraDrawer.CloseTriggerProps
>(function DrawerCloseTrigger(props, ref) {
return (
<ChakraDrawer.CloseTrigger
position="absolute"
top="2"
insetEnd="2"
{...props}
asChild
>
<CloseButton size="sm" ref={ref} />
</ChakraDrawer.CloseTrigger>
)
})
export const DrawerTrigger = ChakraDrawer.Trigger
export const DrawerRoot = ChakraDrawer.Root
export const DrawerFooter = ChakraDrawer.Footer
export const DrawerHeader = ChakraDrawer.Header
export const DrawerBody = ChakraDrawer.Body
export const DrawerBackdrop = ChakraDrawer.Backdrop
export const DrawerDescription = ChakraDrawer.Description
export const DrawerTitle = ChakraDrawer.Title
export const DrawerActionTrigger = ChakraDrawer.ActionTrigger
+33
View File
@@ -0,0 +1,33 @@
import { Field as ChakraField } from "@chakra-ui/react"
import * as React from "react"
export interface FieldProps extends Omit<ChakraField.RootProps, "label"> {
label?: React.ReactNode
helperText?: React.ReactNode
errorText?: React.ReactNode
optionalText?: React.ReactNode
}
export const Field = React.forwardRef<HTMLDivElement, FieldProps>(
function Field(props, ref) {
const { label, children, helperText, errorText, optionalText, ...rest } =
props
return (
<ChakraField.Root ref={ref} {...rest}>
{label && (
<ChakraField.Label>
{label}
<ChakraField.RequiredIndicator fallback={optionalText} />
</ChakraField.Label>
)}
{children}
{helperText && (
<ChakraField.HelperText>{helperText}</ChakraField.HelperText>
)}
{errorText && (
<ChakraField.ErrorText>{errorText}</ChakraField.ErrorText>
)}
</ChakraField.Root>
)
},
)
@@ -0,0 +1,53 @@
import type { BoxProps, InputElementProps } from "@chakra-ui/react"
import { Group, InputElement } from "@chakra-ui/react"
import * as React from "react"
export interface InputGroupProps extends BoxProps {
startElementProps?: InputElementProps
endElementProps?: InputElementProps
startElement?: React.ReactNode
endElement?: React.ReactNode
children: React.ReactElement<InputElementProps>
startOffset?: InputElementProps["paddingStart"]
endOffset?: InputElementProps["paddingEnd"]
}
export const InputGroup = React.forwardRef<HTMLDivElement, InputGroupProps>(
function InputGroup(props, ref) {
const {
startElement,
startElementProps,
endElement,
endElementProps,
children,
startOffset = "6px",
endOffset = "6px",
...rest
} = props
const child =
React.Children.only<React.ReactElement<InputElementProps>>(children)
return (
<Group ref={ref} {...rest}>
{startElement && (
<InputElement pointerEvents="none" {...startElementProps}>
{startElement}
</InputElement>
)}
{React.cloneElement(child, {
...(startElement && {
ps: `calc(var(--input-height) - ${startOffset})`,
}),
...(endElement && { pe: `calc(var(--input-height) - ${endOffset})` }),
...children.props,
})}
{endElement && (
<InputElement placement="end" {...endElementProps}>
{endElement}
</InputElement>
)}
</Group>
)
},
)
@@ -0,0 +1,12 @@
"use client"
import type { HTMLChakraProps, RecipeProps } from "@chakra-ui/react"
import { createRecipeContext } from "@chakra-ui/react"
export interface LinkButtonProps
extends HTMLChakraProps<"a", RecipeProps<"button">> {}
const { withContext } = createRecipeContext({ key: "button" })
// Replace "a" with your framework's link component
export const LinkButton = withContext<HTMLAnchorElement, LinkButtonProps>("a")
+112
View File
@@ -0,0 +1,112 @@
"use client"
import { AbsoluteCenter, Menu as ChakraMenu, Portal } from "@chakra-ui/react"
import * as React from "react"
import { LuCheck, LuChevronRight } from "react-icons/lu"
interface MenuContentProps extends ChakraMenu.ContentProps {
portalled?: boolean
portalRef?: React.RefObject<HTMLElement>
}
export const MenuContent = React.forwardRef<HTMLDivElement, MenuContentProps>(
function MenuContent(props, ref) {
const { portalled = true, portalRef, ...rest } = props
return (
<Portal disabled={!portalled} container={portalRef}>
<ChakraMenu.Positioner>
<ChakraMenu.Content ref={ref} {...rest} />
</ChakraMenu.Positioner>
</Portal>
)
},
)
export const MenuArrow = React.forwardRef<
HTMLDivElement,
ChakraMenu.ArrowProps
>(function MenuArrow(props, ref) {
return (
<ChakraMenu.Arrow ref={ref} {...props}>
<ChakraMenu.ArrowTip />
</ChakraMenu.Arrow>
)
})
export const MenuCheckboxItem = React.forwardRef<
HTMLDivElement,
ChakraMenu.CheckboxItemProps
>(function MenuCheckboxItem(props, ref) {
return (
<ChakraMenu.CheckboxItem ps="8" ref={ref} {...props}>
<AbsoluteCenter axis="horizontal" insetStart="4" asChild>
<ChakraMenu.ItemIndicator>
<LuCheck />
</ChakraMenu.ItemIndicator>
</AbsoluteCenter>
{props.children}
</ChakraMenu.CheckboxItem>
)
})
export const MenuRadioItem = React.forwardRef<
HTMLDivElement,
ChakraMenu.RadioItemProps
>(function MenuRadioItem(props, ref) {
const { children, ...rest } = props
return (
<ChakraMenu.RadioItem ps="8" ref={ref} {...rest}>
<AbsoluteCenter axis="horizontal" insetStart="4" asChild>
<ChakraMenu.ItemIndicator>
<LuCheck />
</ChakraMenu.ItemIndicator>
</AbsoluteCenter>
<ChakraMenu.ItemText>{children}</ChakraMenu.ItemText>
</ChakraMenu.RadioItem>
)
})
export const MenuItemGroup = React.forwardRef<
HTMLDivElement,
ChakraMenu.ItemGroupProps
>(function MenuItemGroup(props, ref) {
const { title, children, ...rest } = props
return (
<ChakraMenu.ItemGroup ref={ref} {...rest}>
{title && (
<ChakraMenu.ItemGroupLabel userSelect="none">
{title}
</ChakraMenu.ItemGroupLabel>
)}
{children}
</ChakraMenu.ItemGroup>
)
})
export interface MenuTriggerItemProps extends ChakraMenu.ItemProps {
startIcon?: React.ReactNode
}
export const MenuTriggerItem = React.forwardRef<
HTMLDivElement,
MenuTriggerItemProps
>(function MenuTriggerItem(props, ref) {
const { startIcon, children, ...rest } = props
return (
<ChakraMenu.TriggerItem ref={ref} {...rest}>
{startIcon}
{children}
<LuChevronRight />
</ChakraMenu.TriggerItem>
)
})
export const MenuRadioItemGroup = ChakraMenu.RadioItemGroup
export const MenuContextTrigger = ChakraMenu.ContextTrigger
export const MenuRoot = ChakraMenu.Root
export const MenuSeparator = ChakraMenu.Separator
export const MenuItem = ChakraMenu.Item
export const MenuItemText = ChakraMenu.ItemText
export const MenuItemCommand = ChakraMenu.ItemCommand
export const MenuTrigger = ChakraMenu.Trigger
+211
View File
@@ -0,0 +1,211 @@
"use client"
import type { ButtonProps, TextProps } from "@chakra-ui/react"
import {
Button,
Pagination as ChakraPagination,
IconButton,
Text,
createContext,
usePaginationContext,
} from "@chakra-ui/react"
import * as React from "react"
import {
HiChevronLeft,
HiChevronRight,
HiMiniEllipsisHorizontal,
} from "react-icons/hi2"
import { LinkButton } from "./link-button"
interface ButtonVariantMap {
current: ButtonProps["variant"]
default: ButtonProps["variant"]
ellipsis: ButtonProps["variant"]
}
type PaginationVariant = "outline" | "solid" | "subtle"
interface ButtonVariantContext {
size: ButtonProps["size"]
variantMap: ButtonVariantMap
getHref?: (page: number) => string
}
const [RootPropsProvider, useRootProps] = createContext<ButtonVariantContext>({
name: "RootPropsProvider",
})
export interface PaginationRootProps
extends Omit<ChakraPagination.RootProps, "type"> {
size?: ButtonProps["size"]
variant?: PaginationVariant
getHref?: (page: number) => string
}
const variantMap: Record<PaginationVariant, ButtonVariantMap> = {
outline: { default: "ghost", ellipsis: "plain", current: "outline" },
solid: { default: "outline", ellipsis: "outline", current: "solid" },
subtle: { default: "ghost", ellipsis: "plain", current: "subtle" },
}
export const PaginationRoot = React.forwardRef<
HTMLDivElement,
PaginationRootProps
>(function PaginationRoot(props, ref) {
const { size = "sm", variant = "outline", getHref, ...rest } = props
return (
<RootPropsProvider
value={{ size, variantMap: variantMap[variant], getHref }}
>
<ChakraPagination.Root
ref={ref}
type={getHref ? "link" : "button"}
{...rest}
/>
</RootPropsProvider>
)
})
export const PaginationEllipsis = React.forwardRef<
HTMLDivElement,
ChakraPagination.EllipsisProps
>(function PaginationEllipsis(props, ref) {
const { size, variantMap } = useRootProps()
return (
<ChakraPagination.Ellipsis ref={ref} {...props} asChild>
<Button as="span" variant={variantMap.ellipsis} size={size}>
<HiMiniEllipsisHorizontal />
</Button>
</ChakraPagination.Ellipsis>
)
})
export const PaginationItem = React.forwardRef<
HTMLButtonElement,
ChakraPagination.ItemProps
>(function PaginationItem(props, ref) {
const { page } = usePaginationContext()
const { size, variantMap, getHref } = useRootProps()
const current = page === props.value
const variant = current ? variantMap.current : variantMap.default
if (getHref) {
return (
<LinkButton href={getHref(props.value)} variant={variant} size={size}>
{props.value}
</LinkButton>
)
}
return (
<ChakraPagination.Item ref={ref} {...props} asChild>
<Button variant={variant} size={size}>
{props.value}
</Button>
</ChakraPagination.Item>
)
})
export const PaginationPrevTrigger = React.forwardRef<
HTMLButtonElement,
ChakraPagination.PrevTriggerProps
>(function PaginationPrevTrigger(props, ref) {
const { size, variantMap, getHref } = useRootProps()
const { previousPage } = usePaginationContext()
if (getHref) {
return (
<LinkButton
href={previousPage != null ? getHref(previousPage) : undefined}
variant={variantMap.default}
size={size}
>
<HiChevronLeft />
</LinkButton>
)
}
return (
<ChakraPagination.PrevTrigger ref={ref} asChild {...props}>
<IconButton variant={variantMap.default} size={size}>
<HiChevronLeft />
</IconButton>
</ChakraPagination.PrevTrigger>
)
})
export const PaginationNextTrigger = React.forwardRef<
HTMLButtonElement,
ChakraPagination.NextTriggerProps
>(function PaginationNextTrigger(props, ref) {
const { size, variantMap, getHref } = useRootProps()
const { nextPage } = usePaginationContext()
if (getHref) {
return (
<LinkButton
href={nextPage != null ? getHref(nextPage) : undefined}
variant={variantMap.default}
size={size}
>
<HiChevronRight />
</LinkButton>
)
}
return (
<ChakraPagination.NextTrigger ref={ref} asChild {...props}>
<IconButton variant={variantMap.default} size={size}>
<HiChevronRight />
</IconButton>
</ChakraPagination.NextTrigger>
)
})
export const PaginationItems = (props: React.HTMLAttributes<HTMLElement>) => {
return (
<ChakraPagination.Context>
{({ pages }) =>
pages.map((page, index) => {
return page.type === "ellipsis" ? (
<PaginationEllipsis key={index} index={index} {...props} />
) : (
<PaginationItem
key={index}
type="page"
value={page.value}
{...props}
/>
)
})
}
</ChakraPagination.Context>
)
}
interface PageTextProps extends TextProps {
format?: "short" | "compact" | "long"
}
export const PaginationPageText = React.forwardRef<
HTMLParagraphElement,
PageTextProps
>(function PaginationPageText(props, ref) {
const { format = "compact", ...rest } = props
const { page, totalPages, pageRange, count } = usePaginationContext()
const content = React.useMemo(() => {
if (format === "short") return `${page} / ${totalPages}`
if (format === "compact") return `${page} of ${totalPages}`
return `${pageRange.start + 1} - ${Math.min(
pageRange.end,
count,
)} of ${count}`
}, [format, page, totalPages, pageRange, count])
return (
<Text fontWeight="medium" ref={ref} {...rest}>
{content}
</Text>
)
})
@@ -0,0 +1,162 @@
"use client"
import type {
ButtonProps,
GroupProps,
InputProps,
StackProps,
} from "@chakra-ui/react"
import {
Box,
HStack,
IconButton,
Input,
Stack,
mergeRefs,
useControllableState,
} from "@chakra-ui/react"
import { forwardRef, useRef } from "react"
import { FiEye, FiEyeOff } from "react-icons/fi"
import { Field } from "./field"
import { InputGroup } from "./input-group"
export interface PasswordVisibilityProps {
defaultVisible?: boolean
visible?: boolean
onVisibleChange?: (visible: boolean) => void
visibilityIcon?: { on: React.ReactNode; off: React.ReactNode }
}
export interface PasswordInputProps
extends InputProps,
PasswordVisibilityProps {
rootProps?: GroupProps
startElement?: React.ReactNode
type: string
errors: any
}
export const PasswordInput = forwardRef<HTMLInputElement, PasswordInputProps>(
function PasswordInput(props, ref) {
const {
rootProps,
defaultVisible,
visible: visibleProp,
onVisibleChange,
visibilityIcon = { on: <FiEye />, off: <FiEyeOff /> },
startElement,
type,
errors,
...rest
} = props
const [visible, setVisible] = useControllableState({
value: visibleProp,
defaultValue: defaultVisible || false,
onChange: onVisibleChange,
})
const inputRef = useRef<HTMLInputElement>(null)
return (
<Field
invalid={!!errors[type]}
errorText={errors[type]?.message}
alignSelf="start"
>
<InputGroup
width="100%"
startElement={startElement}
endElement={
<VisibilityTrigger
disabled={rest.disabled}
onPointerDown={(e) => {
if (rest.disabled) return
if (e.button !== 0) return
e.preventDefault()
setVisible(!visible)
}}
>
{visible ? visibilityIcon.off : visibilityIcon.on}
</VisibilityTrigger>
}
{...rootProps}
>
<Input
{...rest}
ref={mergeRefs(ref, inputRef)}
type={visible ? "text" : "password"}
/>
</InputGroup>
</Field>
)
},
)
const VisibilityTrigger = forwardRef<HTMLButtonElement, ButtonProps>(
function VisibilityTrigger(props, ref) {
return (
<IconButton
tabIndex={-1}
ref={ref}
me="-2"
aspectRatio="square"
size="sm"
variant="ghost"
height="calc(100% - {spacing.2})"
aria-label="Toggle password visibility"
color="inherit"
{...props}
/>
)
},
)
interface PasswordStrengthMeterProps extends StackProps {
max?: number
value: number
}
export const PasswordStrengthMeter = forwardRef<
HTMLDivElement,
PasswordStrengthMeterProps
>(function PasswordStrengthMeter(props, ref) {
const { max = 4, value, ...rest } = props
const percent = (value / max) * 100
const { label, colorPalette } = getColorPalette(percent)
return (
<Stack align="flex-end" gap="1" ref={ref} {...rest}>
<HStack width="full" ref={ref} {...rest}>
{Array.from({ length: max }).map((_, index) => (
<Box
key={index}
height="1"
flex="1"
rounded="sm"
data-selected={index < value ? "" : undefined}
layerStyle="fill.subtle"
colorPalette="gray"
_selected={{
colorPalette,
layerStyle: "fill.solid",
}}
/>
))}
</HStack>
{label && <HStack textStyle="xs">{label}</HStack>}
</Stack>
)
})
function getColorPalette(percent: number) {
switch (true) {
case percent < 33:
return { label: "Low", colorPalette: "red" }
case percent < 66:
return { label: "Medium", colorPalette: "orange" }
default:
return { label: "High", colorPalette: "green" }
}
}
+18
View File
@@ -0,0 +1,18 @@
"use client"
import { ChakraProvider } from "@chakra-ui/react"
import React, { type PropsWithChildren } from "react"
import { system } from "../../theme"
import { ColorModeProvider } from "./color-mode"
import { Toaster } from "./toaster"
export function CustomProvider(props: PropsWithChildren) {
return (
<ChakraProvider value={system}>
<ColorModeProvider defaultTheme="light">
{props.children}
</ColorModeProvider>
<Toaster />
</ChakraProvider>
)
}
+24
View File
@@ -0,0 +1,24 @@
import { RadioGroup as ChakraRadioGroup } from "@chakra-ui/react"
import * as React from "react"
export interface RadioProps extends ChakraRadioGroup.ItemProps {
rootRef?: React.Ref<HTMLDivElement>
inputProps?: React.InputHTMLAttributes<HTMLInputElement>
}
export const Radio = React.forwardRef<HTMLInputElement, RadioProps>(
function Radio(props, ref) {
const { children, inputProps, rootRef, ...rest } = props
return (
<ChakraRadioGroup.Item ref={rootRef} {...rest}>
<ChakraRadioGroup.ItemHiddenInput ref={ref} {...inputProps} />
<ChakraRadioGroup.ItemIndicator />
{children && (
<ChakraRadioGroup.ItemText>{children}</ChakraRadioGroup.ItemText>
)}
</ChakraRadioGroup.Item>
)
},
)
export const RadioGroup = ChakraRadioGroup.Root
+47
View File
@@ -0,0 +1,47 @@
import type {
SkeletonProps as ChakraSkeletonProps,
CircleProps,
} from "@chakra-ui/react"
import { Skeleton as ChakraSkeleton, Circle, Stack } from "@chakra-ui/react"
import * as React from "react"
export interface SkeletonCircleProps extends ChakraSkeletonProps {
size?: CircleProps["size"]
}
export const SkeletonCircle = React.forwardRef<
HTMLDivElement,
SkeletonCircleProps
>(function SkeletonCircle(props, ref) {
const { size, ...rest } = props
return (
<Circle size={size} asChild ref={ref}>
<ChakraSkeleton {...rest} />
</Circle>
)
})
export interface SkeletonTextProps extends ChakraSkeletonProps {
noOfLines?: number
}
export const SkeletonText = React.forwardRef<HTMLDivElement, SkeletonTextProps>(
function SkeletonText(props, ref) {
const { noOfLines = 3, gap, ...rest } = props
return (
<Stack gap={gap} width="full" ref={ref}>
{Array.from({ length: noOfLines }).map((_, index) => (
<ChakraSkeleton
height="4"
key={index}
{...props}
_last={{ maxW: "80%" }}
{...rest}
/>
))}
</Stack>
)
},
)
export const Skeleton = ChakraSkeleton
+43
View File
@@ -0,0 +1,43 @@
"use client"
import {
Toaster as ChakraToaster,
Portal,
Spinner,
Stack,
Toast,
createToaster,
} from "@chakra-ui/react"
export const toaster = createToaster({
placement: "top-end",
pauseOnPageIdle: true,
})
export const Toaster = () => {
return (
<Portal>
<ChakraToaster toaster={toaster} insetInline={{ mdDown: "4" }}>
{(toast) => (
<Toast.Root width={{ md: "sm" }} color={toast.meta?.color}>
{toast.type === "loading" ? (
<Spinner size="sm" color="blue.solid" />
) : (
<Toast.Indicator />
)}
<Stack gap="1" flex="1" maxWidth="100%">
{toast.title && <Toast.Title>{toast.title}</Toast.Title>}
{toast.description && (
<Toast.Description>{toast.description}</Toast.Description>
)}
</Stack>
{toast.action && (
<Toast.ActionTrigger>{toast.action.label}</Toast.ActionTrigger>
)}
{toast.meta?.closable && <Toast.CloseTrigger />}
</Toast.Root>
)}
</ChakraToaster>
</Portal>
)
}
+5 -29
View File
@@ -2,7 +2,6 @@ import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"
import { useNavigate } from "@tanstack/react-router" import { useNavigate } from "@tanstack/react-router"
import { useState } from "react" import { useState } from "react"
import { AxiosError } from "axios"
import { import {
type Body_login_login_access_token as AccessToken, type Body_login_login_access_token as AccessToken,
type ApiError, type ApiError,
@@ -10,8 +9,8 @@ import {
type UserPublic, type UserPublic,
type UserRegister, type UserRegister,
UsersService, UsersService,
} from "../client" } from "@/client"
import useCustomToast from "./useCustomToast" import { handleError } from "@/utils"
const isLoggedIn = () => { const isLoggedIn = () => {
return localStorage.getItem("access_token") !== null return localStorage.getItem("access_token") !== null
@@ -20,9 +19,8 @@ const isLoggedIn = () => {
const useAuth = () => { const useAuth = () => {
const [error, setError] = useState<string | null>(null) const [error, setError] = useState<string | null>(null)
const navigate = useNavigate() const navigate = useNavigate()
const showToast = useCustomToast()
const queryClient = useQueryClient() const queryClient = useQueryClient()
const { data: user, isLoading } = useQuery<UserPublic | null, Error>({ const { data: user } = useQuery<UserPublic | null, Error>({
queryKey: ["currentUser"], queryKey: ["currentUser"],
queryFn: UsersService.readUserMe, queryFn: UsersService.readUserMe,
enabled: isLoggedIn(), enabled: isLoggedIn(),
@@ -34,20 +32,9 @@ const useAuth = () => {
onSuccess: () => { onSuccess: () => {
navigate({ to: "/login" }) navigate({ to: "/login" })
showToast(
"Account created.",
"Your account has been created successfully.",
"success",
)
}, },
onError: (err: ApiError) => { onError: (err: ApiError) => {
let errDetail = (err.body as any)?.detail handleError(err)
if (err instanceof AxiosError) {
errDetail = err.message
}
showToast("Something went wrong.", errDetail, "error")
}, },
onSettled: () => { onSettled: () => {
queryClient.invalidateQueries({ queryKey: ["users"] }) queryClient.invalidateQueries({ queryKey: ["users"] })
@@ -67,17 +54,7 @@ const useAuth = () => {
navigate({ to: "/" }) navigate({ to: "/" })
}, },
onError: (err: ApiError) => { onError: (err: ApiError) => {
let errDetail = (err.body as any)?.detail handleError(err)
if (err instanceof AxiosError) {
errDetail = err.message
}
if (Array.isArray(errDetail)) {
errDetail = "Something went wrong"
}
setError(errDetail)
}, },
}) })
@@ -91,7 +68,6 @@ const useAuth = () => {
loginMutation, loginMutation,
logout, logout,
user, user,
isLoading,
error, error,
resetError: () => setError(null), resetError: () => setError(null),
} }
+17 -15
View File
@@ -1,23 +1,25 @@
import { useToast } from "@chakra-ui/react" "use client"
import { useCallback } from "react"
import { toaster } from "@/components/ui/toaster"
const useCustomToast = () => { const useCustomToast = () => {
const toast = useToast() const showSuccessToast = (description: string) => {
toaster.create({
const showToast = useCallback( title: "Success!",
(title: string, description: string, status: "success" | "error") => {
toast({
title,
description, description,
status, type: "success",
isClosable: true,
position: "bottom-right",
}) })
}, }
[toast],
)
return showToast const showErrorToast = (description: string) => {
toaster.create({
title: "Something went wrong!",
description,
type: "error",
})
}
return { showSuccessToast, showErrorToast }
} }
export default useCustomToast export default useCustomToast
+20 -8
View File
@@ -1,19 +1,31 @@
import { ChakraProvider } from "@chakra-ui/react" import { MutationCache, QueryCache, QueryClient, QueryClientProvider } from "@tanstack/react-query"
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"
import { RouterProvider, createRouter } from "@tanstack/react-router" import { RouterProvider, createRouter } from "@tanstack/react-router"
import React, { StrictMode } from "react"
import ReactDOM from "react-dom/client" import ReactDOM from "react-dom/client"
import { routeTree } from "./routeTree.gen" import { routeTree } from "./routeTree.gen"
import { StrictMode } from "react" import { ApiError, OpenAPI } from "./client"
import { OpenAPI } from "./client" import { CustomProvider } from "./components/ui/provider"
import theme from "./theme"
OpenAPI.BASE = import.meta.env.VITE_API_URL OpenAPI.BASE = import.meta.env.VITE_API_URL
OpenAPI.TOKEN = async () => { OpenAPI.TOKEN = async () => {
return localStorage.getItem("access_token") || "" return localStorage.getItem("access_token") || ""
} }
const queryClient = new QueryClient() const handleApiError = (error: Error) => {
if (error instanceof ApiError && [401, 403].includes(error.status)) {
localStorage.removeItem("access_token")
window.location.href = "/login"
}
}
const queryClient = new QueryClient({
queryCache: new QueryCache({
onError: handleApiError,
}),
mutationCache: new MutationCache({
onError: handleApiError,
}),
})
const router = createRouter({ routeTree }) const router = createRouter({ routeTree })
declare module "@tanstack/react-router" { declare module "@tanstack/react-router" {
@@ -24,10 +36,10 @@ declare module "@tanstack/react-router" {
ReactDOM.createRoot(document.getElementById("root")!).render( ReactDOM.createRoot(document.getElementById("root")!).render(
<StrictMode> <StrictMode>
<ChakraProvider theme={theme}> <CustomProvider>
<QueryClientProvider client={queryClient}> <QueryClientProvider client={queryClient}>
<RouterProvider router={router} /> <RouterProvider router={router} />
</QueryClientProvider> </QueryClientProvider>
</ChakraProvider> </CustomProvider>
</StrictMode>, </StrictMode>,
) )
+1 -1
View File
@@ -1,7 +1,7 @@
import { Outlet, createRootRoute } from "@tanstack/react-router" import { Outlet, createRootRoute } from "@tanstack/react-router"
import React, { Suspense } from "react" import React, { Suspense } from "react"
import NotFound from "../components/Common/NotFound" import NotFound from "@/components/Common/NotFound"
const loadDevtools = () => const loadDevtools = () =>
Promise.all([ Promise.all([
+12 -14
View File
@@ -1,9 +1,9 @@
import { Flex, Spinner } from "@chakra-ui/react" import { Flex } from "@chakra-ui/react"
import { Outlet, createFileRoute, redirect } from "@tanstack/react-router" import { Outlet, createFileRoute, redirect } from "@tanstack/react-router"
import Sidebar from "../components/Common/Sidebar" import Navbar from "@/components/Common/Navbar"
import UserMenu from "../components/Common/UserMenu" import Sidebar from "@/components/Common/Sidebar"
import useAuth, { isLoggedIn } from "../hooks/useAuth" import { isLoggedIn } from "@/hooks/useAuth"
export const Route = createFileRoute("/_layout")({ export const Route = createFileRoute("/_layout")({
component: Layout, component: Layout,
@@ -17,19 +17,17 @@ export const Route = createFileRoute("/_layout")({
}) })
function Layout() { function Layout() {
const { isLoading } = useAuth()
return ( return (
<Flex maxW="large" h="auto" position="relative"> <Flex direction="column" h="100vh">
<Navbar />
<Flex flex="1" overflow="hidden">
<Sidebar /> <Sidebar />
{isLoading ? ( <Flex flex="1" direction="column" p={4} overflowY="auto">
<Flex justify="center" align="center" height="100vh" width="full">
<Spinner size="xl" color="ui.main" />
</Flex>
) : (
<Outlet /> <Outlet />
)} </Flex>
<UserMenu /> </Flex>
</Flex> </Flex>
) )
} }
export default Layout
+69 -112
View File
@@ -1,38 +1,23 @@
import { import { Badge, Container, Flex, Heading, Table } from "@chakra-ui/react"
Badge,
Box,
Button,
Container,
Flex,
Heading,
SkeletonText,
Table,
TableContainer,
Tbody,
Td,
Th,
Thead,
Tr,
} from "@chakra-ui/react"
import { useQuery, useQueryClient } from "@tanstack/react-query" import { useQuery, useQueryClient } from "@tanstack/react-query"
import { createFileRoute, useNavigate } from "@tanstack/react-router" import { createFileRoute, useNavigate } from "@tanstack/react-router"
import { useEffect } from "react"
import { z } from "zod" import { z } from "zod"
import { type UserPublic, UsersService } from "../../client" import { type UserPublic, UsersService } from "@/client"
import AddUser from "../../components/Admin/AddUser" import AddUser from "@/components/Admin/AddUser"
import ActionsMenu from "../../components/Common/ActionsMenu" import { UserActionsMenu } from "@/components/Common/UserActionsMenu"
import Navbar from "../../components/Common/Navbar" import PendingUsers from "@/components/Pending/PendingUsers"
import {
PaginationItems,
PaginationNextTrigger,
PaginationPrevTrigger,
PaginationRoot,
} from "@/components/ui/pagination.tsx"
const usersSearchSchema = z.object({ const usersSearchSchema = z.object({
page: z.number().catch(1), page: z.number().catch(1),
}) })
export const Route = createFileRoute("/_layout/admin")({
component: Admin,
validateSearch: (search) => usersSearchSchema.parse(search),
})
const PER_PAGE = 5 const PER_PAGE = 5
function getUsersQueryOptions({ page }: { page: number }) { function getUsersQueryOptions({ page }: { page: number }) {
@@ -43,114 +28,86 @@ function getUsersQueryOptions({ page }: { page: number }) {
} }
} }
export const Route = createFileRoute("/_layout/admin")({
component: Admin,
validateSearch: (search) => usersSearchSchema.parse(search),
})
function UsersTable() { function UsersTable() {
const queryClient = useQueryClient() const queryClient = useQueryClient()
const currentUser = queryClient.getQueryData<UserPublic>(["currentUser"]) const currentUser = queryClient.getQueryData<UserPublic>(["currentUser"])
const { page } = Route.useSearch()
const navigate = useNavigate({ from: Route.fullPath }) const navigate = useNavigate({ from: Route.fullPath })
const setPage = (page: number) => const { page } = Route.useSearch()
navigate({ search: (prev) => ({ ...prev, page }) })
const { const { data, isLoading, isPlaceholderData } = useQuery({
data: users,
isPending,
isPlaceholderData,
} = useQuery({
...getUsersQueryOptions({ page }), ...getUsersQueryOptions({ page }),
placeholderData: (prevData) => prevData, placeholderData: (prevData) => prevData,
}) })
const hasNextPage = !isPlaceholderData && users?.data.length === PER_PAGE const setPage = (page: number) =>
const hasPreviousPage = page > 1 navigate({
search: (prev: { [key: string]: string }) => ({ ...prev, page }),
})
useEffect(() => { const users = data?.data.slice(0, PER_PAGE) ?? []
if (hasNextPage) { const count = data?.count ?? 0
queryClient.prefetchQuery(getUsersQueryOptions({ page: page + 1 }))
if (isLoading) {
return <PendingUsers />
} }
}, [page, queryClient, hasNextPage])
return ( return (
<> <>
<TableContainer> <Table.Root size={{ base: "sm", md: "md" }}>
<Table size={{ base: "sm", md: "md" }}> <Table.Header>
<Thead> <Table.Row>
<Tr> <Table.ColumnHeader w="20%">Full name</Table.ColumnHeader>
<Th width="20%">Full name</Th> <Table.ColumnHeader w="25%">Email</Table.ColumnHeader>
<Th width="50%">Email</Th> <Table.ColumnHeader w="15%">Role</Table.ColumnHeader>
<Th width="10%">Role</Th> <Table.ColumnHeader w="20%">Status</Table.ColumnHeader>
<Th width="10%">Status</Th> <Table.ColumnHeader w="20%">Actions</Table.ColumnHeader>
<Th width="10%">Actions</Th> </Table.Row>
</Tr> </Table.Header>
</Thead> <Table.Body>
{isPending ? ( {users?.map((user) => (
<Tbody> <Table.Row key={user.id} opacity={isPlaceholderData ? 0.5 : 1}>
<Tr> <Table.Cell w="20%" color={!user.full_name ? "gray" : "inherit"}>
{new Array(4).fill(null).map((_, index) => (
<Td key={index}>
<SkeletonText noOfLines={1} paddingBlock="16px" />
</Td>
))}
</Tr>
</Tbody>
) : (
<Tbody>
{users?.data.map((user) => (
<Tr key={user.id}>
<Td
color={!user.full_name ? "ui.dim" : "inherit"}
isTruncated
maxWidth="150px"
>
{user.full_name || "N/A"} {user.full_name || "N/A"}
{currentUser?.id === user.id && ( {currentUser?.id === user.id && (
<Badge ml="1" colorScheme="teal"> <Badge ml="1" colorScheme="teal">
You You
</Badge> </Badge>
)} )}
</Td> </Table.Cell>
<Td isTruncated maxWidth="150px"> <Table.Cell w="25%">{user.email}</Table.Cell>
{user.email} <Table.Cell w="15%">
</Td> {user.is_superuser ? "Superuser" : "User"}
<Td>{user.is_superuser ? "Superuser" : "User"}</Td> </Table.Cell>
<Td> <Table.Cell w="20%">
<Flex gap={2}>
<Box
w="2"
h="2"
borderRadius="50%"
bg={user.is_active ? "ui.success" : "ui.danger"}
alignSelf="center"
/>
{user.is_active ? "Active" : "Inactive"} {user.is_active ? "Active" : "Inactive"}
</Flex> </Table.Cell>
</Td> <Table.Cell w="20%">
<Td> <UserActionsMenu
<ActionsMenu user={user}
type="User" disabled={currentUser?.id === user.id}
value={user}
disabled={currentUser?.id === user.id ? true : false}
/> />
</Td> </Table.Cell>
</Tr> </Table.Row>
))} ))}
</Tbody> </Table.Body>
)} </Table.Root>
</Table> <Flex justifyContent="flex-end" mt={4}>
</TableContainer> <PaginationRoot
<Flex count={count}
gap={4} pageSize={PER_PAGE}
alignItems="center" onPageChange={({ page }) => setPage(page)}
mt={4}
direction="row"
justifyContent="flex-end"
> >
<Button onClick={() => setPage(page - 1)} isDisabled={!hasPreviousPage}> <Flex>
Previous <PaginationPrevTrigger />
</Button> <PaginationItems />
<span>Page {page}</span> <PaginationNextTrigger />
<Button isDisabled={!hasNextPage} onClick={() => setPage(page + 1)}> </Flex>
Next </PaginationRoot>
</Button>
</Flex> </Flex>
</> </>
) )
@@ -159,11 +116,11 @@ function UsersTable() {
function Admin() { function Admin() {
return ( return (
<Container maxW="full"> <Container maxW="full">
<Heading size="lg" textAlign={{ base: "center", md: "left" }} pt={12}> <Heading size="lg" pt={12}>
Users Management Users Management
</Heading> </Heading>
<Navbar type={"User"} addModalAs={AddUser} /> <AddUser />
<UsersTable /> <UsersTable />
</Container> </Container>
) )
+1 -1
View File
@@ -1,7 +1,7 @@
import { Box, Container, Text } from "@chakra-ui/react" import { Box, Container, Text } from "@chakra-ui/react"
import { createFileRoute } from "@tanstack/react-router" import { createFileRoute } from "@tanstack/react-router"
import useAuth from "../../hooks/useAuth" import useAuth from "@/hooks/useAuth"
export const Route = createFileRoute("/_layout/")({ export const Route = createFileRoute("/_layout/")({
component: Dashboard, component: Dashboard,
+91 -92
View File
@@ -1,36 +1,31 @@
import { import {
Button,
Container, Container,
EmptyState,
Flex, Flex,
Heading, Heading,
SkeletonText,
Table, Table,
TableContainer, VStack,
Tbody,
Td,
Th,
Thead,
Tr,
} from "@chakra-ui/react" } from "@chakra-ui/react"
import { useQuery, useQueryClient } from "@tanstack/react-query" import { useQuery } from "@tanstack/react-query"
import { createFileRoute, useNavigate } from "@tanstack/react-router" import { createFileRoute, useNavigate } from "@tanstack/react-router"
import { useEffect } from "react" import { FiSearch } from "react-icons/fi"
import { z } from "zod" import { z } from "zod"
import { ItemsService } from "../../client" import { ItemsService } from "@/client"
import ActionsMenu from "../../components/Common/ActionsMenu" import { ItemActionsMenu } from "@/components/Common/ItemActionsMenu"
import Navbar from "../../components/Common/Navbar" import AddItem from "@/components/Items/AddItem"
import AddItem from "../../components/Items/AddItem" import PendingItems from "@/components/Pending/PendingItems"
import {
PaginationItems,
PaginationNextTrigger,
PaginationPrevTrigger,
PaginationRoot,
} from "@/components/ui/pagination.tsx"
const itemsSearchSchema = z.object({ const itemsSearchSchema = z.object({
page: z.number().catch(1), page: z.number().catch(1),
}) })
export const Route = createFileRoute("/_layout/items")({
component: Items,
validateSearch: (search) => itemsSearchSchema.parse(search),
})
const PER_PAGE = 5 const PER_PAGE = 5
function getItemsQueryOptions({ page }: { page: number }) { function getItemsQueryOptions({ page }: { page: number }) {
@@ -41,91 +36,96 @@ function getItemsQueryOptions({ page }: { page: number }) {
} }
} }
function ItemsTable() { export const Route = createFileRoute("/_layout/items")({
const queryClient = useQueryClient() component: Items,
const { page } = Route.useSearch() validateSearch: (search) => itemsSearchSchema.parse(search),
const navigate = useNavigate({ from: Route.fullPath }) })
const setPage = (page: number) =>
navigate({ search: (prev) => ({ ...prev, page }) })
const { function ItemsTable() {
data: items, const navigate = useNavigate({ from: Route.fullPath })
isPending, const { page } = Route.useSearch()
isPlaceholderData,
} = useQuery({ const { data, isLoading, isPlaceholderData } = useQuery({
...getItemsQueryOptions({ page }), ...getItemsQueryOptions({ page }),
placeholderData: (prevData) => prevData, placeholderData: (prevData) => prevData,
}) })
const hasNextPage = !isPlaceholderData && items?.data.length === PER_PAGE const setPage = (page: number) =>
const hasPreviousPage = page > 1 navigate({
search: (prev: { [key: string]: string }) => ({ ...prev, page }),
})
useEffect(() => { const items = data?.data.slice(0, PER_PAGE) ?? []
if (hasNextPage) { const count = data?.count ?? 0
queryClient.prefetchQuery(getItemsQueryOptions({ page: page + 1 }))
if (isLoading) {
return <PendingItems />
}
if (items.length === 0) {
return (
<EmptyState.Root>
<EmptyState.Content>
<EmptyState.Indicator>
<FiSearch />
</EmptyState.Indicator>
<VStack textAlign="center">
<EmptyState.Title>You don't have any items yet</EmptyState.Title>
<EmptyState.Description>
Add a new item to get started
</EmptyState.Description>
</VStack>
</EmptyState.Content>
</EmptyState.Root>
)
} }
}, [page, queryClient, hasNextPage])
return ( return (
<> <>
<TableContainer> <Table.Root size={{ base: "sm", md: "md" }}>
<Table size={{ base: "sm", md: "md" }}> <Table.Header>
<Thead> <Table.Row>
<Tr> <Table.ColumnHeader w="30%">ID</Table.ColumnHeader>
<Th>ID</Th> <Table.ColumnHeader w="30%">Title</Table.ColumnHeader>
<Th>Title</Th> <Table.ColumnHeader w="30%">Description</Table.ColumnHeader>
<Th>Description</Th> <Table.ColumnHeader w="10%">Actions</Table.ColumnHeader>
<Th>Actions</Th> </Table.Row>
</Tr> </Table.Header>
</Thead> <Table.Body>
{isPending ? ( {items?.map((item) => (
<Tbody> <Table.Row key={item.id} opacity={isPlaceholderData ? 0.5 : 1}>
<Tr> <Table.Cell truncate maxW="30%">
{new Array(4).fill(null).map((_, index) => ( {item.id}
<Td key={index}> </Table.Cell>
<SkeletonText noOfLines={1} paddingBlock="16px" /> <Table.Cell truncate maxW="30%">
</Td>
))}
</Tr>
</Tbody>
) : (
<Tbody>
{items?.data.map((item) => (
<Tr key={item.id} opacity={isPlaceholderData ? 0.5 : 1}>
<Td>{item.id}</Td>
<Td isTruncated maxWidth="150px">
{item.title} {item.title}
</Td> </Table.Cell>
<Td <Table.Cell
color={!item.description ? "ui.dim" : "inherit"} color={!item.description ? "gray" : "inherit"}
isTruncated truncate
maxWidth="150px" maxW="30%"
> >
{item.description || "N/A"} {item.description || "N/A"}
</Td> </Table.Cell>
<Td> <Table.Cell width="10%">
<ActionsMenu type={"Item"} value={item} /> <ItemActionsMenu item={item} />
</Td> </Table.Cell>
</Tr> </Table.Row>
))} ))}
</Tbody> </Table.Body>
)} </Table.Root>
</Table> <Flex justifyContent="flex-end" mt={4}>
</TableContainer> <PaginationRoot
<Flex count={count}
gap={4} pageSize={PER_PAGE}
alignItems="center" onPageChange={({ page }) => setPage(page)}
mt={4}
direction="row"
justifyContent="flex-end"
> >
<Button onClick={() => setPage(page - 1)} isDisabled={!hasPreviousPage}> <Flex>
Previous <PaginationPrevTrigger />
</Button> <PaginationItems />
<span>Page {page}</span> <PaginationNextTrigger />
<Button isDisabled={!hasNextPage} onClick={() => setPage(page + 1)}> </Flex>
Next </PaginationRoot>
</Button>
</Flex> </Flex>
</> </>
) )
@@ -134,11 +134,10 @@ function ItemsTable() {
function Items() { function Items() {
return ( return (
<Container maxW="full"> <Container maxW="full">
<Heading size="lg" textAlign={{ base: "center", md: "left" }} pt={12}> <Heading size="lg" pt={12}>
Items Management Items Management
</Heading> </Heading>
<AddItem />
<Navbar type={"Item"} addModalAs={AddItem} />
<ItemsTable /> <ItemsTable />
</Container> </Container>
) )
+27 -32
View File
@@ -1,26 +1,17 @@
import { import { Container, Heading, Tabs } from "@chakra-ui/react"
Container,
Heading,
Tab,
TabList,
TabPanel,
TabPanels,
Tabs,
} from "@chakra-ui/react"
import { useQueryClient } from "@tanstack/react-query"
import { createFileRoute } from "@tanstack/react-router" import { createFileRoute } from "@tanstack/react-router"
import type { UserPublic } from "../../client" import Appearance from "@/components/UserSettings/Appearance"
import Appearance from "../../components/UserSettings/Appearance" import ChangePassword from "@/components/UserSettings/ChangePassword"
import ChangePassword from "../../components/UserSettings/ChangePassword" import DeleteAccount from "@/components/UserSettings/DeleteAccount"
import DeleteAccount from "../../components/UserSettings/DeleteAccount" import UserInformation from "@/components/UserSettings/UserInformation"
import UserInformation from "../../components/UserSettings/UserInformation" import useAuth from "@/hooks/useAuth"
const tabsConfig = [ const tabsConfig = [
{ title: "My profile", component: UserInformation }, { value: "my-profile", title: "My profile", component: UserInformation },
{ title: "Password", component: ChangePassword }, { value: "password", title: "Password", component: ChangePassword },
{ title: "Appearance", component: Appearance }, { value: "appearance", title: "Appearance", component: Appearance },
{ title: "Danger zone", component: DeleteAccount }, { value: "danger-zone", title: "Danger zone", component: DeleteAccount },
] ]
export const Route = createFileRoute("/_layout/settings")({ export const Route = createFileRoute("/_layout/settings")({
@@ -28,31 +19,35 @@ export const Route = createFileRoute("/_layout/settings")({
}) })
function UserSettings() { function UserSettings() {
const queryClient = useQueryClient() const { user: currentUser } = useAuth()
const currentUser = queryClient.getQueryData<UserPublic>(["currentUser"])
const finalTabs = currentUser?.is_superuser const finalTabs = currentUser?.is_superuser
? tabsConfig.slice(0, 3) ? tabsConfig.slice(0, 3)
: tabsConfig : tabsConfig
if (!currentUser) {
return null
}
return ( return (
<Container maxW="full"> <Container maxW="full">
<Heading size="lg" textAlign={{ base: "center", md: "left" }} py={12}> <Heading size="lg" textAlign={{ base: "center", md: "left" }} py={12}>
User Settings User Settings
</Heading> </Heading>
<Tabs variant="enclosed">
<TabList> <Tabs.Root defaultValue="my-profile" variant="subtle">
{finalTabs.map((tab, index) => ( <Tabs.List>
<Tab key={index}>{tab.title}</Tab> {finalTabs.map((tab) => (
<Tabs.Trigger key={tab.value} value={tab.value}>
{tab.title}
</Tabs.Trigger>
))} ))}
</TabList> </Tabs.List>
<TabPanels> {finalTabs.map((tab) => (
{finalTabs.map((tab, index) => ( <Tabs.Content key={tab.value} value={tab.value}>
<TabPanel key={index}>
<tab.component /> <tab.component />
</TabPanel> </Tabs.Content>
))} ))}
</TabPanels> </Tabs.Root>
</Tabs>
</Container> </Container>
) )
} }
+29 -58
View File
@@ -1,29 +1,20 @@
import { ViewIcon, ViewOffIcon } from "@chakra-ui/icons" import { Container, Image, Input, Text } from "@chakra-ui/react"
import {
Button,
Container,
FormControl,
FormErrorMessage,
Icon,
Image,
Input,
InputGroup,
InputRightElement,
Link,
Text,
useBoolean,
} from "@chakra-ui/react"
import { import {
Link as RouterLink, Link as RouterLink,
createFileRoute, createFileRoute,
redirect, redirect,
} from "@tanstack/react-router" } from "@tanstack/react-router"
import { type SubmitHandler, useForm } from "react-hook-form" import { type SubmitHandler, useForm } from "react-hook-form"
import { FiLock, FiMail } from "react-icons/fi"
import type { Body_login_login_access_token as AccessToken } from "@/client"
import { Button } from "@/components/ui/button"
import { Field } from "@/components/ui/field"
import { InputGroup } from "@/components/ui/input-group"
import { PasswordInput } from "@/components/ui/password-input"
import useAuth, { isLoggedIn } from "@/hooks/useAuth"
import Logo from "/assets/images/fastapi-logo.svg" import Logo from "/assets/images/fastapi-logo.svg"
import type { Body_login_login_access_token as AccessToken } from "../client" import { emailPattern, passwordRules } from "../utils"
import useAuth, { isLoggedIn } from "../hooks/useAuth"
import { emailPattern } from "../utils"
export const Route = createFileRoute("/login")({ export const Route = createFileRoute("/login")({
component: Login, component: Login,
@@ -37,7 +28,6 @@ export const Route = createFileRoute("/login")({
}) })
function Login() { function Login() {
const [show, setShow] = useBoolean()
const { loginMutation, error, resetError } = useAuth() const { loginMutation, error, resetError } = useAuth()
const { const {
register, register,
@@ -84,7 +74,11 @@ function Login() {
alignSelf="center" alignSelf="center"
mb={4} mb={4}
/> />
<FormControl id="username" isInvalid={!!errors.username || !!error}> <Field
invalid={!!errors.username}
errorText={errors.username?.message || !!error}
>
<InputGroup w="100%" startElement={<FiMail />}>
<Input <Input
id="username" id="username"
{...register("username", { {...register("username", {
@@ -93,50 +87,27 @@ function Login() {
})} })}
placeholder="Email" placeholder="Email"
type="email" type="email"
required
/> />
{errors.username && (
<FormErrorMessage>{errors.username.message}</FormErrorMessage>
)}
</FormControl>
<FormControl id="password" isInvalid={!!error}>
<InputGroup>
<Input
{...register("password", {
required: "Password is required",
})}
type={show ? "text" : "password"}
placeholder="Password"
required
/>
<InputRightElement
color="ui.dim"
_hover={{
cursor: "pointer",
}}
>
<Icon
as={show ? ViewOffIcon : ViewIcon}
onClick={setShow.toggle}
aria-label={show ? "Hide password" : "Show password"}
>
{show ? <ViewOffIcon /> : <ViewIcon />}
</Icon>
</InputRightElement>
</InputGroup> </InputGroup>
{error && <FormErrorMessage>{error}</FormErrorMessage>} </Field>
</FormControl> <PasswordInput
<Link as={RouterLink} to="/recover-password" color="blue.500"> type="password"
Forgot password? startElement={<FiLock />}
</Link> {...register("password", passwordRules())}
<Button variant="primary" type="submit" isLoading={isSubmitting}> placeholder="Password"
errors={errors}
/>
<RouterLink to="/recover-password" className="main-link">
Forgot Password?
</RouterLink>
<Button variant="solid" type="submit" loading={isSubmitting} size="md">
Log In Log In
</Button> </Button>
<Text> <Text>
Don't have an account?{" "} Don't have an account?{" "}
<Link as={RouterLink} to="/signup" color="blue.500"> <RouterLink to="/signup" className="main-link">
Sign up Sign Up
</Link> </RouterLink>
</Text> </Text>
</Container> </Container>
</> </>
+18 -27
View File
@@ -1,20 +1,16 @@
import { import { Container, Heading, Input, Text } from "@chakra-ui/react"
Button,
Container,
FormControl,
FormErrorMessage,
Heading,
Input,
Text,
} from "@chakra-ui/react"
import { useMutation } from "@tanstack/react-query" import { useMutation } from "@tanstack/react-query"
import { createFileRoute, redirect } from "@tanstack/react-router" import { createFileRoute, redirect } from "@tanstack/react-router"
import { type SubmitHandler, useForm } from "react-hook-form" import { type SubmitHandler, useForm } from "react-hook-form"
import { FiMail } from "react-icons/fi"
import { type ApiError, LoginService } from "../client" import { type ApiError, LoginService } from "@/client"
import { isLoggedIn } from "../hooks/useAuth" import { Button } from "@/components/ui/button"
import useCustomToast from "../hooks/useCustomToast" import { Field } from "@/components/ui/field"
import { emailPattern, handleError } from "../utils" import { InputGroup } from "@/components/ui/input-group"
import { isLoggedIn } from "@/hooks/useAuth"
import useCustomToast from "@/hooks/useCustomToast"
import { emailPattern, handleError } from "@/utils"
interface FormData { interface FormData {
email: string email: string
@@ -38,7 +34,7 @@ function RecoverPassword() {
reset, reset,
formState: { errors, isSubmitting }, formState: { errors, isSubmitting },
} = useForm<FormData>() } = useForm<FormData>()
const showToast = useCustomToast() const { showSuccessToast } = useCustomToast()
const recoverPassword = async (data: FormData) => { const recoverPassword = async (data: FormData) => {
await LoginService.recoverPassword({ await LoginService.recoverPassword({
@@ -49,15 +45,11 @@ function RecoverPassword() {
const mutation = useMutation({ const mutation = useMutation({
mutationFn: recoverPassword, mutationFn: recoverPassword,
onSuccess: () => { onSuccess: () => {
showToast( showSuccessToast("Password recovery email sent successfully.")
"Email sent.",
"We sent an email with a link to get back into your account.",
"success",
)
reset() reset()
}, },
onError: (err: ApiError) => { onError: (err: ApiError) => {
handleError(err, showToast) handleError(err)
}, },
}) })
@@ -79,10 +71,11 @@ function RecoverPassword() {
<Heading size="xl" color="ui.main" textAlign="center" mb={2}> <Heading size="xl" color="ui.main" textAlign="center" mb={2}>
Password Recovery Password Recovery
</Heading> </Heading>
<Text align="center"> <Text textAlign="center">
A password recovery email will be sent to the registered account. A password recovery email will be sent to the registered account.
</Text> </Text>
<FormControl isInvalid={!!errors.email}> <Field invalid={!!errors.email} errorText={errors.email?.message}>
<InputGroup w="100%" startElement={<FiMail />}>
<Input <Input
id="email" id="email"
{...register("email", { {...register("email", {
@@ -92,11 +85,9 @@ function RecoverPassword() {
placeholder="Email" placeholder="Email"
type="email" type="email"
/> />
{errors.email && ( </InputGroup>
<FormErrorMessage>{errors.email.message}</FormErrorMessage> </Field>
)} <Button variant="solid" type="submit" loading={isSubmitting}>
</FormControl>
<Button variant="primary" type="submit" isLoading={isSubmitting}>
Continue Continue
</Button> </Button>
</Container> </Container>
+22 -38
View File
@@ -1,21 +1,15 @@
import { import { Container, Heading, Text } from "@chakra-ui/react"
Button,
Container,
FormControl,
FormErrorMessage,
FormLabel,
Heading,
Input,
Text,
} from "@chakra-ui/react"
import { useMutation } from "@tanstack/react-query" import { useMutation } from "@tanstack/react-query"
import { createFileRoute, redirect, useNavigate } from "@tanstack/react-router" import { createFileRoute, redirect, useNavigate } from "@tanstack/react-router"
import { type SubmitHandler, useForm } from "react-hook-form" import { type SubmitHandler, useForm } from "react-hook-form"
import { FiLock } from "react-icons/fi"
import { type ApiError, LoginService, type NewPassword } from "../client" import { type ApiError, LoginService, type NewPassword } from "@/client"
import { isLoggedIn } from "../hooks/useAuth" import { Button } from "@/components/ui/button"
import useCustomToast from "../hooks/useCustomToast" import { PasswordInput } from "@/components/ui/password-input"
import { confirmPasswordRules, handleError, passwordRules } from "../utils" import { isLoggedIn } from "@/hooks/useAuth"
import useCustomToast from "@/hooks/useCustomToast"
import { confirmPasswordRules, handleError, passwordRules } from "@/utils"
interface NewPasswordForm extends NewPassword { interface NewPasswordForm extends NewPassword {
confirm_password: string confirm_password: string
@@ -46,7 +40,7 @@ function ResetPassword() {
new_password: "", new_password: "",
}, },
}) })
const showToast = useCustomToast() const { showSuccessToast } = useCustomToast()
const navigate = useNavigate() const navigate = useNavigate()
const resetPassword = async (data: NewPassword) => { const resetPassword = async (data: NewPassword) => {
@@ -60,12 +54,12 @@ function ResetPassword() {
const mutation = useMutation({ const mutation = useMutation({
mutationFn: resetPassword, mutationFn: resetPassword,
onSuccess: () => { onSuccess: () => {
showToast("Success!", "Password updated successfully.", "success") showSuccessToast("Password updated successfully.")
reset() reset()
navigate({ to: "/login" }) navigate({ to: "/login" })
}, },
onError: (err: ApiError) => { onError: (err: ApiError) => {
handleError(err, showToast) handleError(err)
}, },
}) })
@@ -90,31 +84,21 @@ function ResetPassword() {
<Text textAlign="center"> <Text textAlign="center">
Please enter your new password and confirm it to reset your password. Please enter your new password and confirm it to reset your password.
</Text> </Text>
<FormControl mt={4} isInvalid={!!errors.new_password}> <PasswordInput
<FormLabel htmlFor="password">Set Password</FormLabel> startElement={<FiLock />}
<Input type="new_password"
id="password" errors={errors}
{...register("new_password", passwordRules())} {...register("new_password", passwordRules())}
placeholder="Password" placeholder="New Password"
type="password"
/> />
{errors.new_password && ( <PasswordInput
<FormErrorMessage>{errors.new_password.message}</FormErrorMessage> startElement={<FiLock />}
)} type="confirm_password"
</FormControl> errors={errors}
<FormControl mt={4} isInvalid={!!errors.confirm_password}>
<FormLabel htmlFor="confirm_password">Confirm Password</FormLabel>
<Input
id="confirm_password"
{...register("confirm_password", confirmPasswordRules(getValues))} {...register("confirm_password", confirmPasswordRules(getValues))}
placeholder="Password" placeholder="Confirm Password"
type="password"
/> />
{errors.confirm_password && ( <Button variant="solid" type="submit">
<FormErrorMessage>{errors.confirm_password.message}</FormErrorMessage>
)}
</FormControl>
<Button variant="primary" type="submit">
Reset Password Reset Password
</Button> </Button>
</Container> </Container>
+36 -64
View File
@@ -1,26 +1,20 @@
import { import { Container, Flex, Image, Input, Text } from "@chakra-ui/react"
Button,
Container,
Flex,
FormControl,
FormErrorMessage,
FormLabel,
Image,
Input,
Link,
Text,
} from "@chakra-ui/react"
import { import {
Link as RouterLink, Link as RouterLink,
createFileRoute, createFileRoute,
redirect, redirect,
} from "@tanstack/react-router" } from "@tanstack/react-router"
import { type SubmitHandler, useForm } from "react-hook-form" import { type SubmitHandler, useForm } from "react-hook-form"
import { FiLock, FiUser } from "react-icons/fi"
import type { UserRegister } from "@/client"
import { Button } from "@/components/ui/button"
import { Field } from "@/components/ui/field"
import { InputGroup } from "@/components/ui/input-group"
import { PasswordInput } from "@/components/ui/password-input"
import useAuth, { isLoggedIn } from "@/hooks/useAuth"
import { confirmPasswordRules, emailPattern, passwordRules } from "@/utils"
import Logo from "/assets/images/fastapi-logo.svg" import Logo from "/assets/images/fastapi-logo.svg"
import type { UserRegister } from "../client"
import useAuth, { isLoggedIn } from "../hooks/useAuth"
import { confirmPasswordRules, emailPattern, passwordRules } from "../utils"
export const Route = createFileRoute("/signup")({ export const Route = createFileRoute("/signup")({
component: SignUp, component: SignUp,
@@ -80,25 +74,25 @@ function SignUp() {
alignSelf="center" alignSelf="center"
mb={4} mb={4}
/> />
<FormControl id="full_name" isInvalid={!!errors.full_name}> <Field
<FormLabel htmlFor="full_name" srOnly> invalid={!!errors.full_name}
Full Name errorText={errors.full_name?.message}
</FormLabel> >
<InputGroup w="100%" startElement={<FiUser />}>
<Input <Input
id="full_name" id="full_name"
minLength={3} minLength={3}
{...register("full_name", { required: "Full Name is required" })} {...register("full_name", {
required: "Full Name is required",
})}
placeholder="Full Name" placeholder="Full Name"
type="text" type="text"
/> />
{errors.full_name && ( </InputGroup>
<FormErrorMessage>{errors.full_name.message}</FormErrorMessage> </Field>
)}
</FormControl> <Field invalid={!!errors.email} errorText={errors.email?.message}>
<FormControl id="email" isInvalid={!!errors.email}> <InputGroup w="100%" startElement={<FiUser />}>
<FormLabel htmlFor="username" srOnly>
Email
</FormLabel>
<Input <Input
id="email" id="email"
{...register("email", { {...register("email", {
@@ -108,52 +102,30 @@ function SignUp() {
placeholder="Email" placeholder="Email"
type="email" type="email"
/> />
{errors.email && ( </InputGroup>
<FormErrorMessage>{errors.email.message}</FormErrorMessage> </Field>
)} <PasswordInput
</FormControl> type="password"
<FormControl id="password" isInvalid={!!errors.password}> startElement={<FiLock />}
<FormLabel htmlFor="password" srOnly>
Password
</FormLabel>
<Input
id="password"
{...register("password", passwordRules())} {...register("password", passwordRules())}
placeholder="Password" placeholder="Password"
type="password" errors={errors}
/> />
{errors.password && ( <PasswordInput
<FormErrorMessage>{errors.password.message}</FormErrorMessage> type="confirm_password"
)} startElement={<FiLock />}
</FormControl>
<FormControl
id="confirm_password"
isInvalid={!!errors.confirm_password}
>
<FormLabel htmlFor="confirm_password" srOnly>
Confirm Password
</FormLabel>
<Input
id="confirm_password"
{...register("confirm_password", confirmPasswordRules(getValues))} {...register("confirm_password", confirmPasswordRules(getValues))}
placeholder="Repeat Password" placeholder="Confirm Password"
type="password" errors={errors}
/> />
{errors.confirm_password && ( <Button variant="solid" type="submit" loading={isSubmitting}>
<FormErrorMessage>
{errors.confirm_password.message}
</FormErrorMessage>
)}
</FormControl>
<Button variant="primary" type="submit" isLoading={isSubmitting}>
Sign Up Sign Up
</Button> </Button>
<Text> <Text>
Already have an account?{" "} Already have an account?{" "}
<Link as={RouterLink} to="/login" color="blue.500"> <RouterLink to="/login" className="main-link">
Log In Log In
</Link> </RouterLink>
</Text> </Text>
</Container> </Container>
</Flex> </Flex>
+21 -51
View File
@@ -1,61 +1,31 @@
import { extendTheme } from "@chakra-ui/react" import { createSystem, defaultConfig } from "@chakra-ui/react"
import { buttonRecipe } from "./theme/button.recipe"
const disabledStyles = { export const system = createSystem(defaultConfig, {
_disabled: { globalCss: {
backgroundColor: "ui.main", html: {
fontSize: "16px",
}, },
} body: {
fontSize: "0.875rem",
const theme = extendTheme({ margin: 0,
padding: 0,
},
".main-link": {
color: "ui.main",
fontWeight: "bold",
},
},
theme: {
tokens: {
colors: { colors: {
ui: { ui: {
main: "#009688", main: { value: "#009688" },
secondary: "#EDF2F7",
success: "#48BB78",
danger: "#E53E3E",
light: "#FAFAFA",
dark: "#1A202C",
darkSlate: "#252D3D",
dim: "#A0AEC0",
},
},
components: {
Button: {
variants: {
primary: {
backgroundColor: "ui.main",
color: "ui.light",
_hover: {
backgroundColor: "#00766C",
},
_disabled: {
...disabledStyles,
_hover: {
...disabledStyles,
},
},
},
danger: {
backgroundColor: "ui.danger",
color: "ui.light",
_hover: {
backgroundColor: "#E32727",
},
},
},
},
Tabs: {
variants: {
enclosed: {
tab: {
_selected: {
color: "ui.main",
},
}, },
}, },
}, },
recipes: {
button: buttonRecipe,
}, },
}, },
}) })
export default theme
+21
View File
@@ -0,0 +1,21 @@
import { defineRecipe } from "@chakra-ui/react"
export const buttonRecipe = defineRecipe({
base: {
fontWeight: "bold",
display: "flex",
alignItems: "center",
justifyContent: "center",
colorPalette: "teal",
},
variants: {
variant: {
ghost: {
bg: "transparent",
_hover: {
bg: "gray.100",
},
},
},
},
})
+4 -2
View File
@@ -1,4 +1,5 @@
import type { ApiError } from "./client" import type { ApiError } from "./client"
import useCustomToast from "./hooks/useCustomToast"
export const emailPattern = { export const emailPattern = {
value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i, value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i,
@@ -43,11 +44,12 @@ export const confirmPasswordRules = (
return rules return rules
} }
export const handleError = (err: ApiError, showToast: any) => { export const handleError = (err: ApiError) => {
const { showErrorToast } = useCustomToast()
const errDetail = (err.body as any)?.detail const errDetail = (err.body as any)?.detail
let errorMessage = errDetail || "Something went wrong." let errorMessage = errDetail || "Something went wrong."
if (Array.isArray(errDetail) && errDetail.length > 0) { if (Array.isArray(errDetail) && errDetail.length > 0) {
errorMessage = errDetail[0].msg errorMessage = errDetail[0].msg
} }
showToast("Error", errorMessage, "error") showErrorToast(errorMessage)
} }
+10
View File
@@ -115,3 +115,13 @@ test("Logged-out user cannot access protected routes", async ({ page }) => {
await page.goto("/settings") await page.goto("/settings")
await page.waitForURL("/login") await page.waitForURL("/login")
}) })
test("Redirects to /login when token is wrong", async ({ page }) => {
await page.goto("/settings")
await page.evaluate(() => {
localStorage.setItem("access_token", "invalid_token")
})
await page.goto("/settings")
await page.waitForURL("/login")
await expect(page).toHaveURL("/login")
})
+12 -8
View File
@@ -50,7 +50,9 @@ test("User can reset password successfully using the link", async ({
timeout: 5000, timeout: 5000,
}) })
await page.goto(`http://localhost:1080/messages/${emailData.id}.html`) await page.goto(
`${process.env.MAILCATCHER_HOST}/messages/${emailData.id}.html`,
)
const selector = 'a[href*="/reset-password?token="]' const selector = 'a[href*="/reset-password?token="]'
@@ -62,8 +64,8 @@ test("User can reset password successfully using the link", async ({
// Set the new password and confirm it // Set the new password and confirm it
await page.goto(url) await page.goto(url)
await page.getByLabel("Set Password").fill(newPassword) await page.getByPlaceholder("New Password").fill(newPassword)
await page.getByLabel("Confirm Password").fill(newPassword) await page.getByPlaceholder("Confirm Password").fill(newPassword)
await page.getByRole("button", { name: "Reset Password" }).click() await page.getByRole("button", { name: "Reset Password" }).click()
await expect(page.getByText("Password updated successfully")).toBeVisible() await expect(page.getByText("Password updated successfully")).toBeVisible()
@@ -77,8 +79,8 @@ test("Expired or invalid reset link", async ({ page }) => {
await page.goto(invalidUrl) await page.goto(invalidUrl)
await page.getByLabel("Set Password").fill(password) await page.getByPlaceholder("New Password").fill(password)
await page.getByLabel("Confirm Password").fill(password) await page.getByPlaceholder("Confirm Password").fill(password)
await page.getByRole("button", { name: "Reset Password" }).click() await page.getByRole("button", { name: "Reset Password" }).click()
await expect(page.getByText("Invalid token")).toBeVisible() await expect(page.getByText("Invalid token")).toBeVisible()
@@ -103,7 +105,9 @@ test("Weak new password validation", async ({ page, request }) => {
timeout: 5000, timeout: 5000,
}) })
await page.goto(`http://localhost:1080/messages/${emailData.id}.html`) await page.goto(
`${process.env.MAILCATCHER_HOST}/messages/${emailData.id}.html`,
)
const selector = 'a[href*="/reset-password?token="]' const selector = 'a[href*="/reset-password?token="]'
let url = await page.getAttribute(selector, "href") let url = await page.getAttribute(selector, "href")
@@ -111,8 +115,8 @@ test("Weak new password validation", async ({ page, request }) => {
// Set a weak new password // Set a weak new password
await page.goto(url) await page.goto(url)
await page.getByLabel("Set Password").fill(weakPassword) await page.getByPlaceholder("New Password").fill(weakPassword)
await page.getByLabel("Confirm Password").fill(weakPassword) await page.getByPlaceholder("Confirm Password").fill(weakPassword)
await page.getByRole("button", { name: "Reset Password" }).click() await page.getByRole("button", { name: "Reset Password" }).click()
await expect( await expect(

Some files were not shown because too many files have changed in this diff Show More