Skip to content

Commit bc8b1b3

Browse files
committed
Update logic to link a source file with an event
1 parent d000795 commit bc8b1b3

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

lib/api/service/client/sound_client_api.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,18 @@ class SoundClientApi extends BaseApi<SoundEventModel> {
5252
(jsonSource) => SoundFileSource.fromJson(jsonSource as Map<String, dynamic>),
5353
);
5454
}
55+
56+
/// Links a [SoundFileSource] to a specific sound event.
57+
///
58+
/// The request is the updated entry from the backend
59+
/// - [modelId]: The unique id of the sound event
60+
/// - [fileToLink]: The [SoundFileSource] which should be linked
61+
Future<SoundFileSource> linkFile(String modelId, SoundFileSource fileToLink) async {
62+
final baseUri = Uri.parse(apiClient.baseUrl);
63+
final uri = baseUri.replace(
64+
path: '${baseUri.path}/$endpoint/$modelId/sources',
65+
);
66+
final result = await apiClient.dio.postUri(uri, data: fileToLink.toJson());
67+
return SoundFileSource.fromJson(result.data);
68+
}
5569
}

lib/api/state/actions/sound/sound_file_actions.dart

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,31 @@ class InitSoundFileAction extends ReduxAction<AppState> {
3232
}
3333
}
3434

35-
class SoundFileSourceAddAction extends ReduxAction<AppState> {
35+
class SoundFileLinkAction extends ReduxAction<AppState> {
3636

37-
final SoundFileSource model;
37+
final SoundFileSource source;
3838

39-
SoundFileSourceAddAction(this.model);
39+
SoundFileLinkAction(this.source);
4040

4141
@override
4242
Future<AppState?> reduce() async {
4343
if (state.selectedSoundEvent == null) return null;
4444

4545
final SoundEventModel soundModel = state.selectedSoundEvent!;
46+
final List<SoundFileSource> list = List.of(soundModel.files.items, growable: true);
47+
final SoundClientApi soundApi = ApiService().soundApi as SoundClientApi;
48+
final SoundFileSource linkedFile = await soundApi.linkFile(soundModel.id!, source);
4649

47-
// Create a NEW list that is growable and add the new item
48-
final List<SoundFileSource> newItemsList = List.of(soundModel.files.items, growable: true);
49-
newItemsList.add(model); // 'model' is your new SoundFileSource
50+
list.add(linkedFile);
5051

5152
// Create a new PaginatedResult with the new list and updated counts
5253
final PaginatedResult<SoundFileSource> updatedSources = soundModel.files.copyWith(
53-
items: newItemsList,
54+
items: list,
5455
totalItems: soundModel.files.totalItems + 1, // Adjust counts as necessary
5556
// Potentially update totalPages if this new item pushes it to a new page
5657
);
5758
return state.copyWith(selectedSoundEvent: soundModel.copyWith(files: updatedSources));
59+
5860
}
5961
}
6062

lib/feature/sound/sound_file_entries.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class _SoundFileEntriesState extends State<SoundFileEntryPage> {
9898
type: type,
9999
weight: weight,
100100
);
101-
context.dispatch(SoundFileSourceAddAction(fileSource));
101+
context.dispatch(SoundFileLinkAction(fileSource));
102102
// Handle save logic here
103103
},
104104
),

0 commit comments

Comments
 (0)