Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MBS-11838: Convert Edit Release edit to React #2230

Merged
merged 1 commit into from Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 44 additions & 15 deletions lib/MusicBrainz/Server/Edit/Release/Edit.pm
Expand Up @@ -28,6 +28,7 @@ use MusicBrainz::Server::Edit::Utils qw(
verify_artist_credits
);
use MusicBrainz::Server::Entity::PartialDate;
use MusicBrainz::Server::Entity::Util::JSON qw( to_json_array to_json_object );
use MusicBrainz::Server::Translation qw( N_l );

no if $] >= 5.018, warnings => "experimental::smartmatch";
Expand All @@ -49,6 +50,7 @@ sub edit_type { $EDIT_RELEASE_EDIT }
sub edit_name { N_l('Edit release') }
sub _edit_model { 'Release' }
sub release_id { shift->data->{entity}{id} }
sub edit_template_react { 'EditRelease' }

sub change_fields
{
Expand Down Expand Up @@ -143,31 +145,56 @@ sub build_display_data
my ($self, $loaded) = @_;

my %map = (
packaging => [ qw( packaging_id ReleasePackaging )],
status => [ qw( status_id ReleaseStatus )],
group => [ qw( release_group_id ReleaseGroup )],
language => [ qw( language_id Language )],
script => [ qw( script_id Script )],
name => 'name',
comment => 'comment',
packaging => [ qw( packaging_id ReleasePackaging )],
status => [ qw( status_id ReleaseStatus )],
release_group => [ qw( release_group_id ReleaseGroup )],
language => [ qw( language_id Language )],
script => [ qw( script_id Script )],
name => 'name',
comment => 'comment',
);

my $data = changed_display_data($self->data, $loaded, %map);

if (exists $self->data->{new}{artist_credit}) {
$data->{artist_credit} = {
new => artist_credit_from_loaded_definition($loaded, $self->data->{new}{artist_credit}),
old => artist_credit_from_loaded_definition($loaded, $self->data->{old}{artist_credit})
new => to_json_object(artist_credit_from_loaded_definition($loaded, $self->data->{new}{artist_credit})),
old => to_json_object(artist_credit_from_loaded_definition($loaded, $self->data->{old}{artist_credit}))
}
}

if (exists $self->data->{new}{barcode}) {
$data->{barcode} = {
new => Barcode->new($self->data->{new}{barcode}),
old => Barcode->new($self->data->{old}{barcode}),
new => $self->data->{new}{barcode},
old => $self->data->{old}{barcode},
};
}

if (exists $data->{packaging}) {
$data->{packaging}{old} = to_json_object($data->{packaging}{old});
$data->{packaging}{new} = to_json_object($data->{packaging}{new});
}

if (exists $data->{status}) {
$data->{status}{old} = to_json_object($data->{status}{old});
$data->{status}{new} = to_json_object($data->{status}{new});
}

if (exists $data->{language}) {
$data->{language}{old} = to_json_object($data->{language}{old});
$data->{language}{new} = to_json_object($data->{language}{new});
}

if (exists $data->{script}) {
$data->{script}{old} = to_json_object($data->{script}{old});
$data->{script}{new} = to_json_object($data->{script}{new});
}

if (exists $data->{release_group}) {
$data->{release_group}{old} = to_json_object($data->{release_group}{old});
$data->{release_group}{new} = to_json_object($data->{release_group}{new});
}

if (exists $self->data->{new}{events}) {
my $inflate_event = sub {
my $event = shift;
Expand All @@ -181,15 +208,17 @@ sub build_display_data

$data->{events} = {
map {
$_ => [
$_ => to_json_array([
map { $inflate_event->($_) } @{ $self->data->{$_}{events} }
]
])
} qw( old new )
};
}

$data->{release} = $loaded->{Release}{ $self->data->{entity}{id} }
|| Release->new( name => $self->data->{entity}{name} );
$data->{release} = to_json_object(
$loaded->{Release}{ $self->data->{entity}{id} } ||
Release->new( name => $self->data->{entity}{name} )
);

return $data;
}
Expand Down
15 changes: 9 additions & 6 deletions lib/MusicBrainz/Server/Edit/Release/EditArtist.pm
Expand Up @@ -16,6 +16,7 @@ use MusicBrainz::Server::Edit::Utils qw(
artist_credit_from_loaded_definition
load_artist_credit_definitions
);
use MusicBrainz::Server::Entity::Util::JSON qw( to_json_array to_json_object );
use MusicBrainz::Server::Translation qw( l N_l );

extends 'MusicBrainz::Server::Edit';
Expand All @@ -29,11 +30,11 @@ with 'MusicBrainz::Server::Edit::Role::AllowAmending' => {

use aliased 'MusicBrainz::Server::Entity::Release';

sub edit_name { N_l('Edit release') }
sub edit_name { N_l('Edit releasesssss') }
sub edit_kind { 'edit' }
sub edit_type { $EDIT_RELEASE_ARTIST }
sub release_id { shift->data->{entity}{id} }
sub edit_template { "edit_release" }
sub edit_template_react { 'EditRelease' }

has '+data' => (
isa => Dict[
Expand Down Expand Up @@ -95,14 +96,16 @@ sub build_display_data {

if (exists $self->data->{new_artist_credit}) {
$data->{artist_credit} = {
new => artist_credit_from_loaded_definition($loaded, $self->data->{new_artist_credit}),
old => artist_credit_from_loaded_definition($loaded, $self->data->{old_artist_credit})
new => to_json_object(artist_credit_from_loaded_definition($loaded, $self->data->{new_artist_credit})),
old => to_json_object(artist_credit_from_loaded_definition($loaded, $self->data->{old_artist_credit}))
}
}

$data->{update_tracklists} = $self->data->{update_tracklists};
$data->{release} = $loaded->{Release}{ $self->data->{entity}{id} }
|| Release->new( name => $self->data->{entity}{name} );
$data->{release} = to_json_object(
$loaded->{Release}{ $self->data->{entity}{id} }
|| Release->new( id => $self->data->{entity}{id}, name => $self->data->{entity}{name} )
);

return $data;
}
Expand Down
190 changes: 190 additions & 0 deletions root/edit/details/EditRelease.js
@@ -0,0 +1,190 @@
/*
* @flow strict-local
* Copyright (C) 2021 MetaBrainz Foundation
*
* This file is part of MusicBrainz, the open internet music database,
* and is licensed under the GPL version 2, or (at your option) any
* later version: http://www.gnu.org/licenses/gpl-2.0.txt
*/

import * as React from 'react';

import DescriptiveLink from
'../../static/scripts/common/components/DescriptiveLink';
import {DeletedLink} from '../../static/scripts/common/components/EntityLink';
import ExpandedArtistCredit from
'../../static/scripts/common/components/ExpandedArtistCredit';
import formatBarcode from '../../static/scripts/common/utility/formatBarcode';
import Diff from '../../static/scripts/edit/components/edit/Diff';
import FullChangeDiff
from '../../static/scripts/edit/components/edit/FullChangeDiff';
import ReleaseEventsDiff
from '../../static/scripts/edit/components/edit/ReleaseEventsDiff';
import WordDiff from '../../static/scripts/edit/components/edit/WordDiff';

type EditReleaseEditT = {
...EditT,
+display_data: {
+artist_credit?: CompT<ArtistCreditT>,
+barcode?: CompT<string | null>,
+comment?: CompT<string | null>,
+events?: CompT<$ReadOnlyArray<ReleaseEventT>>,
+language?: CompT<LanguageT | null>,
+name?: CompT<string>,
+packaging?: CompT<ReleasePackagingT | null>,
+release: ReleaseT,
+release_group?: CompT<ReleaseGroupT>,
+script?: CompT<ScriptT | null>,
+status?: CompT<ReleaseStatusT | null>,
+update_tracklists?: boolean,
},
};

type Props = {
+edit: EditReleaseEditT,
};

const EditRelease = ({edit}: Props): React.MixedElement => {
const display = edit.display_data;
const name = display.name;
const artistCredit = display.artist_credit;
const releaseGroup = display.release_group;
const comment = display.comment;
const language = display.language;
const packaging = display.packaging;
const script = display.script;
const status = display.status;
const barcode = display.barcode;
const releaseEvents = display.events;

return (
<table className="details edit-release">
{edit.preview /*:: === true */ ? null : (
<tr>
<th>{l('Release:')}</th>
<td colSpan="2">
{display.release
? <DescriptiveLink entity={display.release} />
: null}
</td>
</tr>
)}

{name ? (
<WordDiff
label={addColonText(l('Name'))}
newText={name.new}
oldText={name.old}
/>
) : null}

{artistCredit ? (
<tr>
<th>{addColonText(l('Artist'))}</th>
<td className="old">
<ExpandedArtistCredit
artistCredit={artistCredit.old}
/>
</td>
<td className="new">
<ExpandedArtistCredit
artistCredit={artistCredit.new}
/>
</td>
</tr>
) : null}

{releaseGroup ? (
<FullChangeDiff
label={addColonText(l('Release group'))}
newContent={releaseGroup.new
? <DescriptiveLink entity={releaseGroup.new} />
: <DeletedLink allowNew={false} name={null} />}
oldContent={releaseGroup.old
? <DescriptiveLink entity={releaseGroup.old} />
: <DeletedLink allowNew={false} name={null} />}
/>
) : null}

{comment ? (
<WordDiff
label={addColonText(l('Disambiguation'))}
newText={comment.new ?? ''}
oldText={comment.old ?? ''}
/>
) : null}

{status ? (
<FullChangeDiff
label={lp('Status:', 'release status')}
newContent={status.new?.name
? lp_attributes(status.new.name, 'release_status')
: ''}
oldContent={status.old?.name
? lp_attributes(status.old.name, 'release_status')
: ''}
/>
) : null}

{language ? (
<FullChangeDiff
label={addColonText(l('Language'))}
newContent={language.new?.name
? l_languages(language.new.name)
: ''}
oldContent={language.old?.name
? l_languages(language.old.name)
: ''}
/>
) : null}

{script ? (
<FullChangeDiff
label={addColonText(l('Script'))}
newContent={script.new?.name
? l_scripts(script.new.name)
: ''}
oldContent={script.old?.name
? l_scripts(script.old.name)
: ''}
/>
) : null}

{packaging ? (
<FullChangeDiff
label={addColonText(l('Packaging'))}
newContent={packaging.new?.name
? l_scripts(packaging.new.name)
: ''}
oldContent={packaging.old?.name
? l_scripts(packaging.old.name)
: ''}
/>
) : null}

{barcode ? (
<Diff
label={addColonText(l('Barcode'))}
newText={formatBarcode(barcode.new)}
oldText={formatBarcode(barcode.old)}
/>
) : null}

{releaseEvents ? (
<ReleaseEventsDiff
newEvents={releaseEvents.new}
oldEvents={releaseEvents.old}
/>
) : null}

{display.update_tracklists /*:: === true */ ? (
<tr>
<th>{addColon(l('Note'))}</th>
<td>{l('This edit also changed the track artists.')}</td>
</tr>
) : null}
</table>
);
};

export default EditRelease;