Projekt

Obecné

Profil

Stáhnout (2.95 KB) Statistiky
| Větev: | Tag: | Revize:
1 b8cae450 Dominik Poch
import React, { createContext, useEffect, useState } from 'react';
2 ab68c8da Dominik Poch
import { SubTagInfo, TagCategoryInfo, TagInfo } from '../api';
3 b8cae450 Dominik Poch
import { tagController } from '../controllers';
4
5
/**
6
 * Interface of a tag context provider.
7
 */
8
interface ITagCategoryContextProvider {
9
    /**
10 ab68c8da Dominik Poch
     * Categories managed by the context.
11 b8cae450 Dominik Poch
     */
12
    tagCategories: TagCategoryInfo[] | null;
13
14 ab68c8da Dominik Poch
    /**
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 b8cae450 Dominik Poch
    /**
25
     * Sets new tags.
26
     * @param newTags An array of new tags.
27
     */
28
    setTagCategories: (newTagCategories: TagCategoryInfo[] | null) => void;
29 ab68c8da Dominik Poch
30
    /**
31
     * Selects a tag.
32
     * @param tag Tag.
33
     * @param subTag Sub tag.
34
     */
35
    selectTag: (tag: TagInfo, subTag: SubTagInfo | null) => void;
36 b8cae450 Dominik Poch
}
37
38
/**
39
 * The tag context that manages available tags.
40
 */
41
export const TagCategoryContext = createContext<ITagCategoryContextProvider>({
42
    /**
43 ab68c8da Dominik Poch
     * Default categories.
44 b8cae450 Dominik Poch
     */
45
    tagCategories: null,
46
47 ab68c8da Dominik Poch
    /**
48
     * Default selected tag.
49
     */
50
    selectedTag: null,
51
52
    /**
53
     * Default selected sub tag.
54
     */
55
    selectedSubTag: null,
56
57 b8cae450 Dominik Poch
    /**
58
     * Default implementation of setTags method.
59 ab68c8da Dominik Poch
     * @param c Array of new tags.
60 b8cae450 Dominik Poch
     */
61
    setTagCategories: (c) => {
62
        return;
63
    },
64 ab68c8da Dominik Poch
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 b8cae450 Dominik Poch
});
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 0441c314 Dominik Poch
86
    /**
87
     * Selected tag.
88
     */
89 ab68c8da Dominik Poch
    const [selectedTag, setSelectedTag] = useState<TagInfo | null>(null);
90 0441c314 Dominik Poch
91
    /**
92
     * Selected sub tag.
93
     */
94 ab68c8da Dominik Poch
    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 b8cae450 Dominik Poch
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 ab68c8da Dominik Poch
                selectedTag,
120
                selectedSubTag,
121 b8cae450 Dominik Poch
                setTagCategories,
122 ab68c8da Dominik Poch
                selectTag,
123 b8cae450 Dominik Poch
            }}
124
        >
125
            {props.children}
126
        </TagCategoryContext.Provider>
127
    );
128
};
129
130
export default TagCategoryProvider;