From 56405d85e564f2c397dbc3424d6c40520234739d Mon Sep 17 00:00:00 2001 From: Nick Coutsos Date: Sun, 7 Nov 2021 11:49:33 -0500 Subject: Organize github-related components --- application/components/app.vue | 2 +- application/components/github-picker.vue | 157 -------------------- application/components/github/api.js | 99 +++++++++++++ application/components/github/invalid-repo.vue | 59 ++++++++ application/components/github/picker.vue | 158 +++++++++++++++++++++ application/components/github/storage.js | 18 +++ application/components/initialize.vue | 2 +- application/components/keyboard-picker.vue | 2 +- application/components/messages/invalid-repo.vue | 59 -------- application/components/messages/too-many-repos.vue | 37 ----- application/github.js | 99 ------------- 11 files changed, 337 insertions(+), 355 deletions(-) delete mode 100644 application/components/github-picker.vue create mode 100644 application/components/github/api.js create mode 100644 application/components/github/invalid-repo.vue create mode 100644 application/components/github/picker.vue create mode 100644 application/components/github/storage.js delete mode 100644 application/components/messages/invalid-repo.vue delete mode 100644 application/components/messages/too-many-repos.vue delete mode 100644 application/github.js diff --git a/application/components/app.vue b/application/components/app.vue index e53345c..766f901 100644 --- a/application/components/app.vue +++ b/application/components/app.vue @@ -6,7 +6,7 @@ import KeyboardPicker from './keyboard-picker.vue' import Spinner from './spinner.vue' import * as config from '../config' -import * as github from '../github' +import * as github from './github/api' export default { components: { diff --git a/application/components/github-picker.vue b/application/components/github-picker.vue deleted file mode 100644 index e52fcce..0000000 --- a/application/components/github-picker.vue +++ /dev/null @@ -1,157 +0,0 @@ - - - diff --git a/application/components/github/api.js b/application/components/github/api.js new file mode 100644 index 0000000..b2e29e9 --- /dev/null +++ b/application/components/github/api.js @@ -0,0 +1,99 @@ +import * as config from '../../config' + +let token +export let installation +export let repositories + +function request (...args) { + return fetch(...args).then(res => { + return res.status !== 401 ? res : ( + console.error('Authentication failure. Retrying login'), + beginLoginFlow() + ) + }) +} + +export async function init () { + const param = new URLSearchParams(location.search).get('token') + if (!localStorage.auth_token && param) { + history.replaceState({}, null, location.pathname) + localStorage.auth_token = param + } + + if (localStorage.auth_token) { + token = localStorage.auth_token + const data = await request(`${config.apiBaseUrl}/github/installation`, { + headers: { + Authorization: `Bearer ${token}` + } + }).then(res => res.json()) + + if (!data.installation) { + console.log('no installation found for authenticated user') + location.href = `https://github.com/apps/${config.githubAppName}/installations/new` + } + + installation = data.installation + repositories = data.repositories + } +} + +export function beginLoginFlow () { + localStorage.removeItem('auth_token') + location.href = `${config.apiBaseUrl}/github/authorize` +} + +export function isGitHubAuthorized() { + return !!token && installation && repositories && repositories.length +} + +export async function fetchRepoBranches(repository) { + const response = await request( + `${config.apiBaseUrl}/github/installation/${encodeURIComponent(installation.id)}/${encodeURIComponent(repository.full_name)}/branches`, + { headers: { Authorization: `Bearer ${localStorage.auth_token}`} } + ) + + return response.json() +} + +export async function fetchLayoutAndKeymap(repo, branch) { + const repository = repo + const url = new URL(`${config.apiBaseUrl}/github/keyboard-files/${encodeURIComponent(installation.id)}/${encodeURIComponent(repository)}`) + + if (branch) { + url.search = new URLSearchParams({ branch }).toString() + } + + const response = await request(url.toString(), { + headers: { + Authorization: `Bearer ${localStorage.auth_token}` + } + }) + + if (response.status === 400) { + console.error('Failed to load keymap and layout from github') + return response.json() + } + + const data = await response.json() + const defaultLayout = data.info.layouts.default || data.info.layouts[Object.keys(data.info.layouts)[0]] + return { + layout: defaultLayout.layout, + keymap: data.keymap + } +} + +export function commitChanges(repo, branch, layout, keymap) { + const installationId = encodeURIComponent(installation.id) + const repository = encodeURIComponent(repo) + const url = `${config.apiBaseUrl}/github/keyboard-files/${installationId}/${repository}/${encodeURIComponent(branch)}` + + return request(url, { + method: 'POST', + headers: { + Authorization: `Bearer ${token}`, + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ layout, keymap }) + }) +} diff --git a/application/components/github/invalid-repo.vue b/application/components/github/invalid-repo.vue new file mode 100644 index 0000000..eee706f --- /dev/null +++ b/application/components/github/invalid-repo.vue @@ -0,0 +1,59 @@ + + + + + diff --git a/application/components/github/picker.vue b/application/components/github/picker.vue new file mode 100644 index 0000000..73520a1 --- /dev/null +++ b/application/components/github/picker.vue @@ -0,0 +1,158 @@ + + + diff --git a/application/components/github/storage.js b/application/components/github/storage.js new file mode 100644 index 0000000..87603a0 --- /dev/null +++ b/application/components/github/storage.js @@ -0,0 +1,18 @@ +const REPOSITORY = 'selectedGithubRepository' +const BRANCH = 'selectedGithubBranch' + +export function getPersistedRepository() { + return JSON.parse(localStorage.getItem(REPOSITORY)) +} + +export function setPersistedRepository(repository) { + localStorage.setItem(REPOSITORY, JSON.stringify(repository)) +} + +export function getPersistedBranch() { + return JSON.parse(localStorage.getItem(BRANCH)) +} + +export function setPersistedBranch(branch) { + localStorage.setItem(BRANCH, JSON.stringify(branch)) +} diff --git a/application/components/initialize.vue b/application/components/initialize.vue index c0146a4..0d5fc1f 100644 --- a/application/components/initialize.vue +++ b/application/components/initialize.vue @@ -1,7 +1,7 @@ - - diff --git a/application/components/messages/too-many-repos.vue b/application/components/messages/too-many-repos.vue deleted file mode 100644 index 086e471..0000000 --- a/application/components/messages/too-many-repos.vue +++ /dev/null @@ -1,37 +0,0 @@ - - - - - \ No newline at end of file diff --git a/application/github.js b/application/github.js deleted file mode 100644 index 0d0782c..0000000 --- a/application/github.js +++ /dev/null @@ -1,99 +0,0 @@ -import * as config from './config' - -let token -export let installation -export let repositories - -function request (...args) { - return fetch(...args).then(res => { - return res.status !== 401 ? res : ( - console.error('Authentication failure. Retrying login'), - beginLoginFlow() - ) - }) -} - -export async function init () { - const param = new URLSearchParams(location.search).get('token') - if (!localStorage.auth_token && param) { - history.replaceState({}, null, location.pathname) - localStorage.auth_token = param - } - - if (localStorage.auth_token) { - token = localStorage.auth_token - const data = await request(`${config.apiBaseUrl}/github/installation`, { - headers: { - Authorization: `Bearer ${token}` - } - }).then(res => res.json()) - - if (!data.installation) { - console.log('no installation found for authenticated user') - location.href = `https://github.com/apps/${config.githubAppName}/installations/new` - } - - installation = data.installation - repositories = data.repositories - } -} - -export function beginLoginFlow () { - localStorage.removeItem('auth_token') - location.href = `${config.apiBaseUrl}/github/authorize` -} - -export function isGitHubAuthorized() { - return !!token && installation && repositories && repositories.length -} - -export async function fetchRepoBranches(repository) { - const response = await request( - `${config.apiBaseUrl}/github/installation/${encodeURIComponent(installation.id)}/${encodeURIComponent(repository.full_name)}/branches`, - { headers: { Authorization: `Bearer ${localStorage.auth_token}`} } - ) - - return response.json() -} - -export async function fetchLayoutAndKeymap(repo, branch) { - const repository = repo - const url = new URL(`${config.apiBaseUrl}/github/keyboard-files/${encodeURIComponent(installation.id)}/${encodeURIComponent(repository)}`) - - if (branch) { - url.search = new URLSearchParams({ branch }).toString() - } - - const response = await request(url.toString(), { - headers: { - Authorization: `Bearer ${localStorage.auth_token}` - } - }) - - if (response.status === 400) { - console.error('Failed to load keymap and layout from github') - return response.json() - } - - const data = await response.json() - const defaultLayout = data.info.layouts.default || data.info.layouts[Object.keys(data.info.layouts)[0]] - return { - layout: defaultLayout.layout, - keymap: data.keymap - } -} - -export function commitChanges(repo, branch, layout, keymap) { - const installationId = encodeURIComponent(installation.id) - const repository = encodeURIComponent(repo) - const url = `${config.apiBaseUrl}/github/keyboard-files/${installationId}/${repository}/${encodeURIComponent(branch)}` - - return request(url, { - method: 'POST', - headers: { - Authorization: `Bearer ${token}`, - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ layout, keymap }) - }) -} -- cgit v1.2.3