skill import
This commit is contained in:
parent
059b2e3e64
commit
23c39cbae2
2 changed files with 54 additions and 4 deletions
|
@ -1,21 +1,32 @@
|
|||
import { observer } from "mobx-react-lite";
|
||||
import { DrawersSkills, SkillsStore } from "./skills_store";
|
||||
import { Drawer, Modal } from "antd";
|
||||
import { Drawer, Modal, message } from "antd";
|
||||
import { CoreInput } from "../../core/ui/input/input";
|
||||
import { CoreText, CoreTextType } from "../../core/ui/text/text";
|
||||
import { TopicViewModel } from "../topics/topic_view_model";
|
||||
import { BtAction, BtActionViewModel } from "../../core/model/skill_model";
|
||||
import { BtAction, BtActionViewModel, ISkill, SkillModel } from "../../core/model/skill_model";
|
||||
import { btDependencyFormBuilder } from "../behavior_tree_builder/presentation/ui/forms/forms";
|
||||
import { CoreButton } from "../../core/ui/button/button";
|
||||
import { CoreSelect } from "../../core/ui/select/select";
|
||||
import { useStore } from "../../core/helper/use_store";
|
||||
import { FormBuilder } from "../../core/ui/form_builder/form_builder";
|
||||
import { ButtonV2 } from "../../core/ui/button/button_v2";
|
||||
import { Result } from "../../core/helper/result";
|
||||
import { plainToInstance } from "class-transformer";
|
||||
|
||||
export const isValidJson = <T,>(json: any): Result<string, T> => {
|
||||
try {
|
||||
return Result.ok(JSON.parse(json));
|
||||
} catch {
|
||||
return Result.error(`is not json type: ${json}`);
|
||||
}
|
||||
};
|
||||
|
||||
export const SkillsScreenPath = "/skills";
|
||||
|
||||
export const SkillsScreen = observer(() => {
|
||||
const store = useStore(SkillsStore);
|
||||
|
||||
return (
|
||||
<>
|
||||
<div>
|
||||
|
@ -24,6 +35,11 @@ export const SkillsScreen = observer(() => {
|
|||
text="new skill"
|
||||
onClick={() => store.editDrawer(DrawersSkills.newSkill, true)}
|
||||
/>
|
||||
<CoreButton
|
||||
style={{ width: "max-content" }}
|
||||
text="import skill"
|
||||
onClick={() => store.editDrawer(DrawersSkills.importSkill, true)}
|
||||
/>
|
||||
{store.skills?.map((el) => (
|
||||
<div
|
||||
style={{
|
||||
|
@ -62,7 +78,22 @@ export const SkillsScreen = observer(() => {
|
|||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<Drawer
|
||||
width={(window.innerWidth / 100) * 50}
|
||||
title={store.titleDrawer}
|
||||
destroyOnClose={true}
|
||||
onClose={() => store.editDrawer(DrawersSkills.importSkill, false)}
|
||||
open={store.drawers.find((el) => el.name === DrawersSkills.importSkill)?.status}
|
||||
>
|
||||
<div style={{ display: "flex", flexDirection: "column", justifyContent: "space-between", height: "100%" }}>
|
||||
<CoreInput
|
||||
style={{ height: "max-content" }}
|
||||
styleContentEditable={{ height: "max-content" }}
|
||||
label="JSON навыка"
|
||||
onChange={(text) => store.importSkill(text)}
|
||||
/>
|
||||
</div>
|
||||
</Drawer>
|
||||
<Drawer
|
||||
width={(window.innerWidth / 100) * 50}
|
||||
title={store.titleDrawer}
|
||||
|
|
|
@ -2,13 +2,16 @@ import makeAutoObservable from "mobx-store-inheritance";
|
|||
import { UiDrawerFormState } from "../../core/store/base_store";
|
||||
import { NavigateFunction } from "react-router-dom";
|
||||
import { HttpError } from "../../core/repository/core_http_repository";
|
||||
import { DependencyViewModel, ParamViewModel, SkillModel } from "../../core/model/skill_model";
|
||||
import { DependencyViewModel, ISkill, ParamViewModel, SkillModel } from "../../core/model/skill_model";
|
||||
import { message } from "antd";
|
||||
import { SkillsHttpRepository } from "./skills_http_repository";
|
||||
import { FormType } from "../../core/model/form";
|
||||
import { plainToInstance } from "class-transformer";
|
||||
import { isValidJson } from "./skills_screen";
|
||||
|
||||
export enum DrawersSkills {
|
||||
newSkill = "Новый навык",
|
||||
importSkill = "Импорт навыка",
|
||||
}
|
||||
export class SkillsStore extends UiDrawerFormState<SkillModel, HttpError> {
|
||||
isModalOpen: boolean = false;
|
||||
|
@ -25,6 +28,22 @@ export class SkillsStore extends UiDrawerFormState<SkillModel, HttpError> {
|
|||
super(DrawersSkills);
|
||||
makeAutoObservable(this);
|
||||
}
|
||||
importSkill = (text: string) =>
|
||||
isValidJson<ISkill>(text.replace(/[^\x00-\x7F]/g, "")).fold(
|
||||
async (model) =>
|
||||
(await (await plainToInstance(SkillModel, model)).valid<SkillModel>()).fold(
|
||||
async (skillModel) => {
|
||||
skillModel._id = undefined;
|
||||
await this.messageHttp(this.skillsHttpRepository.createNewSkill(skillModel), {
|
||||
successMessage: "Навык импортирован",
|
||||
});
|
||||
await this.init();
|
||||
this.editDrawer(DrawersSkills.importSkill, false);
|
||||
},
|
||||
async (error) => message.error(error)
|
||||
),
|
||||
async (error) => message.error(error)
|
||||
);
|
||||
handleFormBuilderModalCancel = () => (this.formBuilderModal = false);
|
||||
init = async (navigate?: NavigateFunction | undefined) => {
|
||||
this.mapOk("skills", this.skillsHttpRepository.getAllSkills());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue