Skip to content

Commit

Permalink
fix edit link and set id in the front-end
Browse files Browse the repository at this point in the history
  • Loading branch information
azahmed096 committed Jul 25, 2018
1 parent 014b54f commit cea0da0
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 37 deletions.
10 changes: 5 additions & 5 deletions functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ exports.moduleCount = functions.database.ref('/stories/{storyId}')
});

exports.orderOfNewModule = functions.database.ref("/stories/{storyId}/modules/{moduleId}")
.onCreate((snap, context) => {
let storyRef = snap
.onCreate((moduleSnap, context) => {
let storyRef = moduleSnap
.ref
.parent // module
.parent; // storyId;
Expand All @@ -36,10 +36,10 @@ exports.orderOfNewModule = functions.database.ref("/stories/{storyId}/modules/{m
.catch(err => reject(err))
.then(count => {
Promise.all([
snap.ref.child("order").set(count),
moduleSnap.ref.child("order").set(count),
storyRef.ref.child("moduleCount").set(count + 1),
snap.ref.child("id").set(context.params.moduleId)
]).then((x) => resolve(x))
moduleSnap.ref.child("id").set(context.params.moduleId)
]).then(resolve)
.catch(err => reject(err));
})
.catch(err => reject(err));
Expand Down
4 changes: 2 additions & 2 deletions src/components/ModuleEditCard/ModuleEditCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class ModuleEditCard extends React.Component {
order,
onOrderDown,
deleteDisabled,
onEdit,
editUrl,
orderDisabled,
text } = this.props;

Expand All @@ -30,7 +30,7 @@ export default class ModuleEditCard extends React.Component {
if (window.confirm("This is IRREVERSIBLE!"))
onDelete(e);
}}><Trash size="20"/></Link>
<Link className="module-edit row" to="#" onClick={onEdit}><Edit size="20" /></Link>
<Link className="module-edit row" to={editUrl}><Edit size="20" /></Link>
<div className="row module-up-down-container align-self-end">
<Link disabled={orderDisabled} className="module-up" to="#" onClick={(e) => onOrderUp(e)}><ChevronUp size="20"/></Link>
<Link disabled={orderDisabled} className="module-down" to="#" onClick={(e) => onOrderDown(e)}><ChevronDown size="20"/></Link>
Expand Down
4 changes: 2 additions & 2 deletions src/components/ModuleList/ModuleList.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class ModuleList extends React.Component {
upOrder,
downOrder,
onSaveOrder,
onEdit,
getEditUrl,
modules
} = this.props;

Expand All @@ -41,7 +41,7 @@ export default class ModuleList extends React.Component {

return <ModuleEditCard
deleteDisabled={!canDelete}
onEdit={() => onEdit(module.id)}
editUrl={getEditUrl(module.id)}
orderDisabled={!canChangeOrder}
onDelete={() => onDelete(module.id)}
onOrderUp={() => upOrder(module.id)}
Expand Down
11 changes: 9 additions & 2 deletions src/containers/views/AddFunfact/AddFunfact.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Button from '../../../components/button/Button';
import Navbar from '../../../components/nav/Navbar';
import Footer from '../../../components/footer/Footer';
import { Link } from 'react-router-dom';
import { resolve } from '../../../../node_modules/url';

class AddFunfact extends React.Component {
render() {
Expand All @@ -26,15 +27,21 @@ class AddFunfact extends React.Component {
<div className="add-module-container">
<div className="add-module-box container">
<form onSubmit={this.props.handleSubmit(({text}) => {
return firebaseDatabase
return new Promise((resolve, reject) => {
firebaseDatabase
.ref('stories/')
.child(storyId)
.child("modules")
.push({
text,
contentType: "funfact"
}).then(() => history.push(`/teacher/dashboard/${storyId}`));
})
.then(snap => snap.ref.child("id").set(snap.key))
.then(resolve)
.catch(reject)
.then(() => history.push(`/teacher/dashboard/${storyId}`));
})
})
}>
<div className="row justify-content-center">
<h3> Add weetje for { storyId }</h3>
Expand Down
28 changes: 16 additions & 12 deletions src/containers/views/AddImageQuiz/AddImageQuiz.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,22 @@ class AddImageQuiz extends React.Component {
contentType: "imagequiz"
}).then(snap => {
let moduleId = snap.key;
Promise.all(
[correctImage, otherImage1, otherImage2]
.map((file, index) => {
return firebaseStorage()
.ref()
.child(user.uid)
.child("story")
.child(storyId)
.child(moduleId)
.child(names[index])
.put(file)
})).then(tasks => {
let promises = [correctImage, otherImage1, otherImage2]
.map((file, index) => {
return firebaseStorage()
.ref()
.child(user.uid)
.child("story")
.child(storyId)
.child(moduleId)
.child(names[index])
.put(file)
});
promises.push(snap.ref.child("id").set(snap.key)) // this promise
// dont give a task
Promise.all(promises).then(tasks => {
// so let's pop it's result there
tasks.pop();
let urlArray = tasks.map(t => t.metadata.downloadURLs[0]);
firebaseDatabase
.ref("stories/")
Expand Down
12 changes: 7 additions & 5 deletions src/containers/views/AddQuiz/AddQuiz.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ class AddQuiz extends React.Component {
<div className="add-module-box container">
<h3> Add quiz for {storyId}</h3>
<form onSubmit={this.props.handleSubmit(
({...fields, correct, other1, other2}) => {
let o = firebaseDatabase
({correct, other1, other2, ...fields}) => {
return new Promise(resolve => {
firebaseDatabase
.ref('stories/')
.child(storyId);
o.child("modules")
.child(storyId)
.child("modules")
.push({
contentType: "quiz",
options: {
Expand All @@ -42,8 +43,9 @@ class AddQuiz extends React.Component {
other2
},
...fields
})
}).then(snap => snap.ref.child("id").set(snap.key).then(resolve))
.then(() => history.push(`/teacher/dashboard/${storyId}`))
})
})
}>
<div className="row">
Expand Down
15 changes: 9 additions & 6 deletions src/containers/views/AddTextBlock/AddTextBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ class AddImageQuiz extends React.Component {

return (
<div className="page">
<Navbar logout={logout} user={user}/>
<Navbar logout={logout} user={user} />
<div className="add-module-container">
<div className="add-module-box container">
<h3> Add textblock for {storyId}</h3>
<form onSubmit={this.props.handleSubmit(
({ text, textBlockImage }) => {

let promise = new Promise((resolve, reject) => {
let promise = new Promise(resolve => {
firebaseDatabase
.ref('stories/')
.child(storyId)
Expand All @@ -41,7 +41,9 @@ class AddImageQuiz extends React.Component {
contentType: "textblock"
}).then(snap => {
let moduleId = snap.key;
firebaseStorage()
let setIdPromise = snap.ref.child("id").set(moduleId);
let imagePromise = new Promise(imageResolve => {
firebaseStorage()
.ref()
.child(user.uid)
.child("story")
Expand All @@ -60,10 +62,11 @@ class AddImageQuiz extends React.Component {
.child(moduleId)
.child("resource")
.set(task.metadata.downloadURLs[0])
.catch(reject)
.then(resolve)
.then(imageResolve)
})
.catch(reject)
});
Promise.all([setIdPromise, imagePromise])
.then(resolve)
})
});

Expand Down
4 changes: 1 addition & 3 deletions src/containers/views/StoryDashboard/StoryDashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ class StoryDashboard extends React.Component {

<div className="row">
<ModuleList
onEdit={moduleId => {
let route = `/teacher/dashboard/${story.id}/edit/${story.modules[moduleId].contentType}/${moduleId}`;
history.push(route)}}
getEditUrl={moduleId => `/teacher/dashboard/${story.id}/edit/${story.modules[moduleId].contentType}/${moduleId}`}
canDelete={!isDirty && !isModuleLoading}
canChangeOrder={!isModuleLoading && !isUploadingModules}
canSaveOrReset={isDirty && !isUploadingModules}
Expand Down

0 comments on commit cea0da0

Please sign in to comment.