ywana-core8 0.0.768 → 0.0.770

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ywana-core8",
3
- "version": "0.0.768",
3
+ "version": "0.0.770",
4
4
  "description": "ywana-core8",
5
5
  "homepage": "https://ywana.github.io/workspace",
6
6
  "author": "Ernesto Roldan Garcia",
@@ -19,8 +19,21 @@ export const CollectionContextProvider = (props) => {
19
19
  const [selected, setSelected] = useState(null)
20
20
 
21
21
  useEffect(() => {
22
+
22
23
  console.log("CONTEXT LOAD for Filters", filters, customFilters)
23
- load()
24
+ let mounted = true;
25
+
26
+ const callLoad = async () => {
27
+ const data = await load()
28
+ if (mounted) setAll(data)
29
+ }
30
+ callLoad()
31
+
32
+ return () => {
33
+ console.log("CONTEXT UNMOUNT", filters, customFilters)
34
+ mounted = false;
35
+ }
36
+
24
37
  }, [filters, customFilters])
25
38
 
26
39
  async function load() {
@@ -35,7 +48,7 @@ export const CollectionContextProvider = (props) => {
35
48
  const response = await API.all(filters, likes, page);
36
49
  const next = field ? response[field] : response;
37
50
  const data = runCustomFilters(next)
38
- setAll(data)
51
+ return data
39
52
  } catch (error) {
40
53
  console.log(error)
41
54
  }
package/src/html/tab.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import React, { useState } from 'react'
2
2
  import { Fragment } from 'react'
3
+ import { Icon } from '../html'
3
4
  import './tab.css'
4
5
 
5
6
  /**
@@ -35,7 +36,7 @@ export const Tabs = (props) => {
35
36
  */
36
37
  export const Tab = (props) => {
37
38
 
38
- const { id, label, selected, actions, onSelect } = props
39
+ const { id, icon, label, selected, actions, onSelect } = props
39
40
 
40
41
  function select() {
41
42
  if (onSelect) onSelect(id)
@@ -44,6 +45,7 @@ export const Tab = (props) => {
44
45
  const style = selected ? "selected" : ""
45
46
  return (
46
47
  <div className={`tab ${style}`} onClick={select}>
48
+ {icon ? <Icon icon={icon} size="small" /> : null}
47
49
  {label}
48
50
  {actions ? actions : null}
49
51
  </div>
@@ -2,20 +2,43 @@ import { useContext, useState } from "react"
2
2
  import { Button } from "../html"
3
3
  import { TaskContext, TaskContextProvider, TaskMonitor } from "./task"
4
4
 
5
+ /**
6
+ * Task Example
7
+ *
8
+ const task = {
9
+ id: "633eb091b8e64e79c9219091",
10
+ name: "REMOVE_SPOTCOLOR",
11
+ owner: "633de6e1b8e64e79c9219089",
12
+ description: "Remove selected Spot Colors",
13
+ body: {
14
+ resource: "JOBS",
15
+ resourceID: "633de6e1b8e64e79c9219089",
16
+ // ...
17
+ },
18
+ init: "2022-10-06T10:40:17.680Z",
19
+ end: "2022-10-06T10:40:33.439Z",
20
+ percentage: 100,
21
+ state: "COMPLETED", // CREATED, RUNNING, COMPLETED, FAULTED, CANCELED
22
+ result: {
23
+ status: "OK",
24
+ message: "Spot Colors removed",
25
+ data: {
26
+ // ...
27
+ }
28
+ }
29
+ }
30
+
31
+ **/
32
+
5
33
  const CreateTaskButton = (props) => {
6
34
 
7
35
  const taskContext = useContext(TaskContext)
8
36
 
9
37
  async function run() {
10
-
11
- const task = await taskContext.createTask({
38
+ taskContext.createTask({
39
+ name: "TEST",
12
40
  description: "Test Task " + Date.now(),
13
- init: new Date().toString(),
14
- })
15
-
16
- taskContext.addListener(task.id, (task) => {
17
- console.log(task.description + " " + task.state)
18
- taskContext.removeListener(task.id)
41
+ result: "OK",
19
42
  })
20
43
  }
21
44
 
@@ -27,11 +50,21 @@ const CreateTaskButton = (props) => {
27
50
 
28
51
  const TaskTest = (props) => {
29
52
 
53
+ const taskListeners = {
54
+ "TEST": (task, taskContext) => {
55
+ console.log("LISTENER FOR TASK: ", task)
56
+ taskContext.updateTask(Object.assign({}, task, { percentage: task.percentage + 1 }))
57
+ }
58
+ }
59
+
60
+ const taskEditors = {
61
+ "TEST": (task) => <div>{task.description} {task.state}</div>,
62
+ }
63
+
30
64
  return (
31
- <TaskContextProvider host="http://localhost:3001">
32
- <TaskMonitor />
65
+ <TaskContextProvider host="http://localhost:3001" listeners={taskListeners} >
66
+ <TaskMonitor editors={taskEditors}/>
33
67
  <CreateTaskButton />
34
68
  </TaskContextProvider>
35
-
36
69
  )
37
70
  }
@@ -13,10 +13,10 @@ export const TaskContext = React.createContext({})
13
13
  * Task Provider
14
14
  */
15
15
  export const TaskContextProvider = (props) => {
16
-
17
- const { host, url = "/tasks", frequency = 5000, children } = props
16
+
17
+ const { host, url = "/tasks", frequency = 5000, children, listeners = {} } = props
18
18
  const API = CollectionAPI(url, host)
19
- const listeners = []
19
+ const _listeners = Object.assign({}, listeners)
20
20
 
21
21
  async function tasks(filters, likes) {
22
22
  try {
@@ -39,54 +39,58 @@ export const TaskContextProvider = (props) => {
39
39
  async function createTask(task, listener) {
40
40
  try {
41
41
  const response = await API.create(task);
42
- if (listener) addListener(response.id, listener)
42
+ if (listener) addListener(taskName, listener)
43
43
  return response;
44
44
  } catch (error) {
45
45
  console.log("createTask error", error);
46
46
  }
47
47
  }
48
48
 
49
- async function removeTask(id) {
49
+ async function updateTask(task) {
50
50
  try {
51
- removeListener(id)
52
- const response = await API.remove(id);
51
+ const response = await API.update(task);
53
52
  return response;
54
53
  } catch (error) {
55
- console.log("removeTask error", error);
54
+ console.log("updateTask error", error);
56
55
  }
57
56
  }
58
57
 
59
- function addListener(taskId, listener) {
60
- const listenerId = listeners.length
61
- listeners.push({ taskId, listener })
62
- return listenerId
58
+ async function removeTask(task) {
59
+ try {
60
+ const response = await API.remove(task.id);
61
+ return response;
62
+ } catch (error) {
63
+ console.log("removeTask error", error);
64
+ }
63
65
  }
64
66
 
65
- function removeListener(taskId) {
67
+ function addListener(taskName, listener) {
68
+ _listeners[taskName] = listener
69
+ }
66
70
 
67
- listeners.forEach((listener, index) => {
68
- if (listener.taskId === taskId) {
69
- listeners.splice(index, 1)
70
- }
71
- })
71
+ function removeListener(taskName) {
72
+ delete _listeners[taskName]
72
73
  }
73
74
 
74
75
  useEffect(() => {
75
76
  const interval = setInterval(async () => {
76
77
  const tasks = await API.all()
77
- listeners.forEach(({ taskId, listener }) => {
78
- const task = tasks.find(task => task.id === taskId)
79
- if (task) listener(task)
78
+
79
+ // invoque listeners for each task
80
+ Object.entries(_listeners).forEach(([taskName, listener]) => {
81
+ const listenerTasks = tasks.filter(task => task.name === taskName)
82
+ listenerTasks.forEach(task => listener(task, value))
80
83
  })
84
+
81
85
  }, frequency)
82
86
  return () => clearInterval(interval)
83
87
  }, [])
84
88
 
85
-
86
89
  const value = {
87
90
  tasks,
88
91
  task,
89
92
  createTask,
93
+ updateTask,
90
94
  removeTask,
91
95
  addListener,
92
96
  removeListener
@@ -99,6 +103,17 @@ export const TaskContextProvider = (props) => {
99
103
  )
100
104
  }
101
105
 
106
+ /**
107
+ * Task States
108
+ */
109
+ export const TASK_STATES = {
110
+ "CREATED": "CREATED",
111
+ "RUNNING": "RUNNING",
112
+ "COMPLETED": "COMPPLETED",
113
+ "FAULTED": "FAULTED",
114
+ "CANCELED": "CANCELED"
115
+ }
116
+
102
117
  /**
103
118
  * TaskProgress
104
119
  */
@@ -131,8 +146,7 @@ export const TaskProgress = (props) => {
131
146
  */
132
147
  export const TaskMonitor = (props) => {
133
148
 
134
- const { title = "Task Monitor", filters } = props
135
-
149
+ const { title = "Task Monitor", filters, editors } = props
136
150
  const context = useContext(TaskContext)
137
151
  const [tasks = [], setTasks] = useState([])
138
152
 
@@ -150,7 +164,8 @@ export const TaskMonitor = (props) => {
150
164
  }
151
165
 
152
166
  async function remove(task) {
153
- await context.removeTask(task.id)
167
+ console.log("remove", task)
168
+ await context.removeTask(task)
154
169
  refresh()
155
170
  }
156
171
 
@@ -171,15 +186,16 @@ export const TaskMonitor = (props) => {
171
186
  rows: tasks.map(task => {
172
187
  return {
173
188
  id: task.id,
174
- resourceID: task.resourceID,
189
+ state: task.state,
190
+ description: task.description,
191
+ progress: <LinearProgress progress={task.percentage} />,
192
+ percentage: task.percentage,
175
193
  init: task.init,
176
194
  end: task.end,
195
+ resourceID: task.resourceID,
177
196
  owner: task.owner,
178
- description: task.description,
179
- percentage: task.percentage,
180
- progress: <LinearProgress progress={task.percentage} />,
181
- state: task.state,
182
- actions: <Icon size="small" icon="cancel" clickable action={() => remove(task)} />
197
+ actions: <Icon size="small" icon="cancel" clickable action={() => remove(task)} />,
198
+ info: editors && task.result ? <TaskInfo task={task} editors={editors} /> : null
183
199
  }
184
200
  })
185
201
  }
@@ -193,3 +209,13 @@ export const TaskMonitor = (props) => {
193
209
  </div>
194
210
  )
195
211
  }
212
+
213
+ /**
214
+ * Task Info
215
+ */
216
+ const TaskInfo = (props) => {
217
+ const { task, editors } = props
218
+ const editor = editors ? editors[task.action] : null
219
+ return editor(task)
220
+ }
221
+