Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

swrとorval導入 #902

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open

Conversation

Kubosaka
Copy link
Collaborator

@Kubosaka Kubosaka commented Dec 26, 2024

概要

  • openapiを導入したので、フロントでも型やhooksで使えるように対応
  • orval導入
  • swr導入
  • custom-instance作成(本番と開発でbaseUrlを使い分けたいため)
  • 協賛企業ページで生成したhooksを使って実装しました
  • make gen-front-apiでhooksとTypeを自動生成します

apiクライアントにはswr+fetchを使用しました。
swrはvercelが提供しており、軽量でシンプルなので採用しました。

画面スクリーンショット等

  • URL
    スクリーンショット

テスト項目

  • 協賛企業ページが問題なく動作するか確認

備考

@Kubosaka Kubosaka requested review from hikahana and TkymHrt December 26, 2024 13:35
@Kubosaka Kubosaka self-assigned this Dec 26, 2024
@hikahana
Copy link
Collaborator

hikahana commented Dec 27, 2024

マージ先feat/kubosaka/create-budgets-openapiなのは意図ありです?

Copy link
Collaborator

@hikahana hikahana left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

よさそう!
型アサシエーションとかの部分多分直せました

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo
同じyamlファイルですし、backとfrontでmake分けるコマンド分ける必要ない気がするので
make genとかで一括に実行させて良さそう

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.eslintrc.json

  "ignorePatterns": ["orval.config.js", "src/generated/"]

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自動生成系はlint無視してもよさそう。

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これでyearPeriodsと型の修正。全コード貼り付け
微修正はよろしくお願いします。

import clsx from 'clsx';
import type { NextPage } from 'next';
import Head from 'next/head';
import { useState } from 'react';
import OpenDeleteModalButton from '@/components/sponsors/OpenDeleteModalButton';
import OpenEditModalButton from '@/components/sponsors/OpenEditModalButton';
import { useGetSponsorsPeriodsYear, useGetYearsPeriods } from '@/generated/hooks';
import { Card, Loading, Title } from '@components/common';
import MainLayout from '@components/layout/MainLayout';
import OpenAddModalButton from '@components/sponsors/OpenAddModalButton';

const date = new Date();

const Sponsorship: NextPage = () => {
  const { data: getYearPeriods } = useGetYearsPeriods();
  const yearPeriods = getYearPeriods?.data;

  const [selectedYear, setSelectedYear] = useState<string>(
    yearPeriods ? String(yearPeriods[yearPeriods.length - 1].year) : String(date.getFullYear()),
  );

  const { data, isLoading, error } = useGetSponsorsPeriodsYear(Number(selectedYear));
  const sponsors = data?.data;

  if (isLoading) return <Loading />;
  if (error) return <div>error...</div>;

  return (
    <MainLayout>
      <Head>
        <title>協賛企業一覧</title>
        <meta name='viewport' content='initial-scale=1.0, width=device-width' />
      </Head>
      <Card w='w-1/1'>
        <div className='mx-5 mt-10'>
          <div className='flex'>
            <Title title={'協賛企業一覧'} />
            <select
              className='w-100'
              defaultValue={selectedYear}
              onChange={(e) => setSelectedYear(e.target.value)}
            >
              {yearPeriods &&
                yearPeriods.map((year, index) => {
                  return (
                    <option value={year.year} key={index}>
                      {year.year}年度
                    </option>
                  );
                })}
            </select>
          </div>
          <div className='hidden justify-end md:flex'>
            <OpenAddModalButton>協賛企業登録</OpenAddModalButton>
          </div>
        </div>
        <div className='mb-2 overflow-scroll p-5'>
          <table className='mb-5 w-max table-auto border-collapse md:w-full md:table-fixed'>
            <thead>
              <tr className='border border-x-white-0 border-b-primary-1 border-t-white-0 py-3'>
                <th className='w-1/8 border-b-primary-1 pb-2'>
                  <div className='mr-1 text-center text-sm text-black-600'>企業名</div>
                </th>
                <th className='w-1/8 border-b-primary-1 pb-2'>
                  <div className='text-center text-sm text-black-600'>電話番号</div>
                </th>
                <th className='w-1/6 border-b-primary-1 pb-2'>
                  <div className='text-center text-sm text-black-600'>メール</div>
                </th>
                <th className='w-1/6 border-b-primary-1 pb-2'>
                  <div className='text-center'>住所</div>
                </th>
                <th className='w-1/8 border-b-primary-1 pb-2'>
                  <div className='text-center text-black-600'>代表者</div>
                </th>
                <th className='w-1/12 border-b-primary-1 pb-2'>
                  <div className='text-center text-black-600'></div>
                </th>
              </tr>
            </thead>
            <tbody className='border border-x-white-0 border-b-primary-1 border-t-white-0'>
              {sponsors && sponsors.length > 0 ? (
                sponsors.map((sponsor, index) => (
                  <tr
                    className={clsx(index !== sponsors.length - 1 && 'border-b')}
                    key={sponsor.id}
                  >
                    <td className='py-3'>
                      <div className='text-center text-black-300'>{sponsor.name}</div>
                    </td>
                    <td>
                      <div className='text-center text-black-300'>{sponsor.tel}</div>
                    </td>
                    <td>
                      <div className='text-center text-black-300'>{sponsor.email}</div>
                    </td>
                    <td>
                      <div className='text-center text-black-300'>{sponsor.address}</div>
                    </td>
                    <td>
                      <div className='text-center text-black-300'>{sponsor.representative}</div>
                    </td>
                    <td>
                      <div className='flex gap-3'>
                        <OpenEditModalButton sponsor={sponsor} />
                        <OpenDeleteModalButton id={sponsor.id || 0} />
                      </div>
                    </td>
                  </tr>
                ))
              ) : (
                <tr>
                  <td colSpan={6} className='py-3'>
                    <div className='text-center text-black-300'>データがありません</div>
                  </td>
                </tr>
              )}
            </tbody>
          </table>
        </div>
      </Card>
      <div className='fixed bottom-4 right-4 md:hidden'>
        <OpenAddModalButton />
      </div>
    </MainLayout>
  );
};

export default Sponsorship;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getのcontentに型付けました。
components/sponsorにid追加しました。

openapi_20241228.txt

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

teacherのコメントアウトは何も弄ってません

@hikahana
Copy link
Collaborator

あとtsconfig.paths.jsonにこれも追加してもいいかも!
使う場面があるかはちょっとわかりません
"@model/*": ["src/generated/model/*"]

Base automatically changed from feat/kubosaka/create-budgets-openapi to develop December 30, 2024 05:47
@Kubosaka
Copy link
Collaborator Author

openapiの定義からブランチ切ったためですね
今マージしたので向き先がdevelopに変わっています
#902 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants