Skip to content

Commit 531e49d

Browse files
committed
fix: unable to edit song due to missing instrument fields array
1 parent 7569a0b commit 531e49d

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

web/src/modules/song-edit/components/client/context/EditSong.context.tsx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export type useEditSongProviderType = {
2929
register: UseFormRegister<EditSongForm>;
3030
errors: FieldErrors<EditSongForm>;
3131
song: SongFileType | null;
32+
instrumentSounds: string[];
33+
setInstrumentSound: (index: number, value: string) => void;
3234
sendError: string | null;
3335
isSubmitting: boolean;
3436
loadSong: (id: string, username: string, song: UploadSongDtoType) => void;
@@ -50,6 +52,7 @@ export const EditSongProvider = ({
5052
});
5153

5254
const [song, setSong] = useState<SongFileType | null>(null);
55+
const [instrumentSounds, setInstrumentSounds] = useState<string[]>([]);
5356
const [sendError, setSendError] = useState<string | null>(null);
5457
const [isSubmitting, setIsSubmitting] = useState(false);
5558

@@ -248,20 +251,26 @@ export const EditSongProvider = ({
248251
// convert to song
249252
const song = await parseSongFromBuffer(songFile);
250253

251-
// pad instruments array for safety
252-
const songInstruments = Array(song.instruments.length).fill('');
253-
254-
songData.customInstruments.forEach((instrument, index) => {
255-
songInstruments[index] = instrument;
256-
});
257-
254+
// set instruments array
255+
const songInstruments = songData.customInstruments;
256+
setInstrumentSounds(songInstruments);
258257
formMethods.setValue('customInstruments', songInstruments);
259258

260259
setSong(song);
261260
},
262261
[formMethods, setSong],
263262
);
264263

264+
const setInstrumentSound = useCallback(
265+
(index: number, value: string) => {
266+
const newValues = [...instrumentSounds];
267+
newValues[index] = value;
268+
setInstrumentSounds(newValues);
269+
formMethods.setValue('customInstruments', newValues);
270+
},
271+
[formMethods, instrumentSounds],
272+
);
273+
265274
const setSongId = useCallback(
266275
(id: string) => formMethods.setValue('id', id),
267276
[formMethods],
@@ -289,6 +298,8 @@ export const EditSongProvider = ({
289298
value={{
290299
formMethods,
291300
submitSong,
301+
instrumentSounds,
302+
setInstrumentSound,
292303
register,
293304
errors,
294305
song,

0 commit comments

Comments
 (0)