1
|
import React, { createContext, useEffect, useState } from 'react';
|
2
|
import { TagInfo } from '../api';
|
3
|
|
4
|
/**
|
5
|
* Interface of a tag context provider.
|
6
|
*/
|
7
|
interface ITagContextProvider {
|
8
|
/**
|
9
|
* Tags managed by the context.
|
10
|
*/
|
11
|
tags: TagInfo[] | null;
|
12
|
|
13
|
/**
|
14
|
* Sets new tags.
|
15
|
* @param newTags An array of new tags.
|
16
|
*/
|
17
|
setTags: (newTags: TagInfo[] | null) => void;
|
18
|
}
|
19
|
|
20
|
/**
|
21
|
* The tag context that manages available tags.
|
22
|
*/
|
23
|
export const TagContext = createContext<ITagContextProvider>({
|
24
|
/**
|
25
|
* Default tags.
|
26
|
*/
|
27
|
tags: null,
|
28
|
|
29
|
/**
|
30
|
* Default implementation of setTags method.
|
31
|
* @param v Array of new tags.
|
32
|
*/
|
33
|
setTags: (v) => {
|
34
|
return;
|
35
|
},
|
36
|
});
|
37
|
|
38
|
/**
|
39
|
* Provider of the tag context.
|
40
|
* @param props Children that should have access to the tag context.
|
41
|
* @returns Prepared html of the provider.
|
42
|
*/
|
43
|
const TagProvider = (props: { children: React.ReactNode }) => {
|
44
|
/**
|
45
|
* Tags managed by the context.
|
46
|
*/
|
47
|
const [tags, setTags] = useState<TagInfo[] | null>(null);
|
48
|
|
49
|
/**
|
50
|
* Initializes the context.
|
51
|
*/
|
52
|
useEffect(() => {
|
53
|
//TODO: Implement initialization with values from the server
|
54
|
}, []);
|
55
|
|
56
|
return (
|
57
|
<TagContext.Provider
|
58
|
value={{
|
59
|
tags,
|
60
|
setTags,
|
61
|
}}
|
62
|
>
|
63
|
{props.children}
|
64
|
</TagContext.Provider>
|
65
|
);
|
66
|
};
|
67
|
|
68
|
export default TagProvider;
|