# Topページの作成5

`TopPage.tsx`を**リファクタ**（コードの整理）をしましょう。

まず、ログインしていない場合、上部のヘッダー部分は共通する部分になりますので、コンポーネントとして切り出しましょう。

`components/templates`配下に`NotLoginLayout.tsx`を作成しましょう。

そして、そちらにmainタグまでの部分を貼り付けましょう。

<pre class="language-tsx" data-title="templates/NotLoginLayout.tsx◎"><code class="lang-tsx"><strong>export const NotLoginLayout = () => {
</strong><strong>  return (
</strong><strong>    &#x3C;div className="relative">
</strong><strong>      &#x3C;header className="bg-white leading-[50px] fixed top-0 left-0 right-0">
</strong><strong>        &#x3C;div className="container mx-auto flex justify-between">
</strong><strong>          &#x3C;p className="logo">スケジュール管理APP&#x3C;/p>
</strong><strong>          &#x3C;nav>
</strong><strong>            &#x3C;ul className="flex gap-5 text-lime-800">
</strong><strong>              &#x3C;li>ご利用方法&#x3C;/li>
</strong><strong>              &#x3C;li>ログイン&#x3C;/li>
</strong><strong>            &#x3C;/ul>
</strong><strong>          &#x3C;/nav>
</strong><strong>        &#x3C;/div>
</strong><strong>      &#x3C;/header>
</strong><strong>      &#x3C;main className="pt-[50px] bg-gradient-to-r from-lime-100 to-lime-200 h-screen flex flex-col justify-center items-center">
</strong><strong>      &#x3C;/main>
</strong><strong>    &#x3C;/div>
</strong><strong>  );
</strong><strong>}
</strong></code></pre>

そして、propsとして`children`を受け取れるようにし、mainタグ内に`children`を埋め込みましょう。

<pre class="language-tsx" data-title="templates/NotLoginLayout.tsx◎"><code class="lang-tsx"><strong>import { ReactNode } from "react";
</strong>
<strong>type PropsType = {
</strong><strong>  children: ReactNode
</strong><strong>}
</strong>
export const NotLoginLayout = ({ children }: PropsType) => {
  return (
    &#x3C;div className="relative">
      &#x3C;header className="bg-white leading-[50px] fixed top-0 left-0 right-0">
        &#x3C;div className="container mx-auto flex justify-between">
          &#x3C;p className="logo">スケジュール管理APP&#x3C;/p>
          &#x3C;nav>
            &#x3C;ul className="flex gap-5 text-lime-800">
              &#x3C;li>ご利用方法&#x3C;/li>
              &#x3C;li>ログイン&#x3C;/li>
            &#x3C;/ul>
          &#x3C;/nav>
        &#x3C;/div>
      &#x3C;/header>
      &#x3C;main className="pt-[50px] bg-gradient-to-r from-lime-100 to-lime-200 h-screen flex flex-col justify-center items-center">
<strong>        {children}
</strong>      &#x3C;/main>
    &#x3C;/div>
  );
}
</code></pre>

そして、`TopPage.tsx`にてNotLoginLayoutコンポーネントを作成するようにしましょう。

<pre class="language-tsx" data-title="pages/TopPage.tsx◎"><code class="lang-tsx"><strong>import { NotLoginLayout } from "../templates/NotLoginLayout";
</strong>
export const TopPage = () => {
  return (
<strong>    &#x3C;NotLoginLayout>
</strong>      &#x3C;div className="text-center">
        &#x3C;h1 className="text-7xl logo">スケジュール管理APP&#x3C;/h1>
        &#x3C;p className="pt-[10vh] text-5xl">
          お互いのスケジュールを管理するアプリです
        &#x3C;/p>
        &#x3C;div className="pt-[20vh]">
          &#x3C;button className="bg-lime-800 text-white p-4 text-lg rounded-lg">
            ログイン
          &#x3C;/button>
        &#x3C;/div>
      &#x3C;/div>
<strong>    &#x3C;/NotLoginLayout>
</strong>  );
}
</code></pre>

次にbutton部分は今後使い回すので、コンポーネントとして切り出しましょう。

`components/atoms`配下に`PrimaryBtn.tsx`を作成し、ログインボタン部分を貼り付けましょう。

<pre class="language-tsx" data-title="atoms/PrimaryBtn.tsx◎"><code class="lang-tsx"><strong>export const PrimaryBtn = () => {
</strong><strong>  return (
</strong><strong>    &#x3C;button className="bg-lime-800 text-white p-4 text-lg rounded-lg">
</strong><strong>      ログイン
</strong><strong>    &#x3C;/button>
</strong><strong>  );
</strong><strong>}
</strong>
</code></pre>

ボタンのテキストに関しては、様々な部分にて使い回すのでchildrenとして受け取れるようにしましょう。

<pre class="language-tsx" data-title="atoms/PrimaryBtn.tsx◎"><code class="lang-tsx"><strong>import { ReactNode } from "react";
</strong>
<strong>type PropsType = {
</strong><strong>  children: ReactNode
</strong><strong>}
</strong>
<strong>export const PrimaryBtn = ({ children }: PropsType) => {
</strong>  return (
    &#x3C;button className="bg-lime-800 text-white p-4 text-lg rounded-lg">
<strong>      {children}
</strong>    &#x3C;/button>
  );
};

</code></pre>

そして、PrimaryBtnコンポーネントを`TopPage.tsx`にて使用しましょう。

<pre class="language-tsx" data-title="pages/TopPage.tsx◎"><code class="lang-tsx"><strong>import { PrimaryBtn } from "../atoms/PrimaryBtn";
</strong>import { NotLoginLayout } from "../templates/NotLoginLayouts";

export const TopPage = () => {
  return (
    &#x3C;NotLoginLayout>
      &#x3C;div className="text-center">
        &#x3C;h1 className="text-7xl logo">スケジュール管理APP&#x3C;/h1>
        &#x3C;p className="pt-[10vh] text-5xl">
          お互いのスケジュールを管理するアプリです
        &#x3C;/p>
        &#x3C;div className="pt-[20vh]">
<strong>          &#x3C;PrimaryBtn>ログイン&#x3C;/PrimaryBtn>
</strong>        &#x3C;/div>
      &#x3C;/div>
    &#x3C;/NotLoginLayout>
  );
}

</code></pre>

ここまでの作業をcommitしましょう。

```bash
git add .
```

```bash
git commit
```

commitメッセージは、

```
refactor: ヘッダー部分のレイアウトとボタンをコンポーネントに切り出した
```

としましょう。

ではTopページの作成が完了したので、GitHubにpushしましょう。

```basic
git push origin feature/top-page
```

これでGitHubのリポジトリのページにいき、main <- feature/top-pageのPRを作成しましょう。

<figure><img src="https://1869761657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcUBbYqol4PMzZJggiMqV%2Fuploads%2FlBbvvucXwD1OzU7atppU%2Faa.png?alt=media&#x26;token=9ac49be7-6a06-49a2-bf45-96ea47ee7c68" alt=""><figcaption></figcaption></figure>

作成したら自身で`Merge pull request`を押してmergeしましょう。

<figure><img src="https://1869761657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcUBbYqol4PMzZJggiMqV%2Fuploads%2FnUJQHgFgyz0Xccop6B41%2Fbb.png?alt=media&#x26;token=41b4eb63-77ce-4c80-a967-1cbd40603500" alt=""><figcaption></figcaption></figure>

mergeが完了したらWarpに戻り、mainをpullしましょう。

```bash
git switch main
```

```bash
git pull origin main
```
