-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
126 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import useSWRImmutable from "swr/immutable"; | ||
import { useState, useEffect } from "react"; | ||
|
||
export default function useUploads(allUploads, setAllUploads, gitUser, gitRepo) { | ||
|
||
let headers; | ||
const builtURL = `https://api.github.com/repos/${gitUser}/${gitRepo}/contents/uploads` | ||
|
||
if (process.env.NEXT_PUBLIC_GITHUBSECRET === 'true') { | ||
headers = new Headers( | ||
{ | ||
'Content-Type': 'application/json', | ||
'authorization': `token ${process.env.NEXT_PUBLIC_GITHUBSECRET}` | ||
}); | ||
} else { | ||
headers = new Headers( | ||
{ | ||
'Content-Type': 'application/json', | ||
}); | ||
} | ||
|
||
const fetcher = (headers) => (...args) => fetch(...args, { | ||
headers: headers, | ||
method: 'GET', | ||
}).then( | ||
res => res.json() | ||
).catch( | ||
err => console.log('err', err) | ||
) | ||
|
||
const { data: uploads, error } = useSWRImmutable(gitUser ? builtURL : null, fetcher(headers), | ||
{ revalidateOnMount: true }) | ||
|
||
|
||
useEffect(() => { | ||
if (uploads) { | ||
setAllUploads(uploads) | ||
} | ||
}, [uploads]) | ||
|
||
return { uploads, error } | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import useSWRImmutable from "swr/immutable"; | ||
import { useState, useEffect } from "react"; | ||
|
||
export default function useWorkshop(gitUser, gitRepo, gitFile){ | ||
|
||
let headers; | ||
|
||
if (process.env.NEXT_PUBLIC_GITHUBSECRET === 'true') { | ||
headers = new Headers( | ||
{ | ||
'Content-Type': 'application/json', | ||
'authorization': `token ${process.env.NEXT_PUBLIC_GITHUBSECRET}` | ||
}); | ||
} else { | ||
headers = new Headers( | ||
{ | ||
'Content-Type': 'application/json', | ||
}); | ||
} | ||
|
||
const fetcher = (headers) => (...args) => fetch(...args, { | ||
headers: headers, | ||
method: 'GET', | ||
}).then( | ||
res => res.json() | ||
).then( | ||
// decode from base64 | ||
res => Buffer.from(res.content, 'base64').toString() | ||
) | ||
|
||
const { data, isLoading, error } = useSWRImmutable(gitUser !=null ? builtURL : null, fetcher(headers), | ||
{ | ||
onSuccess(data) { | ||
const matterResult = matter(data) | ||
setCurrentFile(matterResult) | ||
setContent(matterResult.content) | ||
setLanguage(matterResult.data.programming_language); | ||
setWorkshopTitle(matterResult.data.title); | ||
}, | ||
onFailure(err) { | ||
console.log('err', err) | ||
console.log('workshop.url', builtURL) | ||
} | ||
}, | ||
{ | ||
revalidateOnFocus: false, | ||
revalidateOnReconnect: false, | ||
revalidateIfStale: false, | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters