Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 41e65564

Přidáno uživatelem Dominik Poch před asi 2 roky(ů)

Changed search of tags

Added copy of tags, used contains instead of startsWith and made search case insensitive

Zobrazit rozdíly:

webapp/pages/tags/index.tsx
28 28
    const { logout, role } = useContext(LoggedUserContext);
29 29
    const router = useRouter();
30 30

  
31
    const [tagData, setTagData] = useState<any[] | null>([]);
32

  
31 33
    /**
32 34
     * Data in a table.
33 35
     */
34
    const [tagData, setTagData] = useState<any[] | undefined>([]);
36
    const [shownData, setShownData] = useState<any[] | undefined>([]);
35 37

  
36 38
    // States that says if modal windows should be shown.
37 39
    const [showAddCategoryModal, setShowAddCategoryModal] = useState(false);
......
307 309
    const loadData = () => {
308 310
        tagController.tagsGet().then((tagTree) => {
309 311
            if (typeof tagTree.data.tagCategories != 'undefined') {
310
                let data = tagTree.data.tagCategories?.map((catInfo) => {
311
                    return {
312
                        key: catInfo.id,
313
                        name: catInfo.name,
314
                        description: catInfo.description,
315
                        color: catInfo.color,
316
                        depth: 0,
317
                        disabledForAnnotators: catInfo.disabledForAnnotators,
318
                        ...(catInfo.tags?.length && {
319
                            children: catInfo.tags?.map((tagInfo) => {
320
                                return {
321
                                    key: tagInfo.id,
322
                                    name: tagInfo.name,
323
                                    description: tagInfo.description,
324
                                    color: tagInfo.color,
325
                                    depth: 1,
326
                                    ...(tagInfo.subTags?.length && {
327
                                        children: tagInfo.subTags?.map((subInfo) => {
328
                                            return {
329
                                                key: subInfo.id,
330
                                                name: subInfo.name,
331
                                                description: subInfo.description,
332
                                                depth: 2,
333
                                            };
334
                                        }),
335
                                    }),
336
                                };
337
                            }),
338
                        }),
339
                    };
340
                });
341

  
342
                setTagData(data);
312
                setTagData(tagTree.data.tagCategories);
313
                setShownData(mapData(tagTree.data.tagCategories));
343 314
            }
344 315
        });
345 316
    };
346 317

  
318
    const mapData = (data: any[] | null) => {
319
        return data?.map((catInfo) => {
320
            return {
321
                key: catInfo.id,
322
                name: catInfo.name,
323
                description: catInfo.description,
324
                color: catInfo.color,
325
                depth: 0,
326
                disabledForAnnotators: catInfo.disabledForAnnotators,
327
                ...(catInfo.tags?.length && {
328
                    children: catInfo.tags?.map((tagInfo: any) => {
329
                        return {
330
                            key: tagInfo.id,
331
                            name: tagInfo.name,
332
                            description: tagInfo.description,
333
                            color: tagInfo.color,
334
                            depth: 1,
335
                            ...(tagInfo.subTags?.length && {
336
                                children: tagInfo.subTags?.map((subInfo: any) => {
337
                                    return {
338
                                        key: subInfo.id,
339
                                        name: subInfo.name,
340
                                        description: subInfo.description,
341
                                        depth: 2,
342
                                    };
343
                                }),
344
                            }),
345
                        };
346
                    }),
347
                }),
348
            };
349
        });
350
    };
351

  
347 352
    /**
348 353
     * Searches given value in 'name' column in a table.
349 354
     * If the provided value is empty the method loads all data from a server.
350 355
     * @param value Value that is searched.
351 356
     */
352 357
    const searchTag = (value: string) => {
353
        if (!value) loadData();
358
        let data = mapData(tagData);
359

  
360
        if (value) {
361
            data = data?.filter((category) => {
362
                category.children = category.children?.filter((tag: any) => {
363
                    tag.children = tag.children?.filter((subTag: any) =>
364
                        subTag.name.toLowerCase().includes(value.toLowerCase())
365
                    );
366

  
367
                    return (
368
                        tag.children?.length > 0 ||
369
                        tag.name.toLowerCase().includes(value.toLowerCase())
370
                    );
371
                });
354 372

  
355
        let data = tagData?.filter((category) => {
356
            category.children = category.children?.filter((tag: any) => {
357
                tag.children = tag.children?.filter((subTag: any) =>
358
                    subTag.name.startsWith(value)
373
                return (
374
                    category.children?.length > 0 ||
375
                    category.name.toLowerCase().includes(value.toLowerCase())
359 376
                );
360

  
361
                return tag.children?.length > 0 || tag.name.startsWith(value);
362 377
            });
378
        }
363 379

  
364
            return category.children?.length > 0 || category.name.startsWith(value);
365
        });
366

  
367
        setTagData(data);
380
        setShownData(data);
368 381
    };
369 382

  
370 383
    return redirecting || role !== 'ADMINISTRATOR' ? null : (
......
436 449
                <Row>
437 450
                    <Table
438 451
                        columns={columns}
439
                        dataSource={tagData}
452
                        dataSource={shownData}
440 453
                        scroll={{ y: 'calc(100vh - 300px)' }}
441 454
                        size="small"
442 455
                        title={() => (

Také k dispozici: Unified diff