diff options
| author | jackyzha0 <[email protected]> | 2020-05-16 22:42:48 -0700 |
|---|---|---|
| committer | jackyzha0 <[email protected]> | 2020-05-16 22:42:48 -0700 |
| commit | 712bf925f8ae84a5a2fee87b8aa3c969df48fd50 (patch) | |
| tree | 839a68fe0c96e313a896d0cf0300523da275fac0 | |
| parent | Merge pull request #26 from jackyzha0/raw-button (diff) | |
| download | ctrl-v-712bf925f8ae84a5a2fee87b8aa3c969df48fd50.tar.xz ctrl-v-712bf925f8ae84a5a2fee87b8aa3c969df48fd50.zip | |
allow language saving
| -rw-r--r-- | backend/api/routes.go | 4 | ||||
| -rw-r--r-- | backend/db/db.go | 9 | ||||
| -rw-r--r-- | backend/db/schemas.go | 5 | ||||
| -rw-r--r-- | frontend/src/components/NewPaste.js | 7 | ||||
| -rw-r--r-- | frontend/src/components/Options.js | 6 | ||||
| -rw-r--r-- | frontend/src/components/PasteInfo.js | 1 | ||||
| -rw-r--r-- | frontend/src/components/ViewPaste.js | 11 | ||||
| -rw-r--r-- | frontend/src/components/renderers/Code.js | 2 | ||||
| -rw-r--r-- | frontend/src/css/index.css | 2 | ||||
| -rw-r--r-- | frontend/src/helpers/httpHelper.js | 1 |
10 files changed, 34 insertions, 14 deletions
diff --git a/backend/api/routes.go b/backend/api/routes.go index 26c932f..d05e99a 100644 --- a/backend/api/routes.go +++ b/backend/api/routes.go @@ -28,12 +28,13 @@ func insertFunc(w http.ResponseWriter, r *http.Request) { content := r.FormValue("content") title := r.FormValue("title") password := r.FormValue("password") + lang := r.FormValue("language") // get ip ip := getIP(r) // insert content - hash, err := db.New(ip, content, expiry, title, password) + hash, err := db.New(ip, content, expiry, title, password, lang) if err != nil { w.WriteHeader(http.StatusBadRequest) fmt.Fprintf(w, "%s", err.Error()) @@ -99,6 +100,7 @@ func handleGetPaste(w http.ResponseWriter, r *http.Request, parsedPassword strin "title": paste.Title, "content": paste.Content, "expiry": paste.Expiry, + "language": paste.Language, } jsonData, _ := json.Marshal(pasteMap) diff --git a/backend/db/db.go b/backend/db/db.go index df112d0..258b4df 100644 --- a/backend/db/db.go +++ b/backend/db/db.go @@ -28,7 +28,7 @@ const TitleLimit = 100 const ContentLimit = 100000 // creates a new paste with title, content and hash, returns the hash of the created paste -func New(ip, content, expiry, title, password string) (string, error) { +func New(ip, content, expiry, title, password, lang string) (string, error) { // generate hash from ip hash := security.GenerateURI(ip) @@ -40,9 +40,10 @@ func New(ip, content, expiry, title, password string) (string, error) { // create new struct new := Paste{ - Hash: hash, - Content: content, - Title: title, + Hash: hash, + Content: content, + Title: title, + Language: lang, } // if there is a password, encrypt content and hash the password diff --git a/backend/db/schemas.go b/backend/db/schemas.go index d3551fc..a3b55d0 100644 --- a/backend/db/schemas.go +++ b/backend/db/schemas.go @@ -10,9 +10,10 @@ import ( type Paste struct { ID bson.ObjectId `bson:"_id,omitempty"` Hash string - Content string - Expiry time.Time `bson:"expiry"` Title string + Content string + Language string Password string + Expiry time.Time `bson:"expiry"` Salt []byte } diff --git a/frontend/src/components/NewPaste.js b/frontend/src/components/NewPaste.js index 0b1c795..6e1b507 100644 --- a/frontend/src/components/NewPaste.js +++ b/frontend/src/components/NewPaste.js @@ -4,6 +4,7 @@ import OptionsContainer from './Options' import Error from './Err' import { PostNewPaste } from '../helpers/httpHelper' import PasteModal from './modals/PasteModal' +import { LANGS } from './renderers/Code' class NewPaste extends React.Component { constructor(props) { @@ -12,6 +13,7 @@ class NewPaste extends React.Component { title: '', content: '', pass: '', + language: LANGS.raw, expiry: '', hash: '', error: '', @@ -73,12 +75,13 @@ class NewPaste extends React.Component { content={this.state.content} maxLength="100000" id="pasteInput" /> - <input className="lt-button lt-shadow lt-hover" type="submit" value="new paste" /> - <Error ref={this.ErrorLabel} /> <OptionsContainer pass={this.state.pass} expiry={this.state.expiry} + lang={this.state.language} onChange={this.handleChange} /> + <input className="lt-button lt-shadow lt-hover" type="submit" value="new paste" /> + <Error ref={this.ErrorLabel} /> </form> ); } diff --git a/frontend/src/components/Options.js b/frontend/src/components/Options.js index 21e932a..f874c35 100644 --- a/frontend/src/components/Options.js +++ b/frontend/src/components/Options.js @@ -1,6 +1,6 @@ import React from 'react'; import styled from 'styled-components' -import { PassInput, ExpiryInput } from './Inputs' +import { PassInput, ExpiryInput, LangInput } from './Inputs' const Flex = styled.div` float: right; @@ -17,6 +17,10 @@ class OptionsContainer extends React.Component { value={this.props.pass} onChange={this.props.onChange} id="passwordInput" /> + <LangInput + value={this.props.lang} + onChange={this.props.onChange} + id="langInput" /> <ExpiryInput value={this.props.expiry} onChange={this.props.onChange} diff --git a/frontend/src/components/PasteInfo.js b/frontend/src/components/PasteInfo.js index 5217901..b1abda6 100644 --- a/frontend/src/components/PasteInfo.js +++ b/frontend/src/components/PasteInfo.js @@ -56,6 +56,7 @@ const PasteInfo = (props) => { </Button> </ButtonRow> <Bold>expires: </Bold>{props.expiry} + {props.err} </StyledDiv> </div> ); diff --git a/frontend/src/components/ViewPaste.js b/frontend/src/components/ViewPaste.js index 0b2ebe7..81d78f3 100644 --- a/frontend/src/components/ViewPaste.js +++ b/frontend/src/components/ViewPaste.js @@ -34,7 +34,6 @@ class ViewPaste extends React.Component { handleChange(event) { const target = event.target; const name = target.name; - console.log(target, name) this.setState({ [name]: target.value @@ -96,8 +95,8 @@ class ViewPaste extends React.Component { lang={this.state.language} theme={this.state.theme} onChange={this.handleChange} + err={<Error ref={this.ErrorLabel} />} expiry={this.state.expiry} /> - <Error ref={this.ErrorLabel} /> </div> ); } @@ -109,9 +108,11 @@ class ViewPaste extends React.Component { } setStateFromData(data) { + console.log(data) this.setState({ title: data.title, content: data.content, + language: data.language, expiry: this.fmtDateStr(data.expiry), }) } @@ -124,6 +125,12 @@ class ViewPaste extends React.Component { }).catch((error) => { const resp = error.response + // network err + if (!resp) { + this.ErrorLabel.current.showMessage(error) + return + } + // catch 401 unauth (password protected) if (resp.status === 401) { this.setState({hasPass: true}) diff --git a/frontend/src/components/renderers/Code.js b/frontend/src/components/renderers/Code.js index 12efc67..a024bb7 100644 --- a/frontend/src/components/renderers/Code.js +++ b/frontend/src/components/renderers/Code.js @@ -14,7 +14,7 @@ export const THEMES = Object.freeze({ export const LANGS = Object.freeze({ 'go': 'go', 'python': 'python', - 'javascript': 'javascript', + 'js': 'javascript', 'html': 'html', 'css': 'css', 'c': 'c', diff --git a/frontend/src/css/index.css b/frontend/src/css/index.css index c0e669c..f377fd5 100644 --- a/frontend/src/css/index.css +++ b/frontend/src/css/index.css @@ -69,7 +69,7 @@ code, pre { } .Dropdown-placeholder { - width: 7em; + width: 5em; } .Dropdown-menu { diff --git a/frontend/src/helpers/httpHelper.js b/frontend/src/helpers/httpHelper.js index 27695d5..28704f5 100644 --- a/frontend/src/helpers/httpHelper.js +++ b/frontend/src/helpers/httpHelper.js @@ -24,6 +24,7 @@ export function PostNewPaste(state) { var bodyFormData = new FormData(); bodyFormData.set('title', state.title); bodyFormData.set('content', state.content); + bodyFormData.set('language', state.language); bodyFormData.set('password', state.pass); bodyFormData.set('expiry', parseExpiry(state.expiry)); |