2020-07-16 04:36:29 +03:00
import * as React from "react" ;
import * as Actions from "~/common/actions" ;
2020-07-28 09:54:15 +03:00
import * as Constants from "~/common/constants" ;
2020-07-16 04:36:29 +03:00
import * as System from "~/components/system" ;
2020-09-05 02:15:29 +03:00
import * as SVG from "~/common/svg" ;
2020-11-28 07:39:01 +03:00
import * as Events from "~/common/custom-events" ;
2020-07-16 04:36:29 +03:00
2020-11-30 08:24:22 +03:00
import { css } from "@emotion/react" ;
2020-07-16 04:36:29 +03:00
import ScenePage from "~/components/core/ScenePage" ;
2020-08-22 07:25:34 +03:00
import ScenePageHeader from "~/components/core/ScenePageHeader" ;
2020-07-29 02:08:38 +03:00
import CodeBlock from "~/components/system/CodeBlock" ;
2020-07-16 04:36:29 +03:00
2020-07-28 09:54:15 +03:00
const STYLES _KEY = css `
display : flex ;
align - items : center ;
justify - content : space - between ;
2020-08-09 01:04:17 +03:00
width : 100 % ;
2020-08-09 06:52:22 +03:00
max - width : 416 px ;
2020-09-14 08:22:19 +03:00
background - color : $ { Constants . system . foreground } ;
2020-09-05 21:12:43 +03:00
color : $ { Constants . system . pitchBlack } ;
border - radius : 4 px ;
2020-09-14 08:22:19 +03:00
height : 40 px ;
2020-07-28 09:54:15 +03:00
` ;
const STYLES _KEY _LEFT = css `
2020-08-09 01:04:17 +03:00
min - width : 10 % ;
width : 100 % ;
2020-07-28 09:54:15 +03:00
font - family : $ { Constants . font . code } ;
2020-09-14 08:22:19 +03:00
padding : 0 16 px ;
2020-08-09 01:04:17 +03:00
font - size : 11 px ;
2020-07-28 09:54:15 +03:00
` ;
2020-09-14 08:22:19 +03:00
const STYLES _KEY _CONTAINER = css `
height : 40 px ;
display : flex ;
flex - direction : row ;
align - items : center ;
margin - bottom : 8 px ;
2020-07-28 09:54:15 +03:00
` ;
const STYLES _CIRCLE _BUTTON = css `
2020-09-14 08:22:19 +03:00
height : 40 px ;
width : 40 px ;
2020-09-05 21:12:43 +03:00
border - radius : 4 px ;
2020-07-28 09:54:15 +03:00
display : inline - flex ;
align - items : center ;
justify - content : center ;
user - select : none ;
2020-09-14 08:22:19 +03:00
background : $ { Constants . system . gray } ;
2020-08-09 01:04:17 +03:00
color : $ { Constants . system . black } ;
2020-07-28 09:54:15 +03:00
box - shadow : 0 1 px 4 px rgba ( 0 , 0 , 0 , 0.07 ) ;
cursor : pointer ;
2020-08-09 01:04:17 +03:00
transition : 200 ms ease all ;
: hover {
background : $ { Constants . system . brand } ;
color : $ { Constants . system . white } ;
}
2020-07-28 09:54:15 +03:00
` ;
class Key extends React . Component {
state = { visible : false } ;
_handleToggleVisible = ( ) => {
this . setState ( { visible : ! this . state . visible } ) ;
} ;
2020-08-09 01:04:17 +03:00
_handleDelete = async ( id ) => {
await this . props . onDelete ( id ) ;
} ;
2020-07-28 09:54:15 +03:00
render ( ) {
return (
2020-09-14 08:22:19 +03:00
< div css = { STYLES _KEY _CONTAINER } >
< div css = { STYLES _KEY } >
{ this . state . visible ? (
< div css = { STYLES _KEY _LEFT } > { this . props . data . key } < / d i v >
) : (
2020-10-28 07:01:15 +03:00
< div css = { STYLES _KEY _LEFT } > XXXXXXXXXXX - XXXX - XXXX - XXXX - XXXXXXXXXXXXXX < / d i v >
2020-09-14 08:22:19 +03:00
) }
2020-07-28 09:54:15 +03:00
< / d i v >
2020-09-14 08:22:19 +03:00
< span
css = { STYLES _CIRCLE _BUTTON }
onClick = { this . _handleToggleVisible }
style = { {
marginLeft : 8 ,
} }
>
< SVG . Privacy height = "16px" / >
< / s p a n >
< span
css = { STYLES _CIRCLE _BUTTON }
onClick = { ( ) => this . _handleDelete ( this . props . data . id ) }
style = { {
marginLeft : 8 ,
} }
>
< SVG . Dismiss height = "16px" / >
< / s p a n >
2020-07-28 09:54:15 +03:00
< / d i v >
) ;
}
}
2020-11-16 22:53:52 +03:00
const EXAMPLE _UPDATE _SLATE = ( key , slateId ) => {
return ` const slateAPIResponseData = ${ EXAMPLE _GET _SLATE _RESPONSE ( key , slateId ) } ;
// NOTE(jim)
// Make any modifications you want!
2021-01-11 03:59:36 +03:00
// Be careful because if you modify too many things, your Slate may not work
2020-11-16 22:53:52 +03:00
// With https://slate.host
const slate = slateAPIResponseData . data ;
slate . data . objects [ 0 ] . title = "Julie Was Here."
2020-11-16 11:47:56 +03:00
const response = await fetch ( 'https://slate.host/api/v1/update-slate' , {
method : 'POST' ,
headers : {
'Content-Type' : 'application/json' ,
// NOTE: your API key
Authorization : 'Basic ${key}' ,
} ,
body : JSON . stringify ( { data : slate } )
} ) ;
const json = await response . json ( ) ;
console . log ( json ) ; ` ;
} ;
2020-08-22 07:25:34 +03:00
const EXAMPLE _GET _SLATE = (
key ,
slateId
) => ` const response = await fetch('https://slate.host/api/v1/get-slate', {
2021-01-11 00:59:14 +03:00
method : 'POST' ,
2020-07-29 02:08:38 +03:00
headers : {
'Content-Type' : 'application/json' ,
// NOTE: your API key
Authorization : 'Basic ${key}' ,
} ,
body : JSON . stringify ( { data : {
2020-08-06 23:49:27 +03:00
// NOTE: your slate ID
2021-01-11 03:59:36 +03:00
id : '${slateId}'
2020-07-29 02:08:38 +03:00
} } )
} ) ;
const json = await response . json ( ) ;
console . log ( json ) ; ` ;
2020-10-28 07:01:15 +03:00
const EXAMPLE _GET = ( key ) => ` const response = await fetch('https://slate.host/api/v1/get', {
2021-01-11 00:59:14 +03:00
method : 'POST' ,
2020-08-06 23:49:27 +03:00
headers : {
'Content-Type' : 'application/json' ,
// NOTE: your API key
Authorization : 'Basic ${key}' ,
} ,
body : JSON . stringify ( { data : {
// NOTE: optional, if you want your private slates too.
private : false
} } )
} ) ;
const json = await response . json ( ) ;
console . log ( json ) ; ` ;
2020-08-08 23:44:27 +03:00
const EXAMPLE _GET _SLATE _RESPONSE = ( key , slateId ) => ` {
2020-07-29 02:08:38 +03:00
data : {
2020-07-29 07:05:42 +03:00
id : '${slateId}' ,
2020-07-29 02:08:38 +03:00
updated _at : '2020-07-27T09:04:53.007Z' ,
created _at : '2020-07-27T09:04:53.007Z' ,
published _at : '2020-07-27T09:04:53.007Z' ,
slatename : 'slatename' ,
2020-11-16 22:53:52 +03:00
// NOTE(jim)
// This 'data' property is JSONB in our postgres database
// so every child is customizable.
2020-07-29 02:08:38 +03:00
data : {
name : "slatename" ,
public : true ,
objects : [
{
id : "data-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ,
2020-11-16 22:53:52 +03:00
name : "Name Example" ,
2020-07-29 02:08:38 +03:00
ownerId : "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ,
2020-11-16 22:53:52 +03:00
title : "The Final Storage System" ,
body : "You can store 4GB on Slate" ,
author : "Jason Leyser" ,
source : "https://google.com" ,
anything : "you-want" ,
// NOTE(jim)
// When you use the API you can make these API fields anything.
2020-08-28 11:50:45 +03:00
url : "https://slate.host/static/art-v2-social.png"
2020-07-29 02:08:38 +03:00
}
] ,
ownerId : "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
}
} ` ;
2020-08-22 07:25:34 +03:00
const EXAMPLE _UPLOAD _TO _SLATE = (
key ,
slateId
2020-11-05 09:25:53 +03:00
) => ` const url = 'https://uploads.slate.host/api/public/ ${ slateId } ';
2020-07-29 02:08:38 +03:00
let file = e . target . files [ 0 ] ;
let data = new FormData ( ) ;
2020-08-04 04:56:41 +03:00
data . append ( "data" , file ) ;
2020-07-29 02:08:38 +03:00
const response = await fetch ( url , {
2020-10-28 07:01:15 +03:00
method : 'POST' ,
2020-07-29 02:08:38 +03:00
headers : {
// NOTE: your API key
Authorization : 'Basic ${key}' ,
} ,
body : data
} ) ;
const json = await response . json ( ) ;
2020-07-29 02:24:15 +03:00
2020-08-08 23:44:27 +03:00
// NOTE: the URL to your asset will be available in the JSON response.
2020-07-29 02:08:38 +03:00
console . log ( json ) ; ` ;
2020-07-16 04:36:29 +03:00
export default class SceneSettingsDeveloper extends React . Component {
2020-07-28 09:54:15 +03:00
state = {
loading : false ,
} ;
_handleSave = async ( e ) => {
this . setState ( { loading : true } ) ;
const response = await Actions . generateAPIKey ( ) ;
2020-09-12 01:25:33 +03:00
2020-11-28 07:39:01 +03:00
Events . hasError ( response ) ;
2020-07-28 09:54:15 +03:00
this . setState ( { loading : false } ) ;
} ;
_handleDelete = async ( id ) => {
this . setState ( { loading : true } ) ;
2020-10-28 07:01:15 +03:00
if ( ! window . confirm ( "Are you sure you want to delete this key? This action is irreversible" ) ) {
2020-07-28 09:54:15 +03:00
this . setState ( { loading : false } ) ;
return ;
}
const response = await Actions . deleteAPIKey ( { id } ) ;
2020-11-28 07:39:01 +03:00
Events . hasError ( response ) ;
2020-10-31 02:12:20 +03:00
this . setState ( { loading : false } ) ;
2020-07-28 09:54:15 +03:00
} ;
2020-07-29 02:08:38 +03:00
async componentDidMount ( ) {
if ( ! this . props . viewer . keys ) {
return ;
}
if ( ! this . props . viewer . keys . length ) {
return ;
}
const response = await fetch ( "/api/v1/get-slate" , {
method : "POST" ,
headers : {
"Content-Type" : "application/json" ,
Authorization : ` Basic ${ this . props . viewer . keys [ 0 ] . key } ` ,
} ,
} ) ;
const json = await response . json ( ) ;
console . log ( json ) ;
}
2020-07-16 04:36:29 +03:00
render ( ) {
2020-07-29 02:08:38 +03:00
let key ;
if ( this . props . viewer . keys ) {
if ( this . props . viewer . keys . length ) {
key = this . props . viewer . keys [ 0 ] . key ;
}
}
let slateId = "your-slate-uuid-v4-value" ;
if ( this . props . viewer . slates ) {
if ( this . props . viewer . slates . length ) {
slateId = this . props . viewer . slates [ 0 ] . id ;
}
}
2020-07-28 09:54:15 +03:00
return (
< ScenePage >
2020-08-22 07:25:34 +03:00
< ScenePageHeader title = "Developer API" >
2020-10-28 07:01:15 +03:00
You can use your API key to get slates and add images to slates . You can have a total of
10 keys at any given time .
2020-08-22 07:25:34 +03:00
< / S c e n e P a g e H e a d e r >
< br / >
< br / >
2020-07-28 09:54:15 +03:00
{ this . props . viewer . keys . map ( ( k ) => {
return < Key key = { k . id } data = { k } onDelete = { this . _handleDelete } / > ;
} ) }
< div style = { { marginTop : 24 } } >
2020-10-28 07:01:15 +03:00
< System . ButtonPrimary onClick = { this . _handleSave } loading = { this . state . loading } >
2020-07-28 09:54:15 +03:00
Generate
< / S y s t e m . B u t t o n P r i m a r y >
< / d i v >
2020-07-29 02:08:38 +03:00
{ key ? (
< React . Fragment >
2020-08-06 23:49:27 +03:00
< System . DescriptionGroup
style = { { marginTop : 48 } }
label = "Get all slates"
description = "This API request will return all of your public slates."
/ >
2020-10-28 07:01:15 +03:00
< CodeBlock children = { EXAMPLE _GET ( key ) } style = { { maxWidth : "768px" } } / >
2020-08-06 23:49:27 +03:00
< br / >
< br / >
2020-07-29 02:08:38 +03:00
< System . DescriptionGroup
style = { { marginTop : 48 } }
label = "Get slate by ID"
2020-08-06 23:49:27 +03:00
description = "This API request will return a specific slate. If you don't provide an ID argument the response will contain the most recently modified slate."
/ >
2020-10-28 07:01:15 +03:00
< CodeBlock children = { EXAMPLE _GET _SLATE ( key , slateId ) } style = { { maxWidth : "768px" } } / >
2020-08-09 06:52:22 +03:00
< System . DescriptionGroup
style = { { marginTop : 48 , marginBottom : 16 } }
label = "Get slate by ID: Response"
2020-11-16 22:53:52 +03:00
description = "This is the shape of the response. Save it locally because you can send this JSON back to our API server using the route /api/v1/update-slate to update your slate."
2020-08-09 06:52:22 +03:00
/ >
2020-11-28 07:39:01 +03:00
< CodeBlock
children = { EXAMPLE _GET _SLATE _RESPONSE ( key , slateId ) }
style = { { maxWidth : "768px" } }
/ >
2020-07-29 02:08:38 +03:00
< System . DescriptionGroup
style = { { marginTop : 48 } }
label = "Upload data to slate by ID"
2020-11-16 22:53:52 +03:00
description = "This API endpoint will add a JavaScript file object to your slate."
2020-08-06 23:49:27 +03:00
/ >
2020-08-22 07:25:34 +03:00
< CodeBlock
children = { EXAMPLE _UPLOAD _TO _SLATE ( key , slateId ) }
style = { { maxWidth : "768px" } }
/ >
2020-11-16 11:47:56 +03:00
< System . DescriptionGroup
style = { { marginTop : 48 } }
label = "Update slate"
2020-11-16 22:53:52 +03:00
description = "This API endpoint will allow you to update a slate by sending your current locally modified version. This API endpoint allows for full customization so be careful."
2020-11-16 11:47:56 +03:00
/ >
2020-11-28 07:39:01 +03:00
< CodeBlock
children = { EXAMPLE _UPDATE _SLATE ( key , slateId ) }
style = { { maxWidth : "768px" } }
/ >
2020-07-29 02:08:38 +03:00
< / R e a c t . F r a g m e n t >
) : null }
2020-07-28 09:54:15 +03:00
< / S c e n e P a g e >
) ;
2020-07-16 04:36:29 +03:00
}
}