Skip to content
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

Switch to Go backend; modify backend request pattern. #235

Closed
wants to merge 8 commits into from
18 changes: 9 additions & 9 deletions src/components/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,19 @@ class Main extends React.Component {
saveText: "Saving...",
});

let programToUpdate = {};

programToUpdate[this.props.mostRecentProgram] = {
let programDataToUpdate = {
code: this.props.code,
};

fetch.updatePrograms(this.props.uid, programToUpdate).then(() => {
this.setState({
saveText: "Saved!",
});
fetch
.updateProgram(this.props.uid, this.props.mostRecentProgramID, programDataToUpdate)
.then(() => {
this.setState({
saveText: "Saved!",
});

setTimeout(this.resetSaveText, 3000);
});
setTimeout(this.resetSaveText, 3000);
});
this.props.cleanCode(this.props.mostRecentProgram); // Set code's "dirty" state to false
};

Expand Down
10 changes: 3 additions & 7 deletions src/components/Sketches/components/ConfirmDeleteModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,16 @@ class ConfirmDeleteModal extends React.Component {
onDeleteSubmit = () => {
let data = {
uid: this.props.uid,
docID: this.props.sketchKey,
name: this.props.sketchKey,
pid: this.props.pid,
};
try {
fetch
.deleteSketch(data)
.then(res => {
return res.json();
})
.then(json => {
if (!json.ok) {
if (!res.ok) {
this.setState({
spinner: false,
error: json.error || "Failed to create sketch, please try again later",
error: res.text() || "Failed to create sketch, please try again later",
});
return;
}
Expand Down
17 changes: 10 additions & 7 deletions src/components/Sketches/components/CreateSketchModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,21 @@ class CreateSketchModal extends React.Component {
fetch
.createSketch(data)
.then(res => {
return res.json();
})
.then(json => {
if (!json.ok) {
if (!res.ok) {
this.setState({
disableSubmit: false,
error: json.error || "Failed to create sketch, please try again later",
error: res.text() || "Failed to create sketch, please try again later",
});
return;
}
this.props.addProgram(json.data.key, json.data.programData || {});
this.props.setMostRecentProgram(json.data.key);

return res.json();
})
.then(json => {
// must be held constant as sketchCount changes following program addition.
let futureKey = this.props.sketchCount;
this.props.addProgram(futureKey, json || {});
this.props.setMostRecentProgram(futureKey);
this.setState({ redirect: true });
this.closeModal();
})
Expand Down
4 changes: 1 addition & 3 deletions src/components/Sketches/components/EditSketchModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,9 @@ class EditSketchModal extends React.Component {
doUpdate = true;
}
if (doUpdate) {
let updateData = {};
updateData[this.props.sketchKey] = data;
try {
fetch
.updatePrograms(this.props.uid, updateData)
.updateProgram(this.props.uid, data)
.then(res => {
return res.json();
})
Expand Down
6 changes: 6 additions & 0 deletions src/components/Sketches/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,19 @@ class Sketches extends React.Component {
onClose={() => this.setConfirmDeleteModalOpen(false)}
sketchName={this.state.selectedSketch}
sketchKey={this.state.selectedKey}
pid={
Array.from(this.props.programs)[this.state.selectedKey]
? Array.from(this.props.programs)[this.state.selectedKey].uid
: ""
}
Comment on lines +172 to +176
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a huge fan of how I made the changes to this file, but these felt the most natural without modifying much else. Thoughts?

/>
);

renderCreateSketchModal = () => (
<CreateSketchModalContainer
isOpen={this.state.createSketchModalOpen}
onClose={() => this.setCreateSketchModalOpen(false)}
sketchCount={Array.from(this.props.programs).length}
/>
);

Expand Down
9 changes: 6 additions & 3 deletions src/components/TextEditor/components/TextEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,15 @@ class TextEditor extends React.Component {
}

try {
let programToUpdate = {};
programToUpdate[this.props.mostRecentProgram] = {
let programDataToUpdate = {
code: this.props.code,
};

await fetch.updatePrograms(this.props.uid, programToUpdate);
await fetch.updateProgram(
this.props.uid,
this.props.mostRecentProgramID,
programDataToUpdate,
);
//TODO: add functionality to be able to tell whether the fetch failed
} catch (err) {
console.log(err);
Expand Down
7 changes: 3 additions & 4 deletions src/components/containers/MainContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const mapStateToProps = state => {
const dirty = state.programs.getIn([mostRecentProgram, "dirty"], false);
const name = state.programs.getIn([mostRecentProgram, "name"], "untitled");
const language = state.programs.getIn([mostRecentProgram, "language"], "txt");
const mostRecentProgramID = state.programs.getIn([mostRecentProgram, "uid"], "txt");

let listOfPrograms = [];

Expand All @@ -24,6 +25,7 @@ const mapStateToProps = state => {
return {
uid: state.userData.uid,
mostRecentProgram,
mostRecentProgramID,
code,
listOfPrograms,
screenWidth: state.ui.screenWidth,
Expand All @@ -47,9 +49,6 @@ const mapDispatchToProps = dispatch => {
};
};

const MainContainer = connect(
mapStateToProps,
mapDispatchToProps,
)(Main);
const MainContainer = connect(mapStateToProps, mapDispatchToProps)(Main);

export default MainContainer;
29 changes: 15 additions & 14 deletions src/lib/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import constants from "../constants";
*/
export const getUserData = async (uid = "", includePrograms = false) => {
const getUserDataEndpoint = (uid = "", includePrograms = false) =>
`${constants.SERVER_URL}/getUserData/${uid}${includePrograms ? "?programs=true" : ""}`;
`${constants.SERVER_URL}/user/get?uid=${uid}${includePrograms ? "&programs=true" : ""}`;

const options = {
method: "get",
Expand All @@ -25,7 +25,9 @@ export const getUserData = async (uid = "", includePrograms = false) => {

try {
let result = await fetch(getUserDataEndpoint(uid, includePrograms), options);
let { ok, data, error } = await result.json();
let ok = await result.ok;
let data = await result.json();
let error = (await result.ok) ? "" : await result.text();

return { ok, data, error };
} catch (err) {
Expand All @@ -40,7 +42,7 @@ export const getUserData = async (uid = "", includePrograms = false) => {
* @param {string} method HTTP method to make the request, defaults to post
*/

const makeServerRequest = (data, endpoint, method = "post") => {
const makeServerRequest = (data, endpoint, method = "POST") => {
let body = "";

// if the passed-in data object has at least 1 key, set the body to the stringified data object
Expand All @@ -67,23 +69,21 @@ const makeServerRequest = (data, endpoint, method = "post") => {
/**
* merges the JSON Parameter programs with the user document in Firestore; selectively updates based on passed-in keys
* @param {string} uid UID of user to selectively update programs
* @param {Object} programs object that contains only the keys of the project that need to be updated
* @param {Object} program object that contains only the keys of the project that need to be updated
*/

export const updatePrograms = (uid = "", programs) => {
const endpoint = `updatePrograms/${uid}`;
return makeServerRequest(programs, endpoint, "put");
export const updateProgram = (uid = "", pid = "", program) => {
const endpoint = `program/update?uid=${uid}&pid=${pid}`;
return makeServerRequest(program, endpoint, "PUT");
};

/**
* merges the JSON parameter userData with the user document in Firestore; selectively updates based on passed-in keys
* @param {string} uid UID for the user to update
* @param {Object} userData object that contains only the keys of the user data that need to be updated
* @param {Object} user object containing updated user with UID.
*/

export const updateUserData = (uid = "", userData) => {
const endpoint = `updateUserData/${uid}`;
return makeServerRequest(userData, endpoint);
export const updateUserData = async user => {
return makeServerRequest(user, "user/update", "PUT");
};

/**
Expand All @@ -92,7 +92,7 @@ export const updateUserData = (uid = "", userData) => {
*/

export const createSketch = data => {
return makeServerRequest(data, "createProgram");
return makeServerRequest(data, "program/create", "POST");
};

/**
Expand All @@ -101,5 +101,6 @@ export const createSketch = data => {
*/

export const deleteSketch = data => {
return makeServerRequest(data, "deleteProgram");
const endpoint = `program/delete?uid=${data.uid}&pid=${data.pid}`;
return fetch(data, endpoint, "DELETE");
};
6 changes: 3 additions & 3 deletions src/reducers/userDataReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function userDataReducer(state = initialState, action) {
case SET_DISPLAY_NAME:
let newName = action.value;
fetch
.updateUserData(state.uid, { displayName: newName })
.updateUserData({ uid: state.uid, displayName: newName })
.then(response => {})
.catch(err => {
state.error = err;
Expand All @@ -39,7 +39,7 @@ function userDataReducer(state = initialState, action) {
case SET_PHOTO_NAME:
let newPhotoName = action.photoName;
fetch
.updateUserData(state.uid, { photoName: newPhotoName })
.updateUserData({ uid: state.uid, photoName: newPhotoName })
.then(response => {
//if nothing went bad, keep the display name, otherwise, change it back (or dont, depends how we wanna do it)
})
Expand All @@ -50,7 +50,7 @@ function userDataReducer(state = initialState, action) {
return Object.assign({}, state, { photoName: newPhotoName });
case SET_MOST_RECENT_PROGRAM:
fetch
.updateUserData(state.uid, { mostRecentProgram: action.value })
.updateUserData({ uid: state.uid, mostRecentProgram: action.value })
.then(response => {})
.catch(err => {
state.error = err;
Expand Down