Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

model.ts 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { BaseState } from '@/interfaces/common';
  2. import { IKnowledgeFile } from '@/interfaces/database/knowledge';
  3. import kbService from '@/services/kbService';
  4. import { message } from 'antd';
  5. import { pick } from 'lodash';
  6. // import { delay } from '@/utils/storeUtil';
  7. import { DvaModel } from 'umi';
  8. export interface ChunkModelState extends BaseState {
  9. data: any[];
  10. total: number;
  11. isShowCreateModal: boolean;
  12. chunk_id: string;
  13. doc_id: string;
  14. chunkInfo: any;
  15. documentInfo: IKnowledgeFile;
  16. available?: number;
  17. }
  18. const model: DvaModel<ChunkModelState> = {
  19. namespace: 'chunkModel',
  20. state: {
  21. data: [],
  22. total: 0,
  23. isShowCreateModal: false,
  24. chunk_id: '',
  25. doc_id: '',
  26. chunkInfo: {},
  27. documentInfo: {} as IKnowledgeFile,
  28. pagination: {
  29. current: 1,
  30. pageSize: 10,
  31. },
  32. searchString: '',
  33. available: undefined, // set to undefined to select all
  34. },
  35. reducers: {
  36. updateState(state, { payload }) {
  37. return {
  38. ...state,
  39. ...payload,
  40. };
  41. },
  42. setIsShowCreateModal(state, { payload }) {
  43. return {
  44. ...state,
  45. isShowCreateModal:
  46. typeof payload === 'boolean' ? payload : !state.isShowCreateModal,
  47. };
  48. },
  49. setAvailable(state, { payload }) {
  50. return { ...state, available: payload };
  51. },
  52. setSearchString(state, { payload }) {
  53. return { ...state, searchString: payload };
  54. },
  55. setPagination(state, { payload }) {
  56. return { ...state, pagination: { ...state.pagination, ...payload } };
  57. },
  58. resetFilter(state, {}) {
  59. return {
  60. ...state,
  61. pagination: {
  62. current: 1,
  63. pageSize: 10,
  64. },
  65. searchString: '',
  66. available: undefined,
  67. };
  68. },
  69. },
  70. effects: {
  71. *chunk_list({ payload = {} }, { call, put, select }) {
  72. const { available, searchString, pagination }: ChunkModelState =
  73. yield select((state: any) => state.chunkModel);
  74. const { data } = yield call(kbService.chunk_list, {
  75. ...payload,
  76. available_int: available,
  77. keywords: searchString,
  78. page: pagination.current,
  79. size: pagination.pageSize,
  80. });
  81. const { retcode, data: res } = data;
  82. if (retcode === 0) {
  83. yield put({
  84. type: 'updateState',
  85. payload: {
  86. data: res.chunks,
  87. total: res.total,
  88. documentInfo: res.doc,
  89. },
  90. });
  91. }
  92. },
  93. throttledGetChunkList: [
  94. function* ({ payload }, { put }) {
  95. yield put({ type: 'chunk_list', payload: { doc_id: payload } });
  96. },
  97. { type: 'throttle', ms: 1000 }, // TODO: Provide type support for this effect
  98. ],
  99. *switch_chunk({ payload = {} }, { call }) {
  100. const { data } = yield call(kbService.switch_chunk, payload);
  101. const { retcode } = data;
  102. if (retcode === 0) {
  103. message.success('Modified successfully !');
  104. }
  105. return retcode;
  106. },
  107. *rm_chunk({ payload = {} }, { call, put }) {
  108. const { data } = yield call(kbService.rm_chunk, payload);
  109. const { retcode } = data;
  110. if (retcode === 0) {
  111. yield put({
  112. type: 'setIsShowCreateModal',
  113. payload: false,
  114. });
  115. yield put({ type: 'setPagination', payload: { current: 1 } });
  116. yield put({ type: 'chunk_list', payload: pick(payload, ['doc_id']) });
  117. }
  118. return retcode;
  119. },
  120. *get_chunk({ payload = {} }, { call, put }) {
  121. const { data } = yield call(kbService.get_chunk, payload);
  122. const { retcode, data: res } = data;
  123. if (retcode === 0) {
  124. yield put({
  125. type: 'updateState',
  126. payload: {
  127. chunkInfo: res,
  128. },
  129. });
  130. }
  131. return data;
  132. },
  133. *create_chunk({ payload = {} }, { call, put }) {
  134. let service = kbService.create_chunk;
  135. if (payload.chunk_id) {
  136. service = kbService.set_chunk;
  137. }
  138. const { data } = yield call(service, payload);
  139. const { retcode } = data;
  140. if (retcode === 0) {
  141. yield put({
  142. type: 'setIsShowCreateModal',
  143. payload: false,
  144. });
  145. yield put({ type: 'chunk_list', payload: pick(payload, ['doc_id']) });
  146. }
  147. },
  148. },
  149. };
  150. export default model;