@gisce/react-ooui 0.9.2 → 0.9.5

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.
@@ -0,0 +1,141 @@
1
+ import React, { useContext, useState } from "react";
2
+ import { Tags as TagsOoui } from "@gisce/ooui";
3
+ import Field from "@/common/Field";
4
+ import { One2manyItem, One2manyValue } from "../base/one2many/One2manyInput";
5
+ import useDeepCompareEffect from "use-deep-compare-effect";
6
+ import { FormContext, FormContextType } from "@/context/FormContext";
7
+ import ConnectionProvider from "@/ConnectionProvider";
8
+ import { Spin, Alert, Timeline as AntTimeline, Tag as AntTag } from "antd";
9
+ import { readObjectValues } from "@/helpers/one2manyHelper";
10
+
11
+ type TagsProps = {
12
+ ooui: TagsOoui;
13
+ };
14
+
15
+ type TagsInputProps = TagsProps & {
16
+ value?: One2manyValue;
17
+ onChange?: (value: One2manyValue) => void;
18
+ };
19
+
20
+ export const Tags = (props: TagsProps) => {
21
+ const { ooui } = props;
22
+ const { relation, context } = ooui;
23
+ const [isLoading, setIsLoading] = useState<boolean>(true);
24
+ const [error, setError] = useState<string>();
25
+ const formContext = useContext(FormContext) as FormContextType;
26
+ const { getContext } = formContext || {};
27
+
28
+ useDeepCompareEffect(() => {
29
+ fetchData();
30
+ }, [ooui]);
31
+
32
+
33
+ const fetchData = async () => {
34
+ setIsLoading(true);
35
+ setError(undefined);
36
+
37
+ try {
38
+ console.log('Fetching....')
39
+ } catch (err) {
40
+ setError(err as any);
41
+ } finally {
42
+ setIsLoading(false);
43
+ }
44
+ };
45
+
46
+ if (isLoading) {
47
+ return <Spin />;
48
+ }
49
+
50
+ if (error) {
51
+ return <Alert className="mt-10" message={error} type="error" banner />;
52
+ }
53
+
54
+ return (
55
+ <Field type={"array"} {...props}>
56
+ <TagsInput ooui={ooui} />
57
+ </Field>
58
+ );
59
+ };
60
+
61
+ export const TagsInput = (props: TagsInputProps) => {
62
+ const { value, ooui, onChange } = props;
63
+ const { items = [] } = value || {};
64
+
65
+ const [isLoading, setIsLoading] = useState<boolean>(false);
66
+ const [error, setError] = useState<string>();
67
+ const { relation, context, readOnly, field } = ooui;
68
+ const itemsToShow = items.filter(
69
+ (item) => item.values && item.operation !== "pendingRemove"
70
+ );
71
+
72
+ const formContext = useContext(FormContext) as FormContextType;
73
+ const { getContext } = formContext || {};
74
+
75
+ useDeepCompareEffect(() => {
76
+ fetchData();
77
+ }, [items]);
78
+
79
+ const triggerChange = (changedValue: Array<One2manyItem>) => {
80
+ onChange?.({
81
+ items: changedValue,
82
+ });
83
+ };
84
+
85
+ async function fetchData() {
86
+ setIsLoading(true);
87
+ setError(undefined);
88
+
89
+ try {
90
+ const itemsWithValues = await readObjectValues({
91
+ treeFields: [ooui.field],
92
+ formFields: [],
93
+ model: relation,
94
+ items,
95
+ context: { ...getContext?.(), ...context },
96
+ });
97
+
98
+ triggerChange(itemsWithValues);
99
+ } catch (err) {
100
+ setError(err as any);
101
+ } finally {
102
+ setIsLoading(false);
103
+ }
104
+ }
105
+
106
+ if (error) {
107
+ return <Alert className="mt-10" message={error} type="error" banner />;
108
+ }
109
+
110
+ if (isLoading) {
111
+ return <Spin />;
112
+ }
113
+
114
+ const color = (text: string) => {
115
+ let hash = 0;
116
+ for (let i = 0; i < text.length; i++) {
117
+ hash = text.charCodeAt(i) + ((hash << 5) - hash);
118
+ }
119
+ var colour = '#';
120
+ for (let i = 0; i < 3; i++) {
121
+ var value = (hash >> (i * 8)) & 0xFF;
122
+ colour += ('00' + value.toString(16)).substr(-2);
123
+ }
124
+ return colour;
125
+ }
126
+
127
+ return (
128
+ <>
129
+ <div style={{ padding: "1rem" }}>
130
+
131
+ {itemsToShow.map((item, index) => {
132
+ const value = item.values?.[field];
133
+ return (
134
+ <AntTag key={index} color={color(value)}>{value}</AntTag>
135
+ );
136
+ })}
137
+ </div>
138
+
139
+ </>
140
+ );
141
+ };