Skip to content

Commit

Permalink
Support tagged projects
Browse files Browse the repository at this point in the history
  • Loading branch information
aggre committed Mar 28, 2018
1 parent beb0cd8 commit 164b4e3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
43 changes: 31 additions & 12 deletions src/elements/oo-projects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,28 @@ define('oo-empty', empty)
define('oo-project-status', projectStatus)

const ATTR = {
DATA_IAM: 'data-iam'
DATA_IAM: 'data-iam',
DATA_TAG: 'data-tag'
}

const stateIam = weakMap<string>()
const stateTag = weakMap<string>()
const stateProjects = weakMap<Array<OOProject>>()
const stateItemCount = weakMap<number>()

export default class extends OOElement {
static get observedAttributes() {
return [ATTR.DATA_IAM]
return [ATTR.DATA_IAM, ATTR.DATA_TAG]
}

get iam() {
return stateIam.get(this)
}

get tag() {
return stateTag.get(this)
}

get projects() {
return stateProjects.get(this)
}
Expand All @@ -48,21 +54,29 @@ export default class extends OOElement {
if (prev === next) {
return
}
stateIam.set(this, next)
stateProjects.set(this, [])
this.fetchProjects(this.iam)
switch (attr) {
case ATTR.DATA_IAM:
stateIam.set(this, next)
break
case ATTR.DATA_TAG:
stateTag.set(this, next)
break
default:
break
}
if (this.connected) {
this.initialFetchProjects()
}
}

connectedCallback() {
super.connectedCallback(false)
if (this.hasAttribute(ATTR.DATA_IAM) === false) {
stateProjects.set(this, [])
this.fetchProjects(this.iam)
}
this.initialFetchProjects()
}

render() {
const iam = this.iam
const tag = this.tag
const projects = this.projects
const count = stateItemCount.get(this)
if (projects.length === 0) {
Expand All @@ -73,7 +87,7 @@ export default class extends OOElement {
const paging = projects[projects.length - 1].created - 1
const more = count > projects.length ? html`
<div class=paging>
<oo-atoms-button on-clicked='${() => this.fetchProjects(iam, paging)}'>More</oo-atoms-button>
<oo-atoms-button on-clicked='${() => this.fetchProjects(iam, tag, paging)}'>More</oo-atoms-button>
</div>
` : html``

Expand Down Expand Up @@ -165,12 +179,17 @@ export default class extends OOElement {
`
}

async fetchProjects(iam: string | null, time?: number) {
async initialFetchProjects() {
stateProjects.set(this, [])
this.fetchProjects(this.iam, this.tag)
}

async fetchProjects(iam: string | null, tag?: string | null, time?: number) {
const api = await (() => {
if (typeof iam === 'string' && iam !== '') {
return getUserProjects(iam, time)
}
return getPublicProjects(time)
return getPublicProjects(tag, time)
})()
const {response, headers} = api
stateItemCount.set(this, Number(headers.get('x-oo-count')))
Expand Down
6 changes: 4 additions & 2 deletions src/lib/oo-api-get-public-projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import api from '../lib/oo-api'
import {OOAPIResult} from '../type/oo-api'
import {OOProject} from '../type/oo-project'

export default async (time?: number): Promise<OOAPIResult<OOProject>> => {
export default async (tag?: string, time?: number): Promise<OOAPIResult<OOProject>> => {
let pathParameter = tag ? `publics/tag/${tag}` : `publics`
pathParameter += `/${time ? time : ''}`
const ooapiRes = await api<OOProject>({
resource: 'projects',
pathParameter: `publics/${time ? time : ''}`,
pathParameter,
method: 'GET'
})

Expand Down

0 comments on commit 164b4e3

Please sign in to comment.