Skip to content

Commit 578f5d2

Browse files
authored
Merge pull request #873 from clubby789/update-url
2 parents 56618ba + c9f8ff7 commit 578f5d2

File tree

6 files changed

+43
-17
lines changed

6 files changed

+43
-17
lines changed

tests/spec/features/navigation_spec.rb

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,11 @@
11
require 'spec_helper'
22
require 'support/editor'
3+
require 'support/matchers/be_at_url'
34
require 'support/playground_actions'
45

56
RSpec.feature "Navigating between pages", type: :feature, js: true do
67
include PlaygroundActions
78

8-
RSpec::Matchers.define :be_at_url do |path, query = {}|
9-
match do |page|
10-
uri = URI::parse(page.current_url)
11-
expect(uri.path).to eql(path)
12-
13-
query = query.map { |k, v| [k.to_s, Array(v).map(&:to_s)] }.to_h
14-
query_hash = CGI::parse(uri.query || '')
15-
expect(query_hash).to include(query)
16-
end
17-
18-
failure_message do |page|
19-
"expected that #{page.current_url} would be #{path} with the query parameters #{query}"
20-
end
21-
end
22-
239
# This is kind of a test of the router library too, so if that ever
2410
# gets extracted, a chunk of these tests can be removed.
2511

tests/spec/features/sharing_with_others_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'spec_helper'
22
require 'support/editor'
3+
require 'support/matchers/be_at_url'
34
require 'support/playground_actions'
45

56
RSpec.feature "Sharing the code with others", type: :feature, js: true do
@@ -23,6 +24,10 @@
2324
direct_link = find_link("Direct link to the gist")[:href]
2425
urlo_link = find_link("Open a new thread in the Rust user forum")[:href]
2526

27+
# Have we automatically added the gist to our URL?
28+
gist_id = Addressable::URI.parse(perma_link).query_values['gist']
29+
expect(page).to be_at_url('/', gist: gist_id)
30+
2631
# Navigate away so we can tell that we go back to the same page
2732
visit 'about:blank'
2833

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
RSpec::Matchers.define :be_at_url do |path, query = {}|
2+
match do |page|
3+
uri = URI::parse(page.current_url)
4+
expect(uri.path).to eql(path)
5+
6+
query = query.map { |k, v| [k.to_s, Array(v).map(&:to_s)] }.to_h
7+
query_hash = CGI::parse(uri.query || '')
8+
expect(query_hash).to include(query)
9+
end
10+
11+
failure_message do |page|
12+
"expected that #{page.current_url} would be #{path} with the query parameters #{query}"
13+
end
14+
end

ui/frontend/Output/Gist.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { State } from '../reducers';
77
import * as selectors from '../selectors';
88

99
import Loader from './Loader';
10+
import Section from './Section';
1011

1112
import styles from './Gist.module.css';
1213

@@ -58,13 +59,17 @@ const Links: React.FC = () => {
5859
const gistUrl = useSelector((state: State) => state.output.gist.url);
5960
const permalink = useSelector(selectors.permalinkSelector);
6061
const urloUrl = useSelector(selectors.urloUrlSelector);
62+
const textChanged = useSelector(selectors.textChangedSinceShareSelector);
6163

6264
return (
6365
<Fragment>
6466
<Copied href={permalink}>Permalink to the playground</Copied>
6567
{ gistUrl ? <Copied href={gistUrl}>Direct link to the gist</Copied> : null }
6668
<Copied href={codeUrl}>Embedded code in link</Copied>
6769
<NewWindow href={urloUrl}>Open a new thread in the Rust user forum</NewWindow>
70+
{textChanged ? <Section kind="warning" label="Code changed">
71+
Source code has been changed since gist was saved
72+
</Section>: null }
6873
</Fragment>
6974
);
7075
};

ui/frontend/Router.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,29 @@ interface Substate {
2020
channel: Channel;
2121
mode: Mode;
2222
edition: Edition;
23+
};
24+
output: {
25+
gist: {
26+
id?: string;
27+
}
2328
}
2429
}
2530

26-
const stateSelector = ({ page, configuration: { channel, mode, edition } }: State): Substate => ({
31+
const stateSelector = ({ page, configuration: { channel, mode, edition}, output }: State): Substate => ({
2732
page,
2833
configuration: {
2934
channel,
3035
mode,
3136
edition,
3237
},
38+
output: {
39+
gist: {
40+
id: output.gist.id,
41+
},
42+
},
3343
});
3444

35-
const stateToLocation = ({ page, configuration }: Substate): Partial<Path> => {
45+
const stateToLocation = ({ page, configuration, output }: Substate): Partial<Path> => {
3646
switch (page) {
3747
case 'help': {
3848
return {
@@ -45,6 +55,7 @@ const stateToLocation = ({ page, configuration }: Substate): Partial<Path> => {
4555
version: configuration.channel,
4656
mode: configuration.mode,
4757
edition: configuration.edition,
58+
gist: output.gist.id,
4859
};
4960
return {
5061
pathname: `/?${qs.stringify(query)}`,

ui/frontend/selectors/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ export const permalinkSelector = createSelector(
205205
},
206206
);
207207

208+
export const textChangedSinceShareSelector = createSelector(
209+
codeSelector, gistSelector,
210+
(code, gist) => code !== gist.code
211+
)
212+
208213
const codeBlock = (code: string, language = '') =>
209214
'```' + language + `\n${code}\n` + '```';
210215

0 commit comments

Comments
 (0)