Forrest.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. import APIRepository from '../classes/APIRepository'
  2. import { appApi } from 'src/boot/axios'
  3. import { TreeNode, TreeNodeList } from 'src/models/TreeNode.js'
  4. const APIAdresses = {
  5. base: '/forrest/tree',
  6. bulkIndex: '/forrest/tree/bulk',
  7. tags: '/forrest/tags',
  8. grid: (grid) => '/forrest/tree/' + grid,
  9. getMultiType: (types) => {
  10. let treeAddress = '/forrest/tree?'
  11. types.forEach(element => {
  12. treeAddress = treeAddress + `multi-type[]=${element}&`
  13. })
  14. return treeAddress
  15. },
  16. getGradesList: '/forrest/tree?type=test',
  17. getNodeById(nodeId) {
  18. return '/forrest/tree/' + nodeId
  19. },
  20. getNodeByType(nodeType) {
  21. return '/forrest/tree?type=' + nodeType
  22. },
  23. getNodeByTitle(nodeType) {
  24. return '/forrest/tree?title=' + nodeType
  25. },
  26. editNode(id) {
  27. return '/forrest/tree/' + id
  28. },
  29. getLessonList(lessonId) {
  30. return '/forrest/tree/' + lessonId
  31. }
  32. }
  33. export default class ForrestAPI extends APIRepository {
  34. constructor() {
  35. super('tree', appApi, '', '', APIAdresses)
  36. this.CacheList = {
  37. base: this.name + this.APIAdresses.base,
  38. bulkIndex: this.name + this.APIAdresses.bulkIndex,
  39. tags: this.name + this.APIAdresses.tags,
  40. grid: (grid) => this.name + this.APIAdresses.grid(grid),
  41. getMultiType: (grid) => this.name + this.APIAdresses.getMultiType(grid),
  42. getGradesList: this.name + this.APIAdresses.getGradesList,
  43. getNodeById: nodeId => this.name + this.APIAdresses.getNodeById(nodeId),
  44. getNodeByType: nodeType => this.name + this.APIAdresses.getNodeByType(nodeType),
  45. getNodeByTitle: nodeTitle => this.name + this.APIAdresses.getNodeByTitle(nodeTitle),
  46. editNode: id => this.name + this.APIAdresses.editNode(id),
  47. getLessonList: id => this.name + this.APIAdresses.getLessonList(id)
  48. }
  49. }
  50. index(data) {
  51. return this.sendRequest({
  52. apiMethod: 'get',
  53. api: this.api,
  54. request: this.APIAdresses.base,
  55. data: this.getNormalizedSendData({
  56. page: 1 // Number
  57. }, data),
  58. resolveCallback: (response) => {
  59. return {
  60. treeNodeList: new TreeNodeList(response.data.data),
  61. paginate: response.data.meta
  62. // {
  63. // current_page: 1,
  64. // from: 1,
  65. // last_page: 1,
  66. // path: '...',
  67. // per_page: 15,
  68. // to: 10,
  69. // total: 10
  70. // }
  71. }
  72. },
  73. rejectCallback: (error) => {
  74. return error
  75. }
  76. })
  77. }
  78. base(data = {}) {
  79. return this.sendRequest({
  80. apiMethod: 'get',
  81. api: this.api,
  82. request: this.APIAdresses.base,
  83. cacheKey: this.CacheList.base,
  84. ...(data?.cache && { cache: data.cache }),
  85. resolveCallback: (response) => {
  86. return new TreeNode(response.data.data)
  87. },
  88. rejectCallback: (error) => {
  89. return error
  90. }
  91. })
  92. }
  93. createNode(data = {}) {
  94. return this.sendRequest({
  95. apiMethod: 'post',
  96. api: this.api,
  97. request: this.APIAdresses.base,
  98. cacheKey: this.CacheList.base,
  99. data: data.data,
  100. ...(data?.cache && { cache: data.cache }),
  101. resolveCallback: (response) => {
  102. return new TreeNode(response.data.data)
  103. },
  104. rejectCallback: (error) => {
  105. return error
  106. }
  107. })
  108. }
  109. getNodeBy(value, data) {
  110. const methodName = 'getNodeBy' + value
  111. let param = data.data.nodeType
  112. if (value === 'Id') {
  113. param = data.data.id
  114. }
  115. return this.sendRequest({
  116. apiMethod: 'get',
  117. api: this.api,
  118. request: this.APIAdresses[methodName](param),
  119. cacheKey: this.CacheList[methodName](param),
  120. ...(data?.cache && { cache: data.cache }),
  121. resolveCallback: (response) => {
  122. return new TreeNode(response.data.data)
  123. },
  124. rejectCallback: (error) => {
  125. return error
  126. }
  127. })
  128. }
  129. getNodeById(data) {
  130. return this.getNodeBy('Id', data)
  131. }
  132. getNodeByType(data) {
  133. return this.getNodeBy('Type', data)
  134. }
  135. getNodeByTitle(data) {
  136. return this.getNodeBy('Title', data)
  137. }
  138. editNode(nodeId, data) {
  139. return this.sendRequest({
  140. apiMethod: 'put',
  141. api: this.api,
  142. request: this.APIAdresses.editNode(nodeId),
  143. cacheKey: this.CacheList.editNode(nodeId),
  144. ...(data?.cache && { cache: data.cache }),
  145. data: data.data,
  146. resolveCallback: (response) => {
  147. return new TreeNode(response.data.data)
  148. },
  149. rejectCallback: (error) => {
  150. return error
  151. }
  152. })
  153. }
  154. getGradesList(data = {}) {
  155. return this.sendRequest({
  156. apiMethod: 'get',
  157. api: this.api,
  158. request: this.APIAdresses.getGradesList,
  159. cacheKey: this.CacheList.getGradesList,
  160. ...(data?.cache && { cache: data.cache }),
  161. resolveCallback: (response) => {
  162. return response.data.data.children
  163. },
  164. rejectCallback: (error) => {
  165. return error
  166. }
  167. })
  168. }
  169. getLessonList(data = {}) {
  170. return this.sendRequest({
  171. apiMethod: 'get',
  172. api: this.api,
  173. request: this.APIAdresses.getLessonList(data.data.id),
  174. cacheKey: this.CacheList.getLessonList(data.data.id),
  175. ...(data?.cache && { cache: data.cache }),
  176. resolveCallback: (response) => {
  177. return response.data.data.children
  178. },
  179. rejectCallback: (error) => {
  180. return error
  181. }
  182. })
  183. }
  184. getTags(data = {}, cache) {
  185. return this.sendRequest({
  186. apiMethod: 'get',
  187. api: this.api,
  188. request: this.APIAdresses.getMultiType(data),
  189. cacheKey: this.CacheList.getMultiType(data),
  190. ...(cache && { cache }),
  191. resolveCallback: (response) => {
  192. return response.data.data
  193. // return fillFakeData()
  194. },
  195. rejectCallback: (error) => {
  196. return error
  197. // return fillFakeData()
  198. }
  199. })
  200. }
  201. }
  202. // function fillFakeData() {
  203. // const treeNodes = [
  204. // {
  205. // children: [
  206. // {
  207. // id: '6281fbeb2f1bafe99f050336',
  208. // title: 'پایه دوازدهم',
  209. // parent: {
  210. // id: '6281fb9e2f1bafe99f050334',
  211. // title: 'درخت دانش'
  212. // },
  213. // ancestors: [
  214. // {
  215. // id: '6281fb9e2f1bafe99f050334',
  216. // title: 'درخت دانش'
  217. // }
  218. // ],
  219. // order: 1,
  220. // type: null,
  221. // number_of_children: 20,
  222. // updated_at: '2022-06-06 12:54:01',
  223. // created_at: '2022-05-16 11:53:23'
  224. // },
  225. // {
  226. // id: '628204312f1bafe99f050346',
  227. // title: 'پایه یازدهم',
  228. // parent: {
  229. // id: '6281fb9e2f1bafe99f050334',
  230. // title: 'درخت دانش'
  231. // },
  232. // ancestors: [
  233. // {
  234. // id: '6281fb9e2f1bafe99f050334',
  235. // title: 'درخت دانش'
  236. // }
  237. // ],
  238. // order: '1',
  239. // type: null,
  240. // number_of_children: 22,
  241. // updated_at: '2022-06-06 12:55:19',
  242. // created_at: '2022-05-16 12:28:41'
  243. // },
  244. // {
  245. // id: '62947d2624d62d858d09a4d2',
  246. // title: 'پایه دهم',
  247. // parent: {
  248. // id: '6281fb9e2f1bafe99f050334',
  249. // title: 'درخت دانش'
  250. // },
  251. // ancestors: [
  252. // {
  253. // id: '6281fb9e2f1bafe99f050334',
  254. // title: 'درخت دانش'
  255. // }
  256. // ],
  257. // order: 1,
  258. // type: null,
  259. // number_of_children: 19,
  260. // updated_at: '2022-06-06 12:58:16',
  261. // created_at: '2022-05-30 12:45:34'
  262. // },
  263. // {
  264. // id: '628204c52f1bafe99f05034a',
  265. // title: 'پایه نهم',
  266. // parent: {
  267. // id: '6281fb9e2f1bafe99f050334',
  268. // title: 'درخت دانش'
  269. // },
  270. // ancestors: [
  271. // {
  272. // id: '6281fb9e2f1bafe99f050334',
  273. // title: 'درخت دانش'
  274. // }
  275. // ],
  276. // order: '3',
  277. // type: null,
  278. // number_of_children: 8,
  279. // updated_at: '2022-06-06 12:56:52',
  280. // created_at: '2022-05-16 12:31:09'
  281. // },
  282. // {
  283. // id: '6284c10647a54520840442a2',
  284. // title: 'پایه هشتم',
  285. // parent: {
  286. // id: '6281fb9e2f1bafe99f050334',
  287. // title: 'درخت دانش'
  288. // },
  289. // ancestors: [
  290. // {
  291. // id: '6281fb9e2f1bafe99f050334',
  292. // title: 'درخت دانش'
  293. // }
  294. // ],
  295. // order: '4',
  296. // type: null,
  297. // number_of_children: 7,
  298. // updated_at: '2022-06-06 12:56:59',
  299. // created_at: '2022-05-18 14:18:54'
  300. // },
  301. // {
  302. // id: '629dba608c4d7e26720cece3',
  303. // title: 'پایه هفتم',
  304. // parent: {
  305. // id: '6281fb9e2f1bafe99f050334',
  306. // title: 'درخت دانش'
  307. // },
  308. // ancestors: [
  309. // {
  310. // id: '6281fb9e2f1bafe99f050334',
  311. // title: 'درخت دانش'
  312. // }
  313. // ],
  314. // order: '4',
  315. // type: null,
  316. // number_of_children: 7,
  317. // updated_at: '2022-06-06 12:57:12',
  318. // created_at: '2022-06-06 12:57:12'
  319. // },
  320. // {
  321. // id: '629dba888c4d7e26720cece5',
  322. // title: 'پایه ششم',
  323. // parent: {
  324. // id: '6281fb9e2f1bafe99f050334',
  325. // title: 'درخت دانش'
  326. // },
  327. // ancestors: [
  328. // {
  329. // id: '6281fb9e2f1bafe99f050334',
  330. // title: 'درخت دانش'
  331. // }
  332. // ],
  333. // order: '7',
  334. // type: null,
  335. // number_of_children: 6,
  336. // updated_at: '2022-06-06 12:57:52',
  337. // created_at: '2022-06-06 12:57:52'
  338. // },
  339. // {
  340. // id: '629dc3b58c4d7e26720ced28',
  341. // title: 'پایه پنجم',
  342. // parent: {
  343. // id: '6281fb9e2f1bafe99f050334',
  344. // title: 'درخت دانش'
  345. // },
  346. // ancestors: [
  347. // {
  348. // id: '6281fb9e2f1bafe99f050334',
  349. // title: 'درخت دانش'
  350. // }
  351. // ],
  352. // order: '8',
  353. // type: null,
  354. // number_of_children: 5,
  355. // updated_at: '2022-06-06 13:37:01',
  356. // created_at: '2022-06-06 13:37:01'
  357. // },
  358. // {
  359. // id: '62f8eb444dcdcce15d084e62',
  360. // title: 'پایه چهارم',
  361. // parent: {
  362. // id: '6281fb9e2f1bafe99f050334',
  363. // title: 'درخت دانش'
  364. // },
  365. // ancestors: [
  366. // {
  367. // id: '6281fb9e2f1bafe99f050334',
  368. // title: 'درخت دانش'
  369. // }
  370. // ],
  371. // order: '9',
  372. // type: null,
  373. // number_of_children: 0,
  374. // updated_at: '2022-08-23 17:26:06',
  375. // created_at: '2022-08-14 17:02:04'
  376. // }
  377. // ]
  378. // },
  379. // {
  380. // children: [
  381. // {
  382. // id: '63ff427566344faf860f0f9f',
  383. // title: 'دبیر 1',
  384. // parent: {
  385. // id: '63f37272c590054efc012d12',
  386. // title: 'دبیر'
  387. // },
  388. // ancestors: [
  389. // {
  390. // id: '63f37272c590054efc012d12',
  391. // title: 'دبیر'
  392. // }
  393. // ],
  394. // order: '0',
  395. // type: null,
  396. // number_of_children: 0,
  397. // updated_at: '2023-03-01 15:49:18',
  398. // created_at: '2023-03-01 15:47:57'
  399. // },
  400. // {
  401. // id: '63ff427c66344faf860f0fa0',
  402. // title: 'دبیر 2',
  403. // parent: {
  404. // id: '63f37272c590054efc012d12',
  405. // title: 'دبیر'
  406. // },
  407. // ancestors: [
  408. // {
  409. // id: '63f37272c590054efc012d12',
  410. // title: 'دبیر'
  411. // }
  412. // ],
  413. // order: '2',
  414. // type: null,
  415. // number_of_children: 1,
  416. // updated_at: '2023-03-01 15:49:18',
  417. // created_at: '2023-03-01 15:48:04'
  418. // },
  419. // {
  420. // id: '63ff429866344faf860f0fa2',
  421. // title: 'دبیر 3',
  422. // parent: {
  423. // id: '63f37272c590054efc012d12',
  424. // title: 'دبیر'
  425. // },
  426. // ancestors: [
  427. // {
  428. // id: '63f37272c590054efc012d12',
  429. // title: 'دبیر'
  430. // }
  431. // ],
  432. // order: '3',
  433. // type: null,
  434. // number_of_children: 0,
  435. // updated_at: '2023-03-01 15:49:18',
  436. // created_at: '2023-03-01 15:48:32'
  437. // },
  438. // {
  439. // id: '63ff42b466344faf860f0fa3',
  440. // title: 'دبیر 4',
  441. // parent: {
  442. // id: '63f37272c590054efc012d12',
  443. // title: 'دبیر'
  444. // },
  445. // ancestors: [
  446. // {
  447. // id: '63f37272c590054efc012d12',
  448. // title: 'دبیر'
  449. // }
  450. // ],
  451. // order: '4',
  452. // type: null,
  453. // number_of_children: 0,
  454. // updated_at: '2023-03-01 15:49:18',
  455. // created_at: '2023-03-01 15:49:00'
  456. // }
  457. // ]
  458. // }
  459. // ]
  460. // return treeNodes
  461. // }