From c4e7547c45a560530884ba7b2c93a231d70b767c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Thu, 22 Oct 2020 11:24:00 +0100 Subject: [PATCH] feat: add revoke token button --- lib/new-admin/graphql/schema.js | 4 +- lib/token-manager.js | 13 ++- .../pages/TokenManagement/TokenManagement.js | 79 +++++++++++-------- .../TokenManagement/TokenManagement.styles.js | 27 +------ 4 files changed, 58 insertions(+), 65 deletions(-) diff --git a/lib/new-admin/graphql/schema.js b/lib/new-admin/graphql/schema.js index a39eb05d..db678330 100644 --- a/lib/new-admin/graphql/schema.js +++ b/lib/new-admin/graphql/schema.js @@ -243,6 +243,7 @@ const typeDefs = gql` saveConfig(config: JSONObject): JSONObject createPairingTotem(name: String!): String saveAccounts(accounts: JSONObject): JSONObject + revokeToken(token: String!): UserToken } ` @@ -293,7 +294,8 @@ const resolvers = { .then(it => { notify() return it - }) + }), + revokeToken: (...[, { token }]) => tokenManager.revokeToken(token) } } diff --git a/lib/token-manager.js b/lib/token-manager.js index 74bde507..3c9a7cc5 100644 --- a/lib/token-manager.js +++ b/lib/token-manager.js @@ -1,8 +1,13 @@ const db = require('./db') -function getTokenList() { - const sql = `select * from user_tokens` - return db.any(sql); +function getTokenList () { + const sql = `select * from user_tokens` + return db.any(sql) } -module.exports = { getTokenList } \ No newline at end of file +function revokeToken (token) { + const sql = `delete from user_tokens where token = $1` + return db.none(sql, [token]) +} + +module.exports = { getTokenList, revokeToken } \ No newline at end of file diff --git a/new-lamassu-admin/src/pages/TokenManagement/TokenManagement.js b/new-lamassu-admin/src/pages/TokenManagement/TokenManagement.js index 1ee83b29..c503a547 100644 --- a/new-lamassu-admin/src/pages/TokenManagement/TokenManagement.js +++ b/new-lamassu-admin/src/pages/TokenManagement/TokenManagement.js @@ -1,4 +1,4 @@ -import { useQuery } from '@apollo/react-hooks' +import { useQuery, useMutation } from '@apollo/react-hooks' import { makeStyles } from '@material-ui/core/styles' import gql from 'graphql-tag' import moment from 'moment' @@ -6,15 +6,16 @@ import * as R from 'ramda' import React from 'react' import Title from 'src/components/Title' -// import DataTable from 'src/components/tables/DataTable' -import { Table as EditableTable } from 'src/components/editableTable' +import { IconButton } from 'src/components/buttons' +import DataTable from 'src/components/tables/DataTable' +import { ReactComponent as DeleteIcon } from 'src/styling/icons/action/delete/enabled.svg' import { mainStyles } from './TokenManagement.styles' const useStyles = makeStyles(mainStyles) const GET_USER_TOKENS = gql` - { + query userTokens { userTokens { token name @@ -23,64 +24,72 @@ const GET_USER_TOKENS = gql` } ` +const REVOKE_USER_TOKEN = gql` + mutation revokeToken($token: String!) { + revokeToken(token: $token) { + token + } + } +` + const Tokens = () => { const classes = useStyles() const { data: tknResponse } = useQuery(GET_USER_TOKENS) + const [revokeToken] = useMutation(REVOKE_USER_TOKEN, { + onCompleted: () => console.log('passed'), + onError: () => console.log('failed'), + refetchQueries: () => ['userTokens'] + }) + const elements = [ { - name: 'name', header: 'Name', - width: 312, + width: 257, textAlign: 'center', - size: 'sm' + size: 'sm', + view: t => t.name }, { - name: 'token', header: 'Token', - width: 520, + width: 505, textAlign: 'center', - size: 'sm' + size: 'sm', + view: t => t.token }, { - name: 'created', header: 'Date (UTC)', - width: 140, + width: 145, textAlign: 'right', size: 'sm', - view: t => moment.utc(t).format('YYYY-MM-DD') + view: t => moment.utc(t.created).format('YYYY-MM-DD') }, { - name: 'created', header: 'Time (UTC)', - width: 140, + width: 145, textAlign: 'right', size: 'sm', - view: t => moment.utc(t).format('HH:mm:ss') + view: t => moment.utc(t.created).format('HH:mm:ss') + }, + { + header: '', + width: 80, + textAlign: 'center', + size: 'sm', + view: t => ( + { + revokeToken({ variables: { token: t.token } }) + }}> + + + ) } ] return ( <> - {console.log(tknResponse)} -
-
- Token Management -
-
- - - ) - - /* return ( - <> - {console.log(tknResponse)}
Token Management @@ -91,7 +100,7 @@ const Tokens = () => { data={R.path(['userTokens'])(tknResponse)} /> - ) */ + ) } export default Tokens diff --git a/new-lamassu-admin/src/pages/TokenManagement/TokenManagement.styles.js b/new-lamassu-admin/src/pages/TokenManagement/TokenManagement.styles.js index 9d52042f..e5ac1d06 100644 --- a/new-lamassu-admin/src/pages/TokenManagement/TokenManagement.styles.js +++ b/new-lamassu-admin/src/pages/TokenManagement/TokenManagement.styles.js @@ -1,33 +1,10 @@ -import typographyStyles from 'src/components/typography/styles' import baseStyles from 'src/pages/Logs.styles' -const { label1 } = typographyStyles -const { titleWrapper, titleAndButtonsContainer, buttonsWrapper } = baseStyles +const { titleWrapper, titleAndButtonsContainer } = baseStyles const mainStyles = { titleWrapper, - titleAndButtonsContainer, - buttonsWrapper, - headerLabels: { - display: 'flex', - flexDirection: 'row', - '& div': { - display: 'flex', - alignItems: 'center' - }, - '& > div:first-child': { - marginRight: 24 - }, - '& span': { - extend: label1, - marginLeft: 6 - } - }, - overflowTd: { - overflow: 'hidden', - whiteSpace: 'nowrap', - textOverflow: 'ellipsis' - } + titleAndButtonsContainer } export { mainStyles }