|
@@ -1,6 +1,7 @@
|
|
|
import { create } from "zustand";
|
|
|
import { editAgentWebsite as _editAgentWebsite, editAgentAvatar, editAgentWebsite } from "@/service/agent";
|
|
|
import { TComponentItem } from "@/types/agent";
|
|
|
+import { isSuccess } from "@/utils";
|
|
|
|
|
|
|
|
|
function safeSwap(arr:any[], index1:number, index2: number) {
|
|
@@ -23,7 +24,7 @@ export interface ComponentState {
|
|
|
currentComponent: TComponentItem | null;
|
|
|
setComponentList: (list: TComponentItem[], agentId: string) => void;
|
|
|
delComponent: (c: TComponentItem) => Promise<void>;
|
|
|
- saveComponent: (c: TComponentItem) => Promise<TComponentItem | null>;
|
|
|
+ saveComponent: (c: TComponentItem) => Promise<boolean>;
|
|
|
insertComponent: (c: TComponentItem[]) => Promise<TComponentItem[] | null>;
|
|
|
swapTwoComponent: (c: TComponentItem, direction: number) => Promise<void>;
|
|
|
setCurrentComponent: (
|
|
@@ -113,43 +114,37 @@ export const useComponentStore = create<ComponentState>((set, get) => ({
|
|
|
saveComponent: async (c: TComponentItem) => {
|
|
|
console.log("saveComponent");
|
|
|
|
|
|
- // insertIndex 是组件列表插入组件时提前设置的
|
|
|
+ if (c.data.id === undefined) {
|
|
|
+ console.log('没有 id 无法编辑该组件')
|
|
|
+ return false
|
|
|
+ }
|
|
|
+
|
|
|
const insertIndex = get().insertIndex;
|
|
|
- console.log(c.id, insertIndex);
|
|
|
- // 没有 id 且 insertIndex > -1 说明是插入组件
|
|
|
- if (c.id === undefined && insertIndex > -1) {
|
|
|
- console.log("insertComponentBatch:", c, insertIndex);
|
|
|
+ const agentId = get().agentId;
|
|
|
+ const components = [...get().components];
|
|
|
+ console.log(c.data.id, insertIndex);
|
|
|
+
|
|
|
+ const edit = components.find(item => item.data.id === c.data.id)
|
|
|
|
|
|
- return await get().insertComponent(c);
|
|
|
+ if(edit){
|
|
|
+ edit.data = c.data
|
|
|
+ edit.enabled = c.enabled
|
|
|
}
|
|
|
|
|
|
// 简单更新组件
|
|
|
- console.log("updateComponent: ", c, insertIndex);
|
|
|
- // 如果是普通保存(直接 tabbar 点击添加组件按钮进入,非插入)
|
|
|
- if (c.id === undefined) {
|
|
|
- return null;
|
|
|
- }
|
|
|
+ console.log("a: ", components);
|
|
|
+ console.log("b: ", get().components);
|
|
|
+
|
|
|
// 普通更新或新建
|
|
|
- const res = await _save(c);
|
|
|
- if (res.code !== 0) {
|
|
|
- return null;
|
|
|
+ const {status} = await editAgentWebsite(agentId, {components: components});
|
|
|
+ if(isSuccess(status)){
|
|
|
+ set({
|
|
|
+ components
|
|
|
+ })
|
|
|
+ return true
|
|
|
}
|
|
|
- set((state) => {
|
|
|
- let _components = state.components;
|
|
|
- if (c.id) {
|
|
|
- _components = _components.filter(
|
|
|
- (item: TComponentItem) => item?.id !== c.id
|
|
|
- );
|
|
|
- }
|
|
|
- let sorted = [..._components, res.data].sort(sortComponentsByIndex);
|
|
|
- state.calcMaxIndex(sorted);
|
|
|
- return {
|
|
|
- components: sorted,
|
|
|
- loading: false,
|
|
|
- };
|
|
|
- });
|
|
|
-
|
|
|
- return res?.data;
|
|
|
+ return false
|
|
|
+
|
|
|
},
|
|
|
setInsertIndex: (index) => {
|
|
|
console.log(index,' setInsertIndex')
|