Open kirin-ri opened 1 year ago
import React from "react"
import { Spacer } from "../spacer";
import { useState, useEffect } from 'react';
import NewAPIDtlModal from './newAPIDtlModal'
import NewSampleDataModal from './newSampleDataModal'
import CreateNewApi from './createNewApi'
import EditApiList from './editApiList'
import { commonAjax } from '../../../components/commonAjax';
import * as Defs from './apiDefs';
import MessageModal from "./MessageModal";
// main
function NewApiList() {
// useState
const [apiList, setApiList] = useState<Defs.Api[]>([]);
const [tagList, setTags] = useState<Defs.Tag[]>([]);
const [changeStatus, setChangeStatus] = useState<boolean>(false);
/* API情報取得のリクエスト */
useEffect(() => {
(async () => {
commonAjax
.axios({loading: true})
.get('/api/api')
.then((res) => {
const data = res.data.api;
const resTags: React.SetStateAction<Defs.Tag[]> = []
let tmpTags:string[]=[]
data.map((item:any)=>{
tmpTags=tmpTags.concat(item.tags)
})
tmpTags = Array.from(new Set(tmpTags))
tmpTags.map((t:string)=>{
resTags.push({tag_name:t,select:true})
})
setApiList(data);
setTags(resTags)
});
})();
}, [changeStatus]);
return (
<div className="content-wrapper scm-api-list">
<section className="page-cover">
<h1>SCM API一覧</h1>
</section>
{/* Content Header (Page header) */}
<section className="content-header">
<div className="content-header-left">
<h1>SCM API一覧</h1>
<div className="content-header-desc">現在、設定されているAPI一覧です</div>
</div>
<div className="content-header-right">
<button className="btn btn-secondary" data-toggle="modal" data-target='#editApiListModal'>編集</button>
<button className="btn btn-primary" data-toggle="modal" data-target='#api-dtl-modal'>追加</button>
</div>
</section>
<section className="content">
{/* filter tag */}
<div className="inline-form">
<div className="inline-form-cat">フィルタータグ</div>
{FilterTags(tagList, setTags)}
</div>
{Spacer({"size":30})}
{/* API List (Grid) */}
{viewList(apiList, tagList, changeStatus, setChangeStatus)}
</section>
<CreateNewApi
changeStatus={changeStatus}
setChangeStatus={setChangeStatus}
/>
<EditApiList
val={apiList}
changeStatus={changeStatus}
setChangeStatus={setChangeStatus}
/>
<MessageModal
changeStatus={changeStatus}
setChangeStatus={setChangeStatus}
/>
</div>
);
}
// fillter tag
function FilterTags (tags:Defs.Tag[],setTags:any){
return(
<div className="inline-form-group">
<div className="inline-form-label">分類</div>
{tags.map((item:Defs.Tag) => {
if(item.select){
return(
<div className="btn btn-tag active"
onClick={()=>{
item.select = false;
let t = tags.slice(0,tags.length);
setTags(t);
}}
>
{item.tag_name}
</div>
)
}else{
return(
<div className="btn btn-tag"
onClick={()=>{
item.select = true;
let t = tags.slice(0,tags.length);
setTags(t);
}}
>
{item.tag_name}
</div>
)
}
})}
</div>
)
}
function viewList (item:Defs.Api[], tagList:Defs.Tag[], changeStatus:boolean, setChangeStatus:any){
// if(item.length){
const length = Math.ceil(item.length / 3)
let listItem:any=[]
if(length!=0){
listItem = transpose(new Array(length).fill(0).map((_, i) => item.slice(i * 3, (i + 1) * 3)))
}
return(
<div className="list-wrapper">
<div className="apiList">
{0 >= 0&&0<listItem.length?listItem[0].map((val: Defs.Api)=>ApiCard(val,tagList,changeStatus,setChangeStatus)):null}
</div>
<div className="apiList">
{1>= 0&&1<listItem.length?listItem[1].map((val: Defs.Api)=>ApiCard(val,tagList,changeStatus,setChangeStatus)):null}
</div>
<div className="apiList">
{2>= 0&&2<listItem.length?listItem[2].map((val: Defs.Api)=>ApiCard(val,tagList,changeStatus,setChangeStatus)):null}
</div>
</div>
)
// }
}
// API Card
function ApiCard(api: Defs.Api,tagList:Defs.Tag[],changeStatus:boolean,setChangeStatus:any) {
let selected = false
tagList.map((item:Defs.Tag)=>{
api.tags.map((apiTag:string)=>{
if(apiTag==item.tag_name){
if(item.select){
selected=true
}
}
})
})
$('#'+api.physical_name).collapse("hide");
return (
<>
<div className={`card ${selected?'card-primary':'card-not-applicable not-applicable'} card-collapse-sample`}>
<div className="card-header collapsed" data-toggle="collapse" data-target={'#'+api.physical_name}>
<div className="api-name-font" style={{fontSize: `clamp(0.6rem, ${29 / (api.physical_name+' / '+api.logical_name).length}vw, 1rem)`}}>
<i
className={`fa fa-circle provide-icon-size ${
selected?api.provide? 'provide-icon-color': 'no-provide-icon-color': undefined
}`}
aria-hidden="true"
/>
{Spacer({"size":10, horizontal:true})}
{api.physical_name+' / '+api.logical_name}
</div>
</div>
<div className="card-body collapse" id={api.physical_name}>
<div className="api-list-title">
<p>
API 連携項目 一覧
</p>
{Spacer({"size":20})}
</div>
{api.outinfo.map((val: Defs.TableInfo) => {
const res: JSX.Element[] = []
val.column.map((item: Defs.Column) => {
res.push(
<div className="list-contents-grid">
<p>{item.logicalName}</p>
</div>
)
})
return res
})}
{Spacer({"size":20})}
<div className="btn-center">
{api.provide
?
<button
className="btn btn-secondary btn-secondary"
data-toggle="modal"
data-target={"#"+api.physical_name+"-dtl-modal"}
>
サンプルデータ挿入 / API連携項目追加
</button>
:
<button
className="btn btn-secondary btn-secondary"
onClick={()=>{putDeploy(api,changeStatus,setChangeStatus)}}
>
Deploy
</button>
}
</div>
{/* <div>{NewAPIDtlModal(api)}</div> */}
<NewAPIDtlModal val={api} />
<NewSampleDataModal id={api.physical_name}/>
</div>
</div>
</>
)
}
// Sample Modal
function Modal() {
return (
<>
{/* Content Header (Page header) */}
<div className="modal fade" id="sampleModal1">
<div className="modal-dialog modal-xl">
<div className="modal-content">
<div className="modal-header">
<h4 className="modal-title">SAMPLE MODAL</h4>
<button type="button" className="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div className="modal-body">
<p>BODY</p>
</div>
</div>
{/* <!-- /.modal-content --> */}
</div>
{/* <!-- /.modal-dialog --> */}
</div>
{/* <!-- /.modal --> */}
{/* /.content */}
</>
)
}
// transpose columns and rows
const transpose = (a: any[]) => a[0].map((_: any, c: string | number) => a.map(r => r[c]).filter(Boolean));
// put api(deploy)
function putDeploy(api:Defs.Api,changeStatus:boolean,setChangeStatus:any){
if (api) {
if (!api.provide) {
(async () => {
commonAjax
.axios({swalFire: true, loading: true})
.put(`/api/api/${api.physical_name}`)
.then((res) => {
setChangeStatus(changeStatus?false:true);
});
})();
}
}
}
export default NewApiList;
import React, { ChangeEvent, useCallback, useEffect, useLayoutEffect, useState } from "react";
import { useForm } from 'react-hook-form';
import { Spacer } from "../spacer";
import { commonAjax } from '../../../components/commonAjax';
import * as Defs from './apiDefs';
import { Colors } from "chart.js";
import { hide } from "@popperjs/core";
// Message Modal
const MessageModal = (props: {changeStatus:boolean,setChangeStatus:any }) => {
// 遷移元画面の更新に使用
const changeStatus = props.changeStatus
const setChangeStatus = props.setChangeStatus
function handleModalClose() {
$("#MessageModal").modal("hide").off("hidden.bs.modal")
}
return (
<>
{/* Content Header (Page header) */}
<div className="modal fade" id="MessageModal">
<div className="modal-dialog modal-xl">
<div className="modal-content">
<div className="modal-header">
<h4 className="modal-title">サンプルデータ挿入 / API連携項目追加</h4>
<button type="button" className="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div className="modal-body">
<div className="name-disp-area">
<div className="api-name">
<div className="dtl-content-width">API 論理名</div>
<div className="dtl-modal-font-color"> 完成品販売計画(年次)</div>
</div>
<div className="api-name">
<div className="dtl-content-width">API 物理名</div>
<div className="dtl-modal-font-color">product_sales_y_plan</div>
</div>
</div>
</div>
<div className="modal-body">
<div className="dividing-line"/>
</div>
<div className="modal-body">
<div className="text-center">
<div className="text-center">
<div className="text-center">
<p>API項目が更新されました。</p>
<p>再度マッピングを行う必要があります。</p></div>
</div>
</div>
{Spacer({"size":25})}
<div className="text-center">
<div className="centering-btn">
<button className="btn btn-primary" onClick={handleModalClose}>
OK
</button>
</div>
</div>
{Spacer({"size":25})}
</div>
</div>
{/* <!-- /.modal-content --> */}
</div>
{/* <!-- /.modal-dialog --> */}
</div>
{/* <!-- /.modal --> */}
{/* /.content */}
</>
)
}
export default MessageModal;