Nova postagem

Rechercher

Artigo
· Dez. 26, 2023 3min de leitura

CSPアプリケーションをReactを使って書き換えるその4

IRIS側の処理は、IRISでREST APIを実装する方法を理解していれば、簡単です。

前回のログイン処理でユーザー認証をIRIS側でどのように実装されているか確認して見ましょう。

まずはディスパッチクラスの定義です。

Shop.Brokerというクラスの中で定義されています。

checkpasswordというメソッドが最後に定義されていて、最終的にShop.Rest.Customer:checkPasswordという(クラス)メソッドが呼ばれているのがわかると思います。

 

ここで定義しているパラメータは、とりあえずおまじない的に含めておくことをお勧めします。

(説明し出すと少し長くなるので)

Class Shop.Broker Extends %CSP.REST
{
Parameter CONVERTINPUTSTREAM = 1;
Parameter HandleCorsRequest = 1;
XData UrlMap
{
<Routes>
  <Route Url="/product/:code" Method="GET" Call="Shop.Rest.Product:getRecord"/>
  <Route Url="/products" Method="GET" Call="Shop.Rest.Product:listRecords"/>
  <Route Url="/deleteproduct/:code" Method="GET" Call="Shop.Rest.Product:deleteRecord"/>
  <Route Url="/addproduct" Method="POST" Call="Shop.Product:createRecord"/>
  <Route Url="/modifyproduct" Method="POST" Call="Shop.Rest.Product:modifyRecord"/>
  <Route Url="/customer/:id" Method="GET" Call="Shop.Rest.Customer:getRecord"/>
  <Route Url="/customers" Method="GET" Call="Shop.Rest.Customer:listRecords"/>
  <Route Url="/deletecustomer/:id" Method="GET" Call="Shop.Rest.Customer:deleteRecord"/>
  <Route Url="/addcustomer" Method="POST" Call="Shop.Customer:createRecord"/>
  <Route Url="/modifycustomer" Method="POST" Call="Shop.Rest.Customer:modifyRecord"/>
  <Route Url="/addorder" Method="POST" Call="Shop.Rest.POrder:createRecord"/>
  <Route Url="/order/:id" Method="GET" Call="Shop.Rest.POrder:getRecord"/>
  <Route Url="/orders" Method="GET" Call="Shop.Rest.POrder:listRecords"/>
  <Route Url="/checkpassword/:userid/:password" Method="GET" Call="Shop.Rest.Customer:checkPassword"/>
</Routes>
}

}

 

 

Shop.Rest.CustomerクラスのcheckPasswordメソッドの中は以下のような感じです。

IRISがわかっている人ならば中身の説明は不要ですよね。

ClassMethod checkPassword(pUserId As %String, pPassword) As %Status
{
    set status = $$$OK
    
    try {
        
      if $data(%request) {
        set %response.ContentType="application/json"
        set %response.CharSet = "utf-8"
      }
      
      set cust=##class(Shop.Customer).CheckPasswd(pUserId, pPassword)
      set return = {}

      if cust = "" {
        set return.authorized = "ng"
      }
      else {
        set return.authorized = "ok"
        set return.ID = cust.%Id()
      }

      do return.%ToJSON()
    
    }
    catch e {
        
      set status = e.AsStatus()
    }
                        
    Quit status
}

 

大体こんな感じです。

結構色々なことを理解しないとなかなか前に進めませんが、やはり理解するには自分で何か作ってみるのが一番です。 

Discussão (0)1
Entre ou crie uma conta para continuar
Artigo
· Dez. 26, 2023 7min de leitura

CSPアプリケーションをReactを使って書き換えるその3

それでは、今回はより具体的にReact開発方法について解説します。

ショップデモのリポジトリの配下にreactというディレクトリがあります。

この下にReactのコードがあります。

ここのreact-setup.mdに記載されている通り、前準備としてreactのテンプレートを作ります。

npx create-react-app shopdemo --template typescript

 

あとはこのReactプロジェクトを動かすためのライブラリのインストールを行います。

詳細は、react-setup.mdに書いてあります。

まず3つのディレクトリがあって、これは絶対こうしなければならないというものでもないのですが、基本的なお作法として用意するのが一般的なようです。

  • public
    • ここにはindex.htmlだけ置くのが一般的なようです。
    • テンプレートが自動生成するものでも良いのですが、Bootstrapを使用する場合は、テンプレートのindex.htmlにそのライブラリのロードを付け加えています。
  • components
    • ここに自分で開発するreactコンポーネントを配置します。
  • hooks
    • hookを用意する場合は、ここに配置します。 ​​​​

ここでは、まずログインをするためのユーザー認証を行うコンポーネントの処理について説明します。

Login.tsxに処理を記述しています。

tsxという拡張子は、jsx形式のファイルで、typescriptで記述する際は、tsxにするのがお作法のようです。


このファイルの終わりの方にreturn文以降に以下のような記述があります。

これがjsx記法と呼ばれるもので、javascriptの中にHTML文を埋め込むことができます。

return (
<div>

<h1>ログイン</h1>

<form onSubmit={handleSubmit}>

<table>

<tbody>

<tr>

<td><label>利用者ID:</label></td>

<td><input name="userid" type="text" placeholder="userid" /></td>

</tr>

<tr>

<td><label>パスワード</label></td>

<td><input name="password" type="password" placeholder="password" /></td>

</tr>

<tr><td><button type="submit">ログイン</button></td></tr>

</tbody>

</table>

</form>

{isError && <p style={{ color: "red" }}>{`${errorText}`}</p>}

</div>

);

};

 

ここでユーザー名とパスワードを入力してもらって、ログイン認証を行う処理を作っていきます。

前回の記事でReactは基本SPAでサブミットはないという説明をしましたが、このログイン処理のように複数のデータをフォーム形式で入力してサブミットするというようなケースは多々あります。

方法は色々あるのですが、ここではreact-router-domというものを使っています。

それ以外にもnext.jsというフレームワークが有名です。

実際にサブミットしているように見えますが、実際にPOSTしているわけではなく、react-router-domというフレームワークの中であくまでもSPAの枠組みの中で処理は実装されている感じです。

(もしかしたらこの理解は間違っているかも)

ここでonSubmitコールバックとしてhandleSubmitというメソッドが呼ばれています。

ここの中括弧は、jsxの作法の1つで、そのカッコ内にjavascriptを記述できます。

ここでは、handleSubmitはJavaScriptの変数で、以下のように定義されています。

ちなみにReactでは変数定義は、基本const、たまにletが使われ、varは使いません。

古いJavaScriptしか知らない人にとってはここが1つのハードルかもしれません。

そして以下のコードは厳密に言うと、TypeScriptですが、あんまり褒められたTypeScriptコードではありません。

eventという変数の型としてanyを使っていますが、本当は適切なイベント型を指定するべきです。

そのイベントに紐づいた(ここはフォームデータなので)データ要素としてユーザー名とパスワードを取得しています。

そしてuserLoginCheckというメソッドを呼び出しています。

  const handleSubmit = (event: any) => {
    event.preventDefault();
    const { userid, password } = event.target.elements;
    userLoginCheck(userid.value, password.value).finally(() => 
    {
      if (login.status) {
        navigate("/Shop", { state: { customerId: login.customerId } })
      }
        
    })
  };

userLoginCheckの中身は以下のようになっています。

const userLoginCheck = async (userid: any, userpassword: any) => {
let status = false;

let customerId = 0;

setIsLoading(true);

setIsError(false);

await axios

.get<any>(`http://${serverAddress}:${serverPort}${applicationName}/checkpassword/${userid}/${userpassword}?IRISUsername=${username}&IRISPassword=${password}`)

.then((result: any) => {

if (result.data.authorized === 'ok') {

login.status = true;

login.customerId = result.data.ID;

}

else {

setIsError(true);

setErrorText('ログインが失敗しました');

}

})

.catch((error: any) => {

setIsError(true)

if (error.response) {

setErrorText(error.response.data.summary);

}

else if (error.request) {

setErrorText(error.request);

}

else {

setErrorText(error.message);

}

})

.finally(() => setIsLoading(false))

};



この後、RESTのインタフェースによりサーバーのAPIを呼び出します。

RESTのインタフェースの実装も複数ありますが、ネット上でサンプルがたくさん見つかるaxiosというライブラリを使用しています。

ここでREST APIのurlを指定します。 

このurlにcheckpasswordというメソッド名が含まれているのがわかると思います。

これがIRIS側で呼ばれるメソッド名となります。

ログインチェックがOKだったら、react-router-domに含まれるnavigateメソッドを呼び出して、/Shopにページ遷移します。

(実際にページ遷移しているわけではなくあくまでもエミュレーション)

あと、axiosの呼び出しは非同期なので、結果が返る前に呼び出しが戻ってくるので、最初はその動きになかなか慣れないかもしれません。

 

ログインが成功するとShop.tsxが呼ばれます。

このページはさらに複数のコンポーネントで構成されています。

そしてショッピングカートの機能を実装するためにcreateContextを使いコンテキスト情報を管理します。

navigate経由でページ遷移する際にデータを引き継ぐ仕組みとしてuseLocationというフックが用意されているので、それを使って情報を引き継ぎます。

jsx記法によるHTML定義の中で動的にデータを変更したい部分にはuseStateというフックを使用します。

import React from 'react';
import { createContext, useState, Dispatch,SetStateAction } from "react";
import { Header } from './Header';
import { ShoppingCart } from './ShoppingCart';
import { ProductList } from './ProductList';
import { useLocation } from "react-router-dom"
export type shopItem = {
        productCode: string;
        productName: string;
        price: number;
        units: number;
  };
  
export const ShopContext = createContext({} as {
    orderItems: shopItem[];
    setOrderItems: Dispatch<SetStateAction<shopItem[]>>;
    }
  ); 

export const Shop = () => {

  const [orderItems, setOrderItems] = useState<shopItem[]>([]);
  
  const location = useLocation();
        
  const values={orderItems,setOrderItems};
  
  return (
    <>
    <div className="title">
    <Header customerId = {location.state.customerId} /> 
    </div>
    <ShopContext.Provider value={values}>
    <div className="shoppingcart" style = {{ float: "left",width: "40%",height: "100%",overflow: "auto",border: "solid #000000 1px"}}>    
    <ShoppingCart customerId = {location.state.customerId} />
    </div>
    <div id="productlist" style = {{ width: "60%",height: "100%",overflow: "auto",border: "solid #000000 1px"}}>
    <ProductList />
    </div>
    </ShopContext.Provider>
    </>    
  );    
}
export default Shop;
Discussão (0)1
Entre ou crie uma conta para continuar
Pergunta
· Dez. 22, 2023

Arbiter ISCAgent version check

How can I check the currently installed version number of an ISCAgent running as the Arbiter in a Red Hat instance?

I can check the properties on a Windows system. I doesn't seem to respond to a --version argument in terminal.

3 Comments
Discussão (3)3
Entre ou crie uma conta para continuar
Anúncio
· Dez. 22, 2023

Happy New Year 2024! Season's greeting to all and sundry!

Dear Community, 

We'd like to take this opportunity to congratulate you on the upcoming 🎄 Festive Season 🎄and wish you a joyful holiday season filled with the warmth of 🧑‍🏫 shared knowledge, 🫂 the camaraderie of fellow members, and the anticipation of 🧑‍💻 exciting projects and contests in the coming year!

As the year comes to a close, let's look back and acknowledge what's happened and what we've achieved during these 365 days. 

We've recently reached a new milestone of 16K members who posted 18K posts! We've hosted developer webinars, put together online and offline meetups, ran programming and tech article contests, and organised hackathons worldwide.

And this is just to begin with! We've been really busy and hope you loved every moment of this year you spent with us! Your active engagement and valuable contributions have transformed the InterSystems Developer Community into a vibrant hub of knowledge and innovation.


Let's celebrate our teams that are making all six of our communities vibrant and friendly places to be. So a huge shout-out to our moderators:

Also, let's not forget the team that's organizing, creating, improving, and doing its best to make this Community and neighbouring portals such a friendly place for everyone: @Dean Andrews, @Evgeny Shvarov, @Anastasia Dyubaylo, @Olga Zavrazhnova, @Irène Mykhailova, @Iryna Mologa, @Vadim Aniskin, @Elena E@Semion Makarov, @Liubka Zelenskaia


Our heartfelt thanks and congratulations to everyone 🎇 

In the upcoming year, may your code be bug-free, your algorithms efficient, and your projects deploy seamlessly. May your debugging sessions be short, and your collaborations thrive. Thank you for contributing to the ever-evolving InterSystems Developers Community.

Happy Holidays! 🌟

5 Comments
Discussão (5)4
Entre ou crie uma conta para continuar
Anúncio
· Dez. 22, 2023

Round 2: GenAI Crowdsourcing Mini-Contest by InterSystems Innovation Acceleration Program

Hi Community,

Round 2 of the GenAI Crowdsourcing Mini-Contest is here! Everyone can join, even if you missed Round 1. You have $5 million in fantasy funds to invest in up to 5 promising submissions.

🎁 Rewards

  • The top-funded submission of Round 1 will emerge victorious.
  • The mastermind of the winning concept earns 5,000 points, while 3 astute "investor(s)" backing the winning idea get a chance to receive 200 bonus points each.

Dive into the game, strategically allocate your virtual investments, and aim for maximum returns! Deadline: Dec 31st, 2023

5 Comments
Discussão (5)2
Entre ou crie uma conta para continuar