Projekt

Obecné

Profil

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