108 lines
2.7 KiB
TypeScript
108 lines
2.7 KiB
TypeScript
'use client';
|
|
import { create } from 'zustand';
|
|
import { query } from '@/modules/query';
|
|
import { toast as message } from 'sonner';
|
|
|
|
export type FlowmeType = {
|
|
id: string;
|
|
uid?: string;
|
|
title: string;
|
|
description: string;
|
|
tags: string[];
|
|
link: string;
|
|
data: Record<string, any>;
|
|
channelId?: string;
|
|
type: string;
|
|
source: string;
|
|
importance: number;
|
|
isArchived: boolean;
|
|
createdAt: string;
|
|
updatedAt: string;
|
|
};
|
|
|
|
type FlowmeStore = {
|
|
showEdit: boolean;
|
|
setShowEdit: (showEdit: boolean) => void;
|
|
formData: Partial<FlowmeType>;
|
|
setFormData: (formData: Partial<FlowmeType>) => void;
|
|
loading: boolean;
|
|
setLoading: (loading: boolean) => void;
|
|
list: FlowmeType[];
|
|
getList: () => Promise<void>;
|
|
updateData: (data: Partial<FlowmeType>) => Promise<any>;
|
|
deleteData: (id: string) => Promise<void>;
|
|
detail: FlowmeType | null;
|
|
setDetail: (detail: FlowmeType | null) => void;
|
|
getDetail: (id: string) => Promise<void>;
|
|
};
|
|
|
|
export const useFlowmeStore = create<FlowmeStore>((set, get) => {
|
|
return {
|
|
showEdit: false,
|
|
setShowEdit: (showEdit) => set({ showEdit }),
|
|
formData: {},
|
|
setFormData: (formData) => set({ formData }),
|
|
loading: false,
|
|
setLoading: (loading) => set({ loading }),
|
|
list: [],
|
|
getList: async () => {
|
|
set({ loading: true });
|
|
const res = await query.post({
|
|
path: 'flowme',
|
|
key: 'list',
|
|
});
|
|
set({ loading: false });
|
|
if (res.code === 200) {
|
|
set({ list: res.data.list });
|
|
} else {
|
|
message.error(res.message || 'Request failed');
|
|
}
|
|
},
|
|
updateData: async (data) => {
|
|
const { getList } = get();
|
|
const res = await query.post({
|
|
path: 'flowme',
|
|
key: 'update',
|
|
data,
|
|
});
|
|
if (res.code === 200) {
|
|
message.success('Success');
|
|
set({ showEdit: false, formData: res.data });
|
|
getList();
|
|
} else {
|
|
message.error(res.message || 'Request failed');
|
|
}
|
|
return res;
|
|
},
|
|
deleteData: async (id) => {
|
|
const { getList } = get();
|
|
const res = await query.post({
|
|
path: 'flowme',
|
|
key: 'delete',
|
|
data: { id },
|
|
});
|
|
if (res.code === 200) {
|
|
getList();
|
|
message.success('Success');
|
|
} else {
|
|
message.error(res.message || 'Request failed');
|
|
}
|
|
},
|
|
detail: null,
|
|
setDetail: (detail) => set({ detail }),
|
|
getDetail: async (id) => {
|
|
set({ detail: null });
|
|
const res = await query.post({
|
|
path: 'flowme',
|
|
key: 'get',
|
|
data: { id },
|
|
});
|
|
if (res.code === 200) {
|
|
set({ detail: res.data });
|
|
} else {
|
|
message.error(res.message || 'Request failed');
|
|
}
|
|
},
|
|
};
|
|
});
|