Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
scratch-www
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
scratch-www
Commits
560379f9
Commit
560379f9
authored
Mar 16, 2021
by
Paul Kaplan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simple data fetching using react hooks
parent
7b7266c5
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
60 additions
and
2 deletions
+60
-2
src/views/studio/debug.jsx
src/views/studio/debug.jsx
+12
-0
src/views/studio/lib/fetchers.js
src/views/studio/lib/fetchers.js
+32
-0
src/views/studio/studio-info.jsx
src/views/studio/studio-info.jsx
+16
-2
No files found.
src/views/studio/debug.jsx
0 → 100644
View file @
560379f9
import
React
from
'
react
'
;
export
default
function
Debug
({
label
,
data
})
{
return
<
div
style=
{
{
padding
:
'
2rem
'
,
'
border
'
:
'
1px solid red
'
,
margin
:
'
2rem
'
}
}
>
<
small
>
{
label
}
</
small
>
<
code
>
<
pre
style=
{
{
fontSize
:
'
0.75rem
'
}
}
>
{
JSON
.
stringify
(
data
,
null
,
'
'
)
}
</
pre
>
</
code
>
</
div
>
}
\ No newline at end of file
src/views/studio/lib/fetchers.js
0 → 100644
View file @
560379f9
const
ITEM_LIMIT
=
4
;
const
infoFetcher
=
studioId
=>
fetch
(
`
${
process
.
env
.
API_HOST
}
/studios/
${
studioId
}
`
)
.
then
(
d
=>
d
.
json
());
const
projectFetcher
=
(
studioId
,
offset
)
=>
fetch
(
`
${
process
.
env
.
API_HOST
}
/studios/
${
studioId
}
/projects?limit=
${
ITEM_LIMIT
}
&offset=
${
offset
}
`
)
.
then
(
d
=>
d
.
json
())
.
then
(
d
=>
({
items
:
d
,
moreToLoad
:
d
.
length
===
ITEM_LIMIT
}));
const
curatorFetcher
=
(
studioId
,
offset
)
=>
fetch
(
`
${
process
.
env
.
API_HOST
}
/studios/
${
studioId
}
/curators?limit=
${
ITEM_LIMIT
}
&offset=
${
offset
}
`
)
.
then
(
d
=>
d
.
json
())
.
then
(
d
=>
({
items
:
d
,
moreToLoad
:
d
.
length
===
ITEM_LIMIT
}));
const
managerFetcher
=
(
studioId
,
offset
)
=>
fetch
(
`
${
process
.
env
.
API_HOST
}
/studios/
${
studioId
}
/managers?limit=
${
ITEM_LIMIT
}
&offset=
${
offset
}
`
)
.
then
(
d
=>
d
.
json
())
.
then
(
d
=>
({
items
:
d
,
moreToLoad
:
d
.
length
===
ITEM_LIMIT
}));
const
activityFetcher
=
studioId
=>
fetch
(
`
${
process
.
env
.
API_HOST
}
/studios/
${
studioId
}
/activity`
)
.
then
(
d
=>
d
.
json
())
.
then
(
d
=>
({
items
:
d
,
moreToLoad
:
false
}));
// No pagination on the activity feed
export
{
activityFetcher
,
infoFetcher
,
projectFetcher
,
curatorFetcher
,
managerFetcher
};
src/views/studio/studio-info.jsx
View file @
560379f9
import
React
from
'
react
'
;
import
React
,
{
useState
,
useEffect
}
from
'
react
'
;
import
{
useParams
}
from
'
react-router-dom
'
;
import
{
useParams
}
from
'
react-router-dom
'
;
import
{
infoFetcher
}
from
'
./lib/fetchers
'
;
import
Debug
from
'
./debug.jsx
'
;
const
StudioInfo
=
()
=>
{
const
StudioInfo
=
()
=>
{
const
{
studioId
}
=
useParams
();
const
{
studioId
}
=
useParams
();
const
[
state
,
setState
]
=
useState
({
loading
:
false
,
error
:
null
,
data
:
null
});
// Since this data is in a component that is always visible, it doesn't necessarily
// need to be kept in redux. One alternative is to use the infinite-list redux
// module and just treat the studio info as the first and only item in the list.
useEffect
(()
=>
{
if
(
!
studioId
)
return
;
infoFetcher
(
studioId
)
.
then
(
data
=>
setState
({
loading
:
false
,
error
:
null
,
data
}))
.
catch
(
error
=>
setState
({
loading
:
false
,
error
,
data
:
null
}))
},
[
studioId
]);
return
(
return
(
<
div
>
<
div
>
<
h2
>
Studio Info
</
h2
>
<
h2
>
Studio Info
</
h2
>
<
p
>
Studio
{
studioId
}
</
p
>
{
state
.
loading
&&
<
div
>
Loading..
</
div
>
}
{
state
.
error
&&
<
Debug
label=
"Error"
data=
{
state
.
error
}
/>
}
<
Debug
label=
"Studio Info"
data=
{
state
.
data
}
/>
</
div
>
</
div
>
);
);
};
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment