Projekt

Obecné

Profil

Stáhnout (2.87 KB) Statistiky
| Větev: | Tag: | Revize:
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
    const [selectedTag, setSelectedTag] = useState<TagInfo | null>(null);
86
    const [selectedSubTag, setSelectedSubTag] = useState<SubTagInfo | null>(null);
87

    
88
    /**
89
     * Selects a tag.
90
     */
91
    const selectTag = (tag: TagInfo, subTag: SubTagInfo | null) => {
92
        setSelectedTag(tag);
93
        setSelectedSubTag(subTag);
94
    };
95

    
96
    /**
97
     * Initializes the context.
98
     */
99
    useEffect(() => {
100
        tagController.tagsGet().then((tagTree) => {
101
            if (typeof tagTree.data.tagCategories != 'undefined') {
102
                setTagCategories(tagTree.data.tagCategories);
103
            }
104
        });
105
    }, []);
106

    
107
    return (
108
        <TagCategoryContext.Provider
109
            value={{
110
                tagCategories,
111
                selectedTag,
112
                selectedSubTag,
113
                setTagCategories,
114
                selectTag,
115
            }}
116
        >
117
            {props.children}
118
        </TagCategoryContext.Provider>
119
    );
120
};
121

    
122
export default TagCategoryProvider;
(3-3/3)