ywana-core8 0.0.695 → 0.0.697

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.695",
3
+ "version": "0.0.697",
4
4
  "description": "ywana-core8",
5
5
  "homepage": "https://ywana.github.io/workspace",
6
6
  "author": "Ernesto Roldan Garcia",
package/src/html/table.js CHANGED
@@ -164,6 +164,8 @@ const DataTableCell = ({ index, row, column, cell, editable }) => {
164
164
 
165
165
  const render = (type) => {
166
166
  const { id, disabled = false, min, max, onChange, format, options, item } = column
167
+
168
+
167
169
  if (id === "checked") {
168
170
  return <CheckBox id={id} value={cell} onChange={(id, value) => onChange(row.id, id, value)} />
169
171
  } else if (editable && onChange) {
@@ -0,0 +1,45 @@
1
+ import { useContext, useState } from "react"
2
+ import { Button } from "../html"
3
+ import { TaskContext, TaskContextProvider, TaskMonitor } from "./task"
4
+
5
+ const CreateTaskButton = (props) => {
6
+
7
+ const taskContext = useContext(TaskContext)
8
+
9
+ async function run() {
10
+
11
+ const task = await taskContext.createTask({
12
+ 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
+ })
19
+ }
20
+
21
+ return (
22
+ <Button label="NEW TASK" action={run} />
23
+ )
24
+ }
25
+
26
+
27
+ const TaskTest = (props) => {
28
+
29
+ const [events, setEvents] = useState([])
30
+
31
+ function addEvent(event) {
32
+ setEvents([...events, event])
33
+ }
34
+
35
+ return (
36
+ <TaskContextProvider host="http://localhost:3001">
37
+ <TaskMonitor />
38
+ <CreateTaskButton onSuccess={addEvent}/>
39
+ {events.map((event, index) => (
40
+ <div key={index}>{event}</div>
41
+ ))}
42
+ </TaskContextProvider>
43
+
44
+ )
45
+ }
@@ -1,5 +1,6 @@
1
1
  import React, { useEffect, useContext, useState } from 'react'
2
2
  import { CollectionAPI } from '../domain/CollectionAPI'
3
+ import { FORMATS, TYPES } from '../domain/ContentType'
3
4
  import { LinearProgress, Header, DataTable, Icon } from '../html'
4
5
  import "./task.css"
5
6
 
@@ -12,9 +13,10 @@ export const TaskContext = React.createContext({})
12
13
  * Task Provider
13
14
  */
14
15
  export const TaskContextProvider = (props) => {
15
-
16
- const { host, url = "/tasks", children } = props
16
+
17
+ const { host, url = "/tasks", frequency = 5000, children } = props
17
18
  const API = CollectionAPI(url, host)
19
+ const [listeners, setListeners] = useState([])
18
20
 
19
21
  async function tasks(filters, likes) {
20
22
  try {
@@ -34,9 +36,10 @@ export const TaskContextProvider = (props) => {
34
36
  }
35
37
  }
36
38
 
37
- async function createTask(task) {
39
+ async function createTask(task, listener) {
38
40
  try {
39
41
  const response = await API.create(task);
42
+ if (listener) addListener(response.id, listener)
40
43
  return response;
41
44
  } catch (error) {
42
45
  console.log("createTask error", error);
@@ -45,6 +48,7 @@ export const TaskContextProvider = (props) => {
45
48
 
46
49
  async function removeTask(id) {
47
50
  try {
51
+ removeTaskListeners(id)
48
52
  const response = await API.remove(id);
49
53
  return response;
50
54
  } catch (error) {
@@ -52,11 +56,36 @@ export const TaskContextProvider = (props) => {
52
56
  }
53
57
  }
54
58
 
59
+ function addListener(taskId, listener) {
60
+ const listenerId = listeners.length
61
+ listeners.push({ taskId, listener })
62
+ return listenerId
63
+ }
64
+
65
+ function removeListener(taskId) {
66
+ const newListeners = listeners.filter(listener => listener.taskId !== taskId)
67
+ setListeners(newListeners)
68
+ }
69
+
70
+ useEffect(() => {
71
+ const interval = setInterval(async () => {
72
+ const tasks = await API.all()
73
+ listeners.forEach(({ taskId, listener }) => {
74
+ const task = tasks.find(task => task.id === taskId)
75
+ if (task) listener(task)
76
+ })
77
+ }, frequency)
78
+ return () => clearInterval(interval)
79
+ }, [])
80
+
81
+
55
82
  const value = {
56
83
  tasks,
57
84
  task,
58
85
  createTask,
59
86
  removeTask,
87
+ addListener,
88
+ removeListener
60
89
  };
61
90
 
62
91
  return (
@@ -129,8 +158,8 @@ export const TaskMonitor = (props) => {
129
158
  { id: "description", label: "Descripcion" },
130
159
  { id: "progress", label: "%" },
131
160
  { id: "percentage", label: "" },
132
- { id: "init", label: "Inicio" },
133
- { id: "end", label: "Fin" },
161
+ { id: "init", label: "Inicio", type: TYPES.STRING, format: FORMATS.DATE },
162
+ { id: "end", label: "Fin", type: TYPES.STRING, format: FORMATS.DATE },
134
163
  { id: "resourceID", label: "Recurso" },
135
164
  { id: "owner", label: "Propietario" },
136
165
  { id: "actions", label: "" }