-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu-refs.js
41 lines (38 loc) · 1.01 KB
/
menu-refs.js
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
import React, {useState, useEffect} from 'react';
import sanityClient from 'part:@sanity/base/client';
export default function MenuReference({id}) {
const [referenced, setReferenced] = useState(false);
const query = `*[references("${id}")]`;
useEffect(() => {
const subscription = sanityClient.observable.fetch(query).subscribe({
next: res => {
if (res[0] !== undefined) {
setReferenced({
items: res[0]._id === 'global-main'
});
}
},
error: error => {
console.log('Observable failed:' + error);
},
complete: () => {
console.log('Complete');
}
});
return () => {
subscription.unsubscribe();
};
}, [query]);
return (
<div
key={id}
style={{
width: '20px',
height: '20px',
borderRadius: '50%',
border: `2px solid ${referenced ? 'rgb(25, 179, 102)' : 'red'}`,
backgroundColor: `${referenced ? 'rgb(25, 179, 102)' : 'none'}`
}}
/>
);
}