Skip to content

Commit f92ccfe

Browse files
committed
fix(archived-headlines): restore headline and update state with restored headline
1 parent 4d5ad84 commit f92ccfe

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

lib/content_management/bloc/archived_headlines/archived_headlines_bloc.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,11 @@ class ArchivedHeadlinesBloc
7575
emit(state.copyWith(headlines: updatedHeadlines));
7676

7777
try {
78-
await _headlinesRepository.update(
78+
final restoredHeadline = await _headlinesRepository.update(
7979
id: event.id,
8080
item: headlineToRestore.copyWith(status: ContentStatus.active),
8181
);
82+
emit(state.copyWith(restoredHeadline: restoredHeadline));
8283
} on HttpException catch (e) {
8384
emit(state.copyWith(headlines: originalHeadlines, exception: e));
8485
} catch (e) {

lib/content_management/bloc/archived_headlines/archived_headlines_state.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,31 @@ class ArchivedHeadlinesState extends Equatable {
1616
this.cursor,
1717
this.hasMore = false,
1818
this.exception,
19+
this.restoredHeadline,
1920
});
2021

2122
final ArchivedHeadlinesStatus status;
2223
final List<Headline> headlines;
2324
final String? cursor;
2425
final bool hasMore;
2526
final HttpException? exception;
27+
final Headline? restoredHeadline;
2628

2729
ArchivedHeadlinesState copyWith({
2830
ArchivedHeadlinesStatus? status,
2931
List<Headline>? headlines,
3032
String? cursor,
3133
bool? hasMore,
3234
HttpException? exception,
35+
Headline? restoredHeadline,
3336
}) {
3437
return ArchivedHeadlinesState(
3538
status: status ?? this.status,
3639
headlines: headlines ?? this.headlines,
3740
cursor: cursor ?? this.cursor,
3841
hasMore: hasMore ?? this.hasMore,
3942
exception: exception ?? this.exception,
43+
restoredHeadline: restoredHeadline,
4044
);
4145
}
4246

@@ -47,5 +51,6 @@ class ArchivedHeadlinesState extends Equatable {
4751
cursor,
4852
hasMore,
4953
exception,
54+
restoredHeadline,
5055
];
5156
}

lib/content_management/view/archived_headlines_page.dart

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:data_table_2/data_table_2.dart';
44
import 'package:flutter/material.dart';
55
import 'package:flutter_bloc/flutter_bloc.dart';
66
import 'package:flutter_news_app_web_dashboard_full_source_code/content_management/bloc/archived_headlines/archived_headlines_bloc.dart';
7+
import 'package:flutter_news_app_web_dashboard_full_source_code/content_management/bloc/content_management_bloc.dart';
78
import 'package:flutter_news_app_web_dashboard_full_source_code/l10n/app_localizations.dart';
89
import 'package:flutter_news_app_web_dashboard_full_source_code/l10n/l10n.dart';
910
import 'package:intl/intl.dart';
@@ -35,16 +36,27 @@ class _ArchivedHeadlinesView extends StatelessWidget {
3536
),
3637
body: Padding(
3738
padding: const EdgeInsets.all(AppSpacing.lg),
38-
child: BlocBuilder<ArchivedHeadlinesBloc, ArchivedHeadlinesState>(
39-
builder: (context, state) {
40-
if (state.status == ArchivedHeadlinesStatus.loading &&
41-
state.headlines.isEmpty) {
42-
return LoadingStateWidget(
43-
icon: Icons.newspaper,
44-
headline: l10n.loadingArchivedHeadlines,
45-
subheadline: l10n.pleaseWait,
46-
);
39+
child: BlocListener<ArchivedHeadlinesBloc, ArchivedHeadlinesState>(
40+
listenWhen: (previous, current) =>
41+
previous.status != current.status,
42+
listener: (context, state) {
43+
if (state.status == ArchivedHeadlinesStatus.success &&
44+
state.restoredHeadline != null) {
45+
context.read<ContentManagementBloc>().add(
46+
const LoadHeadlinesRequested(limit: kDefaultRowsPerPage),
47+
);
4748
}
49+
},
50+
child: BlocBuilder<ArchivedHeadlinesBloc, ArchivedHeadlinesState>(
51+
builder: (context, state) {
52+
if (state.status == ArchivedHeadlinesStatus.loading &&
53+
state.headlines.isEmpty) {
54+
return LoadingStateWidget(
55+
icon: Icons.newspaper,
56+
headline: l10n.loadingArchivedHeadlines,
57+
subheadline: l10n.pleaseWait,
58+
);
59+
}
4860

4961
if (state.status == ArchivedHeadlinesStatus.failure) {
5062
return FailureStateWidget(
@@ -116,16 +128,17 @@ class _ArchivedHeadlinesView extends StatelessWidget {
116128
dataRowHeight: 56,
117129
columnSpacing: AppSpacing.md,
118130
horizontalMargin: AppSpacing.md,
119-
),
120131
),
121-
],
132+
),
122133
);
123-
},
134+
},
135+
),
124136
),
125137
),
126138
);
127139
}
128140
}
141+
}
129142

130143
class _HeadlinesDataSource extends DataTableSource {
131144
_HeadlinesDataSource({

0 commit comments

Comments
 (0)