1
|
import React, { createContext, useEffect, useState } from 'react';
|
2
|
import { SubTagInfo, TagCategoryInfo, TagInfo } from '../api';
|
3
|
import { tagController } from '../controllers';
|
4
|
|
5
|
/**
|
6
|
* Interface of a tag context provider.
|
7
|
*/
|
8
|
interface ITagCategoryContextProvider {
|
9
|
/**
|
10
|
* Categories managed by the context.
|
11
|
*/
|
12
|
tagCategories: TagCategoryInfo[] | null;
|
13
|
|
14
|
/**
|
15
|
* Selected tag managed by the context.
|
16
|
*/
|
17
|
selectedTag: TagInfo | null;
|
18
|
|
19
|
/**
|
20
|
* Selected sub tag managed by the context.
|
21
|
*/
|
22
|
selectedSubTag: SubTagInfo | null;
|
23
|
|
24
|
/**
|
25
|
* Sets new tags.
|
26
|
* @param newTags An array of new tags.
|
27
|
*/
|
28
|
setTagCategories: (newTagCategories: TagCategoryInfo[] | null) => void;
|
29
|
|
30
|
/**
|
31
|
* Selects a tag.
|
32
|
* @param tag Tag.
|
33
|
* @param subTag Sub tag.
|
34
|
*/
|
35
|
selectTag: (tag: TagInfo, subTag: SubTagInfo | null) => void;
|
36
|
}
|
37
|
|
38
|
/**
|
39
|
* The tag context that manages available tags.
|
40
|
*/
|
41
|
export const TagCategoryContext = createContext<ITagCategoryContextProvider>({
|
42
|
/**
|
43
|
* Default categories.
|
44
|
*/
|
45
|
tagCategories: null,
|
46
|
|
47
|
/**
|
48
|
* Default selected tag.
|
49
|
*/
|
50
|
selectedTag: null,
|
51
|
|
52
|
/**
|
53
|
* Default selected sub tag.
|
54
|
*/
|
55
|
selectedSubTag: null,
|
56
|
|
57
|
/**
|
58
|
* Default implementation of setTags method.
|
59
|
* @param c Array of new tags.
|
60
|
*/
|
61
|
setTagCategories: (c) => {
|
62
|
return;
|
63
|
},
|
64
|
|
65
|
/**
|
66
|
* Default implementation of selectTag.
|
67
|
* @param t Tag.
|
68
|
* @param s Sub tag.
|
69
|
*/
|
70
|
selectTag: (t: TagInfo, s: SubTagInfo | null) => {
|
71
|
return;
|
72
|
},
|
73
|
});
|
74
|
|
75
|
/**
|
76
|
* Provider of the tag context.
|
77
|
* @param props Children that should have access to the tag context.
|
78
|
* @returns Prepared html of the provider.
|
79
|
*/
|
80
|
const TagCategoryProvider = (props: { children: React.ReactNode }) => {
|
81
|
/**
|
82
|
* Tags managed by the context.
|
83
|
*/
|
84
|
const [tagCategories, setTagCategories] = useState<TagCategoryInfo[] | null>(null);
|
85
|
|
86
|
/**
|
87
|
* Selected tag.
|
88
|
*/
|
89
|
const [selectedTag, setSelectedTag] = useState<TagInfo | null>(null);
|
90
|
|
91
|
/**
|
92
|
* Selected sub tag.
|
93
|
*/
|
94
|
const [selectedSubTag, setSelectedSubTag] = useState<SubTagInfo | null>(null);
|
95
|
|
96
|
/**
|
97
|
* Selects a tag.
|
98
|
*/
|
99
|
const selectTag = (tag: TagInfo, subTag: SubTagInfo | null) => {
|
100
|
setSelectedTag(tag);
|
101
|
setSelectedSubTag(subTag);
|
102
|
};
|
103
|
|
104
|
/**
|
105
|
* Initializes the context.
|
106
|
*/
|
107
|
useEffect(() => {
|
108
|
tagController.tagsGet().then((tagTree) => {
|
109
|
if (typeof tagTree.data.tagCategories != 'undefined') {
|
110
|
setTagCategories(tagTree.data.tagCategories);
|
111
|
}
|
112
|
});
|
113
|
}, []);
|
114
|
|
115
|
return (
|
116
|
<TagCategoryContext.Provider
|
117
|
value={{
|
118
|
tagCategories,
|
119
|
selectedTag,
|
120
|
selectedSubTag,
|
121
|
setTagCategories,
|
122
|
selectTag,
|
123
|
}}
|
124
|
>
|
125
|
{props.children}
|
126
|
</TagCategoryContext.Provider>
|
127
|
);
|
128
|
};
|
129
|
|
130
|
export default TagCategoryProvider;
|