-
Notifications
You must be signed in to change notification settings - Fork 347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add to SGF Library Directly From Game #2364
Closed
JoshuaNascimento
wants to merge
9
commits into
online-go:devel
from
JoshuaNascimento:InGame_SGF_Library_Add
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5d7d69a
Initial concept for ingame sgf add to library function
JoshuaNascimento 86d22eb
Commit code up to functionality of obtaining current sgf file from ap…
JoshuaNascimento 051fe4d
Created custom fetch request specifying content-type to correct the 4…
JoshuaNascimento 7cdafc6
Sucessfully implemented barebones feature to add game directly to a p…
JoshuaNascimento 6d53307
added text field so user can enter in a custom SGF File name
JoshuaNascimento 6bfcb7b
Added ability to create a collection when a user has no collections w…
JoshuaNascimento 4383c40
Basic styling for component as well as checks for default values in t…
JoshuaNascimento 5b426dc
changed position of save to library in tooltip bar
JoshuaNascimento 9135356
Merge branch 'online-go:devel' into InGame_SGF_Library_Add
JoshuaNascimento File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright (C) Online-Go.com | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
.GameLibraryModal.Modal { | ||
width: 100%; | ||
max-width: 40rem; | ||
padding: 1rem; | ||
display: flex; | ||
flex-direction: row; | ||
align-items: center; | ||
|
||
.file-name-inputfield{ | ||
margin: 1rem; | ||
} | ||
} | ||
.collection-list { | ||
display:flex; | ||
width: 100%; | ||
flex-direction: column; | ||
align-content: stretch; | ||
align-items: center; | ||
} | ||
|
||
.collection-row { | ||
width: 100% | ||
display:flex; | ||
justify-content: space-evenly; | ||
align-items: center; | ||
} | ||
|
||
.cell { | ||
max-width: 20rem; | ||
padding-left: 0.5em; | ||
display: table-cell; | ||
text-align: left; | ||
position: relative; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
/* | ||
* Copyright (C) Online-Go.com | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import * as React from "react"; | ||
import { openModal, Modal } from "Modal"; | ||
import { api1, post } from "requests"; | ||
import { errorAlerter } from "misc"; | ||
import { _ } from "translate"; | ||
|
||
interface Events {} | ||
|
||
interface GameLibraryModalProperties { | ||
userID: number; | ||
userLibrary: any; | ||
gameID: number; | ||
} | ||
|
||
export class GameLibraryModal extends Modal<Events, GameLibraryModalProperties, any> { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
gameName: "", | ||
collectionName: "", | ||
collections: [], | ||
gameAdded: false, | ||
}; | ||
} | ||
|
||
componentDidMount(): void { | ||
const userLibrary: any = JSON.stringify(this.props.userLibrary); | ||
const userlibraryJSON = JSON.parse(userLibrary); | ||
this.setState({ collections: userlibraryJSON }); | ||
} | ||
|
||
setGameName = (ev) => { | ||
this.setState({ gameName: ev.target.value }); | ||
}; | ||
|
||
setCollectionName = (ev) => { | ||
this.setState({ collectionName: ev.target.value }); | ||
}; | ||
|
||
resetTextFields = () => { | ||
this.setState({ gameName: "" }); | ||
this.setState({ collectionName: "" }); | ||
}; | ||
|
||
async addToLibrary(collection) { | ||
const url = api1(`games/${this.props.gameID}/sgf`); | ||
await fetch(url, { | ||
method: "GET", | ||
// Specify the content-type of the request otherwise the "games/%%/sgf" endpoint will respond with a 415 error code | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}) | ||
.then((response) => response.blob()) | ||
.then((data) => { | ||
// Pull sgfName from value implemented by user setting a default value if empty | ||
let gameName = this.state.gameName; | ||
if (this.state.gameName === "") { | ||
gameName = `My Game #${this.props.gameID}`; | ||
} | ||
const gameFile = new File([data as BlobPart], `${gameName}.sgf`, { | ||
type: "application/x-go-sgf", | ||
lastModified: new Date().getTime(), | ||
}); | ||
// Create post request adding SGF File to the given collection using collection's id | ||
post("me/games/sgf/%%", collection[0], gameFile).catch(errorAlerter); | ||
this.resetTextFields(); | ||
this.close(); // Closes modal after creating post request | ||
this.setState({ gameAdded: true }); | ||
}) | ||
.catch(errorAlerter); | ||
} | ||
|
||
async createCollection() { | ||
if (this.state.collectionName === "") { | ||
await this.setState({ collectionName: "My_Collection" }); | ||
} | ||
// Create a new library within the user's libraries | ||
post("library/%%/collections", this.props.userID, { | ||
name: this.state.collectionName, | ||
}) | ||
.then((res) => { | ||
// Pull the newly created collection's id to be used to identify which collection we add our SGF File too | ||
const collection_id = [res.collection_id]; | ||
this.addToLibrary(collection_id).catch(errorAlerter); | ||
}) | ||
.catch(errorAlerter); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div className="Modal GameLibraryModal"> | ||
<div className="collection-list"> | ||
<h1>Libraries</h1> | ||
|
||
{/* If user has collections in their library map and render them. Otherwise; allow user to create collection */} | ||
{this.state.collections.length > 0 ? ( | ||
<> | ||
<span> | ||
<i className="fa fa-file" /> | ||
<input | ||
className="file-name-inputfield" | ||
type="text" | ||
value={this.state.gameName} | ||
onChange={this.setGameName} | ||
placeholder={_("Insert SGF File Name")} | ||
/> | ||
</span> | ||
|
||
{this.state.collections.map((data) => ( | ||
<div className="collection-row" key={data.id}> | ||
<span className="cell"> | ||
<h1>{data[1]}</h1> | ||
</span> | ||
<span className="cell"> | ||
<button onClick={() => this.addToLibrary(data)}>add</button> | ||
</span> | ||
</div> | ||
))} | ||
</> | ||
) : ( | ||
<div className="collection-row"> | ||
<span className="cell"> | ||
<i className="fa fa-file" /> | ||
<input | ||
className="file-name-inputfield" | ||
type="text" | ||
value={this.state.gameName} | ||
onChange={this.setGameName} | ||
placeholder={_("Insert SGF File Name")} | ||
/> | ||
</span> | ||
<span className="cell"> | ||
<i className="fa fa-folder" /> | ||
<input | ||
type="text" | ||
value={this.state.collectionName} | ||
onChange={this.setCollectionName} | ||
placeholder={_("Insert Collection Name")} | ||
/> | ||
</span> | ||
<span className="cell"> | ||
<button onClick={() => this.createCollection()}> | ||
Create Collection | ||
</button> | ||
</span> | ||
</div> | ||
)} | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export function openGameLibraryModal(userID: number, userLibrary: any, gameID: number): void { | ||
openModal( | ||
<GameLibraryModal userID={userID} userLibrary={userLibrary} gameID={gameID} fastDismiss />, | ||
); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to open the modal and show a Loading indicator, you can probably just drop in
<Loading />
while you wait for the data, that way people know their action has been registered and they just need to be patient while we wait for the internet.