on master help sign in
search: pull other searches out of initial promise
c666bcaMatilde Park
components/Search.js+53−57
@@ -60,76 +60,72 @@ class Search extends Component {
60
60
61 onInputValueChange = debounce(async (query) => {
61 onInputValueChange = debounce(async (query) => {
62 if (query.length) {
62 if (query.length) {
63− fetch(this.searchEndpoint(query))
63+ const search = fetch(this.searchEndpoint(query))
64 .then((res) => res.json())
64 .then((res) => res.json())
65 .then(async (res) => {
65 .then(async (res) => {
66− // Wrap results in an object which will tell React what component to use to render results.
67− const results = res.results.map((item) => ({
66+ return res.results.map((item) => ({
68 type: "RESULT",
67 type: "RESULT",
69 content: item,
68 content: item,
70 }));
69 }));
70+ });
71
71
72− const patp = this.patpSearch(query)
72+ const patp = this.patpSearch(query)
73− ? !isNaN(query)
73+ ? !isNaN(query)
74− ? ob.patp(query)
74+ ? ob.patp(query)
75− : ob.patp(ob.patp2dec(`~${deSig(query)}`))
75+ : ob.patp(ob.patp2dec(`~${deSig(query)}`))
76− : null;
76+ : null;
77
77
78− const patpResult = this.patpSearch(query)
78+ const patpResult = this.patpSearch(query)
79− ? [
79+ ? [
80− {
80+ {
81− type: "PATP",
81+ type: "PATP",
82− content: {
82+ content: {
83− patp: patp,
83+ patp: patp,
84− slug: `/ids/${patp}`,
84+ slug: `/ids/${patp}`,
85− },
85+ },
86− },
86+ },
87− ]
87+ ]
88− : [];
88+ : [];
89
89
90− const glossarySearch = fetch(this.glossarySearch(query))
90+ const glossarySearch = fetch(this.glossarySearch(query))
91− .then((res) => res.json())
91+ .then((res) => res.json())
92− .then((res) => {
92+ .then((res) => {
93− return res.results.map((item) => ({
93+ return res.results.map((item) => ({
94− type: "GLOSSARY_RESULT",
94+ type: "GLOSSARY_RESULT",
95− content: item,
95+ content: item,
96− }));
96+ }));
97− });
97+ });
98
98
99− const devSearch = fetch(this.devSearch(query))
99+ const devSearch = fetch(this.devSearch(query))
100− .then((res) => res.json())
100+ .then((res) => res.json())
101− .then((res) => {
101+ .then((res) => {
102− return res.results.map((item) => ({
102+ return res.results.map((item) => ({
103− type: "DEV_RESULT",
103+ type: "DEV_RESULT",
104− content: item,
104+ content: item,
105− }));
105+ }));
106− });
106+ });
107
107
108− const opsSearch = fetch(this.opsSearch(query))
108+ const opsSearch = fetch(this.opsSearch(query))
109− .then((res) => res.json())
109+ .then((res) => res.json())
110− .then((res) => {
110+ .then((res) => {
111− return res.results.map((item) => ({
111+ return res.results.map((item) => ({
112− type: "OPS_RESULT",
112+ type: "OPS_RESULT",
113− content: item,
113+ content: item,
114− }));
114+ }));
115− });
115+ });
116
116
117− const [glossaryResults, devResults, opsResults] = await Promise.all([
117+ const [results, glossaryResults, devResults, opsResults] =
118− glossarySearch,
118+ await Promise.all([search, glossarySearch, devSearch, opsSearch]);
119− devSearch,
120− opsSearch,
121− ]);
122
119
123− const list = [
120+ const list = [
124− ...glossaryResults,
121+ ...glossaryResults,
125− ...patpResult,
122+ ...patpResult,
126− ...results,
123+ ...results,
127− ...devResults,
124+ ...devResults,
128− ...opsResults,
125+ ...opsResults,
129− ];
126+ ];
130
127
131− this.setState({ results: list });
128+ this.setState({ results: list });
132− });
133 } else {
129 } else {
134 this.setState({ results: [] });
130 this.setState({ results: [] });
135 }
131 }