Skip to content

Commit

Permalink
feat(feed): 完成动态话题后台删除功能
Browse files Browse the repository at this point in the history
  • Loading branch information
medz committed Aug 6, 2018
1 parent 29c364c commit 28f2a07
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 3 deletions.
11 changes: 11 additions & 0 deletions app/Models/FeedTopic.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Zhiyi\Component\ZhiyiPlus\PlusComponentFeed\Models\Feed as FeedModel;

class FeedTopic extends Model
{
Expand All @@ -47,6 +48,16 @@ public function users(): BelongsToMany
->using(FeedTopicUserLink::class);
}

public function feeds(): BelongsToMany
{
$table = (new FeedTopicLink)->getTable();

return $this
->belongsToMany(FeedModel::class, $table, 'topic_id', 'feed_id')
->using(FeedTopicLink::class);
;
}

public function creator(): HasOne
{
return $this->hasOne(User::class, 'id', 'creator_user_id');
Expand Down
6 changes: 6 additions & 0 deletions packages/slimkit-plus-feed/admin/api/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ export function hotToggle(id) {
validateStatus: status => status === 204
});
}

export function destroy(id) {
return adminRequest.delete(`topics/${id}`, {
validateStatus: status => status === 204,
});
}
29 changes: 27 additions & 2 deletions packages/slimkit-plus-feed/admin/pages/topic/List.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
list as listRequest,
add as addRequest,
update as updateRequest,
hotToggle as hotToggleRequest
hotToggle as hotToggleRequest,
destroy as destroyRequest
} from '../../api/topic';

class List extends React.Component {
Expand Down Expand Up @@ -124,11 +125,34 @@ class List extends React.Component {
})
}),
error: message => this.setState({
submitting: falae,
submitting: false,
message: { type: 'error', text: message, open: true }
})
});

handleDestroyTopic = (id, fn) => fn({
submit: () => {
this.setState({ submitting: true });

return destroyRequest(id);
},
success: () => this.setState({
submitting: false,
message: { type: 'success', text: '删除成功', open: true },
topics: lodash.reduce(this.state.topics, (topics, topic) => {
if (parseInt(topic.id) != parseInt(id)) {
topics.push(topic);
}

return topics;
}, [])
}),
error: message => this.setState({
submitting: false,
message: { type: 'error', text: message, open: true }
})
})

render() {
return <View
showSearchBar={this.state.showSearchBar}
Expand All @@ -147,6 +171,7 @@ class List extends React.Component {
handleCloseMessage={this.handleCloseMessage}
handleSubmitEditForm={this.handleSubmitEditForm}
handleToggleTopicHot={this.handleToggleTopicHot}
handleDestroyTopic={this.handleDestroyTopic}
/>;
}

Expand Down
50 changes: 49 additions & 1 deletion packages/slimkit-plus-feed/admin/pages/topic/List.view.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class ListView extends React.Component {
message: PropTypes.object.isRequired,
handleCloseMessage: PropTypes.func.isRequired,
handleSubmitEditForm: PropTypes.func.isRequired,
handleToggleTopicHot: PropTypes.func.isRequired
handleToggleTopicHot: PropTypes.func.isRequired,
handleDestroyTopic: PropTypes.func.isRequired
}

state = {
Expand Down Expand Up @@ -122,6 +123,15 @@ class ListView extends React.Component {
success();
}).catch(({ response: { data = { message: '操作失败' } } = {} }) => error(data)))

handleOpenDeleteTopic = (id) => this.setState({ deleteTopic: id })

handleCloseDeleteTopic = () => this.setState({ deleteTopic: 0 })

handleSubmitDeleteTopic = () => this.props.handleDestroyTopic(this.state.deleteTopic, ({ submit, success, error }) => submit().then(() => {
this.handleCloseDeleteTopic();
success();
}).catch(({ response: { data = { message: '操作失败' } } = {} }) => error(data)))

render() {
let { classes, topics } = this.props;
return (
Expand Down Expand Up @@ -200,6 +210,7 @@ class ListView extends React.Component {
mini={true}
className={classes.actionsFab}
color="secondary"
onClick={() => this.handleOpenDeleteTopic(topic.id)}
>
<DeleteIcon />
</Button>
Expand Down Expand Up @@ -338,6 +349,43 @@ class ListView extends React.Component {
</div>
</Modal>

{/* 删除确认提示 */}
<Modal open={!!this.state.deleteTopic}>
<div className={classes.modalWrap} >
<Paper
classes={{
root: classes.modalPager
}}
>
确认要删除话题嘛?
<div className={classes.modalActions}>

{this.props.submitting && <CircularProgress size={36}/>}

<Button
color="primary"
className={classes.actionsFab}
variant="contained"
onClick={this.handleCloseDeleteTopic}
disabled={this.props.submitting}
>
取&nbsp;消
</Button>

<Button
color="secondary"
className={classes.actionsFab}
variant="contained"
onClick={this.handleSubmitDeleteTopic}
disabled={this.props.submitting}
>
删&nbsp;除
</Button>
</div>
</Paper>
</div>
</Modal>

</div>
);
}
Expand Down
1 change: 1 addition & 0 deletions packages/slimkit-plus-feed/routes/new-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
$route->get('topics', \Zhiyi\Plus\Packages\Feed\Admin\Controllers\Topic::class.'@adminListTopics');
$route->post('topics', \Zhiyi\Plus\Packages\Feed\Admin\Controllers\Topic::class.'@create');
$route->put('topics/{topic}', \Zhiyi\Plus\Packages\Feed\Admin\Controllers\Topic::class.'@update');
$route->delete('topics/{topic}', \Zhiyi\Plus\Packages\Feed\Admin\Controllers\Topic::class.'@destroy');
$route->put('topics/{topic}/hot-toggle', \Zhiyi\Plus\Packages\Feed\Admin\Controllers\Topic::class.'@hotToggle');
});
16 changes: 16 additions & 0 deletions packages/slimkit-plus-feed/src/Admin/Controllers/Topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,20 @@ public function hotToggle(TopicModel $topic)

return response('', 204);
}

public function destroy(TopicModel $topic)
{
return $topic->getConnection()->transaction(function () use ($topic) {
// 删除关联用户
$topic->users()->sync([], true);

// 删除动态关联
$topic->feeds()->sync([], true);

// 删除话题
$topic->delete();

return response('', 204);
});
}
}

0 comments on commit 28f2a07

Please sign in to comment.