challenges: require challenge title, when user subscribe add new tag of challenge name. unsubscribe deletes tasks

This commit is contained in:
Tyler Renelle 2013-05-17 23:33:24 +01:00
parent 9b41712ca6
commit e7558139e6
3 changed files with 76 additions and 54 deletions

View file

@ -16,7 +16,6 @@
"guid": "*",
"moment": "*",
"stripe": "*",
"lodash": "1.0.x",
"coffee-script": "1.4.x",
"underscore": "*",
"mongoskin": "*",
@ -26,7 +25,8 @@
"resolve": "~0.2.3",
"expect.js": "~0.2.0",
"derby-i18n": "git://github.com/switz/derby-i18n#master",
"relative-date": "~1.1.1"
"relative-date": "~1.1.1",
"lodash": "~1.2.1"
},
"private": true,
"subdomain": "habitrpg",

View file

@ -1,4 +1,5 @@
_ = require 'underscore'
lodash = require 'lodash'
module.exports.app = (appExports, model) ->
browser = require './browser'
@ -30,15 +31,31 @@ module.exports.app = (appExports, model) ->
model.set '_challenge.creating', false
appExports.challengeSubscribe = (e) ->
userChallenges = user.get('challenges')
chal = e.get()
# Add challenge name as a tag for user
tags = user.get('tags')
unless tags and _.findWhere(tags,{id: chal.id})
model.push('_user.tags', {id: chal.id, name: chal.name})
tags = {}; tags[chal.id] = true
# Add all challenge's tasks to user's tasks
userChallenges = user.get('challenges')
user.unshift('challenges', chal.id) unless userChallenges and (userChallenges.indexOf(chal.id) != -1)
_.each ['habit', 'daily', 'todo', 'reward'], (type) ->
_.each chal["#{type}s"], (task) -> model.push("_#{type}List", task)
_.each chal["#{type}s"], (task) ->
task.tags = tags
task.challenge = chal.id
model.push("_#{type}List", task)
appExports.challengeUnsubscribe = (e) ->
i = user.get('challenges')?.indexOf e.get('id')
chal = e.get()
i = user.get('challenges')?.indexOf chal.id
user.remove("challenges.#{i}") if i? and i != -1
_.each ['habit', 'daily', 'todo', 'reward'], (type) ->
_.each chal["#{type}s"], (task) ->
model.remove "_#{type}List", lodash.findIndex(model.get("_#{type}List",{id:task.id}))
model.del "_user.tasks.#{task.id}"
appExports.challengeCollapse = (e, el) ->
$(el).next().toggle()

View file

@ -33,12 +33,10 @@
<div class="tab-pane" id="challengesViewParty">
{#each _party.challenges as :challenge}
<div>
<div class='pull-right'>
{#if indexOf(_user.challenges,:challenge.id)}
<a x-bind="click:challengeUnsubscribe" class='btn btn-large btn-danger'><i class='icon-ban-circle'></i> Unsubscribe</a>
{else}
<a x-bind="click:challengeSubscribe" class='btn btn-large btn-success'><i class='icon-ok'></i> Subscribe</a>
{/}
<a x-bind="click:challengeUnsubscribe" class='btn btn-large btn-danger {#unless indexOf(_user.challenges,:challenge.id)}hidden{/}'><i class='icon-ban-circle'></i> Unsubscribe</a>
<a x-bind="click:challengeSubscribe" class='btn btn-large btn-success {#if indexOf(_user.challenges,:challenge.id)}hidden{/}'><i class='icon-ok'></i> Subscribe</a>
</div>
<h3 x-bind="click:challengeCollapse"><i class='icon-chevron-down icon-chevron-right'></i> {:challenge.name} (by {:challenge.user})</h3>
@ -51,6 +49,7 @@
rewards={:challenge.rewards} />
</div>
<hr/>
</div>
{/}
</div>
@ -70,7 +69,6 @@
{#unless _challenge.creating}
<a x-bind='click:challengeCreate' class='btn btn-large btn-success'>Create New Challenge</a>
{else}
<input id='newChallengeName' type='text' value={_challenge.new.name} placeholder="Challenge Title" required/>
<div class="grid">
<app:tasks:task-lists
@ -81,6 +79,10 @@
editable=true />
</div>
<form x-bind="submit:challengeSave">
<input id='newChallengeName' type='text' value={_challenge.new.name} placeholder="Challenge Title" required />
<fieldset>
<div>
<select>
@ -116,8 +118,11 @@
</div>
</fieldset>
<input type='submit' x-bind="click:challengeSave" class='btn btn-large btn-success' value='Save' />
<input type='submit' class='btn btn-large btn-success' value='Save' />
<input type='button' x-bind='click:challengeDiscard' class='btn btn-large btn-danger' value=Discard />
</form>
{/}
</div>