1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

24日目にLINEミニアプリが完成する初心者 ―― Day21. クーポン表示機能を実装

Last updated at Posted at 2024-12-20

前回は「24 日目に LINE ミニアプリが完成する初心者 ―― Day20. 二次元コードリーダーを表示する」と題して、二次元コードリーダーについて学習していました。


今回は二次元コードリーダーの実践編として、クーポンを表示してみたいと思います。

クーポンを作成

今回は二次元コードリーダーで"HONOLULU_COOKIES"という文字列を読み取った時だけ、ホノルルクッキープレゼントのクーポンを表示する実装にして行きたいと思います。

コンポーネントの作成

はじめにクーポン用のコンポーネントを作成して行きます。

クーポンを使うボタンを下に表示しています。

src/nextjs/components/ui/Coupon/Coupon.tsx
import {
  Card,
  CardContent,
  CardHeader,
  CardMedia,
  Typography,
} from "@mui/material";
import Button from "../Button/Button";

export default function Coupon() {
  return (
    <Card className="w-11/12 bg-red-100 p-8">
      <CardHeader title="ホノルルクッキープレゼント!" />
      <CardMedia
        component="img"
        height="194"
        image="/honolulu_cookies.png"
        alt="Paella dish"
      />
      <CardContent>
        <Typography>
          本クーポン提示で、ホノルルクッキーを
          <br />
          プレゼントさせていただきます。
          <br />
          (お一人様一回限り)
        </Typography>
      </CardContent>
      <Button label="クーポン使う" />
    </Card>
  );
}

作ったコンポーネントはこのようになりました。

モーダルの追加

作ったコンポーネントをモーダルとして表示できるように追加して行きます。

モーダルはuseStateを使って管理します。

src/nextjs/pages/index.tsx
import { useState } from "react";
import Head from "next/head";
import liff from "@line/liff";
import { Modal } from "@mui/material";
import { Button, Coupon } from "@/components/ui";

export default function Home() {
  const [text, setText] = useState("ここにテキストが表示されます");
  const [open, setOpen] = useState<boolean>(true);

  const handleClick = () => {
    liff
      .scanCodeV2()
      .then((result) => {
        setText(result.value);
      })
      .catch((error) => {
        console.log("error", error);
      });
  };

  return (
    <div>
      <Head>
        <title>Mahalo Living</title>
      </Head>
      <div className="home">
        <h1 className="home__title">Mahalo Living</h1>
      </div>
      <div className="text-center">
        <Button
          label="二次元コードリーダー"
          onClick={() => handleClick()}
          width="w-1/2"
        />
        <h1 className="text-3xl font-medium text-center p-12">{text}</h1>
        <Modal
          open={open}
          onClose={() => setOpen(false)}
          className="flex items-center"
        >
          <div className="flex justify-center">
            <Coupon onClose={setOpen} />
          </div>
        </Modal>
      </div>
    </div>
  );
}

上記のコードではCouponsetOpenを渡していますが、まだCoupon側で受け取る設定が設定ができていないため、以下の通り追加を行って行きます。

src/nextjs/components/ui/Coupon/Coupon.tsx
export default function Coupon({ onClose }: {onClose: Dispatch<SetStateAction<boolean>>}) {
  return (
    <Card className="w-11/12 bg-red-100 p-8">

      {/* 中略 */}

      <Button label="クーポン使う" onClick={() => onClose(false)} />
    </Card>
  );
}

クーポンコードの判定ロジック追加

ここまでの実装ではクーポンコードに応じたモーダルオープンが実装出来ていないため追加を行って行きます。

handleClickthenにロジックを追加します。

src/nextjs/pages/index.tsx
  const handleClick = () => {
    liff
      .scanCodeV2()
      .then((result) => {
        if (result.value === "HONOLULU_COOKIES") {
          setText("クーポンを取得しました");
          setOpen(true);
        } else {
          setText("クーポンが取得できませんでした");
        }
      })
      .catch((error) => {
        console.log("error", error);
      });
  };

これで実装は完了です!

デプロイ

いつも通り以下のコマンドをline-liff-v2-starter配下で実行しデプロイを行います。

$ netlify deploy --build --prod

# 動作確認

LINE から動作確認を行って行きます。

QR コードは前回同様以下のサイトで作成しています。

初めはHONOLULU_COOKIESという文言で QR コードを作成して読み取りを行います。

すると以下の通りクーポンが表示されました!



「クーポンを使う」をタッチすると「クーポンを取得しました」というメッセージが表示されました。



次に別の文字列で作成した QR コードを読み込んでみます。

すると今度はクーポンは表示されず、「クーポンが取得できませんでした」という文字列が表示されました。

まとめ

ここまでで 二次元コードリーダーについて学習してきました。

次回は、詳しく触れていなかった部分として Messaging API について学習して行きたいと思います。

残りは 3 日!
気になる方は是非フォローやカレンダー購読をお願いします:star:

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?