File size: 3,874 Bytes
d757506
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { useEffect, useState, useRef } from "react";
import { Input, Space, Button, Image, Result } from 'antd-mobile';
import { UpCircleOutline } from 'antd-mobile-icons'
import { getSearchCustomService, getItemImageCustomService } from './service';
import ChatData from './data.json';
import api from '@/axios/apiNames';

import './style.less';

interface Item {
    id: string;
    item: string;
}

interface ImageItem {
  url: string;
  from: string;
  id: string;
  link?: string;
  prompt?: string;
  label?: string;
}

const demoSrc =
  "https://images.unsplash.com/photo-1567945716310-4745a6b7844b?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1500&q=60";

function SearchCom() {
    const [images, setImages] = useState<ImageItem[]>([]);

    const getClosetAll = () => {
        return new Promise((resolve, reject) => {
            const closetAll = ChatData["item in the closet"].map((item: Item) => { return getItemImageCustomService(item?.id)
            });

            Promise.all(closetAll).then((result) => {
                const results: ImageItem[] = result.map((res) => {
                    const data = res?.data[0];
                    const item = 
                        {
                            id: data?.id,
                            url: (data?.attributes?.images?.data[0]?.attributes?.url) || '',
                            from: 'closet',
                            link: ''
                        };
                    item.url = `${'http://106.52.238.44:8080'}${item.url}`;
                    return item;
                })
                resolve(results);
            }).catch((err) => reject(err))
        })
    }

    const getNotClosetAll = () => {
        const notClosetAll = ChatData["item not in the closet"].map((key: string) => { return getSearchCustomService(key)});
        return new Promise((resolve, reject) => {
            Promise.all(notClosetAll).then((result) => {
                const results: ImageItem[] = []
                result.forEach((res) => {
                    const data = res?.organic_results[2];
                    if (data) {
                        if (data?.related_images) {
                            const item = 
                            {
                                id: data?.related_images?.[0]?.link || '',
                                url: data?.related_images?.[0]?.image || '',
                                from: 'not closet',
                                link: data?.related_images[0]?.link || '',
                            };
                            results.push(item);
                        } else if(data?.thumbnail) {
                            const item = 
                            {
                                id: data?.link || '',
                                url: data?.thumbnail || '',
                                from: 'not closet',
                                link: data?.link || '',
                            };
                            results.push(item);
                        }
                    }
                })
                resolve(results);
            }).catch((err) => reject(err));
        });
    }

    useEffect(() => {
        const getAllData = async () => {
            const closetAllData = await getClosetAll();
            const notClosetAllData = await getNotClosetAll();
            setImages([...images, ...closetAllData as ImageItem[], ...notClosetAllData as ImageItem[]]);
        }
        getAllData();
    }, []);

    return (<>
       <div className="search-container">
       <div className="search-container-image">     
       <Space wrap> 
            {
            images.map((img) => <Image key={img.id} src={img.url} />)
            }
        </Space>
        </div>
       </div>
    </>)
}

export default SearchCom;