Skip to content

Commit

Permalink
Added privateKey to returned type of Tags,
Browse files Browse the repository at this point in the history
removed now-redundant type TagWithPrivateKey,
removed now-redundant getAllTagsWithPrivateKeys
  • Loading branch information
twobackfromtheend committed Jul 10, 2019
1 parent 2fd654c commit edd593f
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 41 deletions.
14 changes: 9 additions & 5 deletions backend/blueprints/spa_api/service_layers/replay/tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,32 @@


class Tag:
def __init__(self, name: str, owner: str, db_tag: DBTag = None):
def __init__(self, name: str, owner: str, privateKey: str = None, db_tag: DBTag = None):
super().__init__()
self.name = name
self.owner_id = owner
self.ownerId = owner
self.privateKey = privateKey
self.db_tag = db_tag

def to_JSON(self, with_id=False):
if with_id:
return {
"name": self.name,
"owner_id": self.owner_id,
"ownerId": self.ownerId,
"privateKey": self.privateKey,
"tag_id": self.db_tag.id
}

return {
"name": self.name,
"owner_id": self.owner_id
"ownerId": self.ownerId,
"privateKey": self.privateKey,
}

@staticmethod
def create_from_dbtag(tag: DBTag):
return Tag(tag.name, tag.owner, db_tag=tag)
private_key = None if tag.private_id is None else Tag.encode_tag(tag.id, tag.private_id)
return Tag(tag.name, tag.owner, privateKey=private_key, db_tag=tag)

@staticmethod
@with_session
Expand Down
6 changes: 3 additions & 3 deletions webapp/src/Components/Pages/TagsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as React from "react"
import { connect } from "react-redux"
import { Dispatch } from "redux"
import { StoreState, TagsAction } from "../../Redux"
import { getAllTagsWithPrivateKeys } from "../../Requests/Tag"
import { getAllTags } from "../../Requests/Tag"
import { TagPageListItem } from "../Shared/Tag/TagPageListItem"
import { BasePage } from "./BasePage"

Expand All @@ -12,15 +12,15 @@ const mapStateToProps = (state: StoreState) => ({
})

const mapDispatchToProps = (dispatch: Dispatch) => ({
setTags: (tags: TagWithPrivateKey[]) => dispatch(TagsAction.setTagsAction(tags))
setTags: (tags: Tag[]) => dispatch(TagsAction.setTagsAction(tags))
})

type Props = ReturnType<typeof mapStateToProps>
& ReturnType<typeof mapDispatchToProps>

class TagsPageComponent extends React.PureComponent<Props> {
public componentDidMount() {
getAllTagsWithPrivateKeys()
getAllTags()
.then((tags) => this.props.setTags(tags))
}

Expand Down
4 changes: 2 additions & 2 deletions webapp/src/Components/Shared/Tag/TagPageListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import { TagsAction } from "../../../Redux"
import { generateTagPrivateIdAndGetKey } from "../../../Requests/Tag"

const mapDispatchToProps = (dispatch: Dispatch) => ({
addPrivateKeyToTagAction: (tag: TagWithPrivateKey) => dispatch(TagsAction.addPrivateKeyToTagAction(tag))
addPrivateKeyToTagAction: (tag: Tag) => dispatch(TagsAction.addPrivateKeyToTagAction(tag))
})

interface OwnProps {
tag: TagWithPrivateKey
tag: Tag
}

type Props = OwnProps
Expand Down
10 changes: 5 additions & 5 deletions webapp/src/Components/Shared/Upload/UploadTags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import { Link } from "react-router-dom"
import { Dispatch } from "redux"
import { TAGS_PAGE_LINK } from "../../../Globals"
import { StoreState, TagsAction } from "../../../Redux"
import { getAllTagsWithPrivateKeys } from "../../../Requests/Tag"
import { getAllTags } from "../../../Requests/Tag"
import { AddTagPrivateKeyDialog } from "./AddTagPrivateKeyDialog."

const mapStateToProps = (state: StoreState) => ({
tags: state.tags
})
const mapDispatchToProps = (dispatch: Dispatch) => ({
setTags: (tags: TagWithPrivateKey[]) => dispatch(TagsAction.setTagsAction(tags))
setTags: (tags: Tag[]) => dispatch(TagsAction.setTagsAction(tags))
})

interface OwnProps {
Expand All @@ -47,7 +47,7 @@ class UploadTagsComponent extends React.PureComponent<Props, State> {
}

public componentDidMount() {
getAllTagsWithPrivateKeys()
getAllTags()
.then((tags) => this.props.setTags(tags))
}

Expand All @@ -59,7 +59,7 @@ class UploadTagsComponent extends React.PureComponent<Props, State> {
tagsWithPrivateKeys = [...tagsWithPrivateKeys, ...this.state.externalPrivateKeys]

return (
<>
<div style={{paddingTop: 16}}>
{tags !== null && (
<Grid container spacing={8}>
<Grid item xs={11}>
Expand Down Expand Up @@ -120,7 +120,7 @@ class UploadTagsComponent extends React.PureComponent<Props, State> {
toggleExternalKeyDialog={this.toggleExternalKeyDialog}
addExternalPrivateKey={this.addExternalPrivateKey}
/>
</>
</div>
)
}

Expand Down
3 changes: 0 additions & 3 deletions webapp/src/Models/types/Tag.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
interface Tag {
name: string
ownerId: string
}

interface TagWithPrivateKey extends Tag {
privateKey: null | string
}
4 changes: 2 additions & 2 deletions webapp/src/Redux/tags/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ enum Type {

export const TagsAction = {
Type,
setTagsAction: createAction<TagWithPrivateKey[]>(Type.SET_TAGS),
addPrivateKeyToTagAction: createAction<TagWithPrivateKey>(Type.ADD_PRIVATE_KEY_TO_TAG)
setTagsAction: createAction<Tag[]>(Type.SET_TAGS),
addPrivateKeyToTagAction: createAction<Tag>(Type.ADD_PRIVATE_KEY_TO_TAG)
}
4 changes: 2 additions & 2 deletions webapp/src/Redux/tags/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Action, handleActions } from "redux-actions"
import { TagsAction } from "./actions"

export type TagsState = TagWithPrivateKey[] | null
export type TagsState = Tag[] | null

const initialState: TagsState = null

Expand All @@ -17,7 +17,7 @@ export const tagsReducer = handleActions<TagsState, any>(
return state
},
[TagsAction.Type.ADD_PRIVATE_KEY_TO_TAG]: (
state, action: Action<TagWithPrivateKey>
state, action: Action<Tag>
): TagsState => {
if (action.payload !== undefined && state !== null) {
return state.map((tag) => {
Expand Down
22 changes: 3 additions & 19 deletions webapp/src/Requests/Tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,7 @@ export const getPrivateTagKey = (tag: Tag): Promise<string> => {
return doGet(`/tag/${tag.name}/private_key`)
}

// TODO: Create backend to reduce number of calls.
export const getAllTagsWithPrivateKeys = (): Promise<TagWithPrivateKey[]> => {
return getAllTags()
.then(async(tags) => {
const privateKeys = tags.map(getPrivateTagKey)
const caughtprivateKeys = await Promise.all(privateKeys.map((privateKey: Promise<string>) => {
return privateKey
.then((_) => _)
.catch(() => null)
}))
return tags.map((tag, i) => (
{...tag, privateKey: caughtprivateKeys[i]}
))
})
}

export const generateTagPrivateIdAndGetKey = (tag: Tag): Promise<string> => {
return generateTagPrivateID(tag)
.then(() => getPrivateTagKey(tag))
}
return generateTagPrivateID(tag)
.then(() => getPrivateTagKey(tag))
}

0 comments on commit edd593f

Please sign in to comment.