Skip to content

Commit

Permalink
feat: share button
Browse files Browse the repository at this point in the history
  • Loading branch information
avlyalin committed Jun 5, 2020
1 parent baf52bd commit 8a2bd08
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 9 deletions.
4 changes: 1 addition & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
"@hot-loader/react-dom": "^16.13.0",
"animate.css": "^4.1.0",
"classnames": "^2.2.6",
"copy-to-clipboard": "^3.3.1",
"core-js": "^3.6.5",
"firebase": "^7.14.1",
"prop-types": "^15.7.2",
Expand Down
16 changes: 15 additions & 1 deletion src/containers/app/app.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { hot } from 'react-hot-loader/root';
import React, { Component } from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import copy from 'copy-to-clipboard';
import {
CARDS_DICTIONARIES,
CARDS_TYPES,
FIELD_SIZES,
LANGUAGES,
TEAMS,
} from 'src/data/constants';
import { getGameSessionId } from 'src/utils/query-params';
import { getGameLink, getGameSessionId } from 'src/utils/query-params';
import * as FirebaseService from 'src/service';
import { getRemainingCardsCount } from 'src/utils/team-progress';
import { Loader } from 'src/components/loader';
Expand Down Expand Up @@ -168,6 +169,18 @@ class App extends Component {
);
}

shareSession() {
if (navigator.share) {
navigator.share({
title: 'Codenames - кодовые имена',
text: 'Присоединиться к игре',
url: getGameLink(this.sessionId),
});
} else {
copy(getGameLink(this.sessionId));
}
}

render() {
return (
<>
Expand All @@ -190,6 +203,7 @@ class App extends Component {
onChangeUsername={this.saveUsername.bind(this)}
onJoinTeam={this.joinTeam.bind(this)}
onJoinTeamAsCaptain={this.joinTeamAsCaptain.bind(this)}
onClickShare={this.shareSession.bind(this)}
/>
</Route>
<Route path="/game">
Expand Down
13 changes: 11 additions & 2 deletions src/containers/lobby/lobby.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function Lobby({
onChangeUsername,
onJoinTeam,
onJoinTeamAsCaptain,
onClickShare,
}) {
const onChangeFieldSize = (e) => {
onChangeSettings({ ...settings, fieldSize: e.target.value });
Expand Down Expand Up @@ -54,6 +55,8 @@ function Lobby({
bgColor = 'bg-red-linear md:bg-red-linear-image';
}

const shareIcon = navigator.share ? 'share-alt' : 'clone';

return (
<div
className={classnames(
Expand All @@ -76,8 +79,13 @@ function Lobby({
<FormGroup label={'ID сессии'}>
<InputGroup
append={
<Button classes="w-10" color={color} fullWidth={false}>
<FontAwesomeIcon icon="share-alt" size="lg" />
<Button
classes="w-10"
color={color}
fullWidth={false}
onClick={onClickShare}
>
<FontAwesomeIcon icon={shareIcon} size="lg" />
</Button>
}
>
Expand Down Expand Up @@ -165,6 +173,7 @@ Lobby.propTypes = {
onChangeSettings: PropTypes.func.isRequired,
onJoinTeam: PropTypes.func.isRequired,
onJoinTeamAsCaptain: PropTypes.func.isRequired,
onClickShare: PropTypes.func.isRequired,
};

export { Lobby };
2 changes: 2 additions & 0 deletions src/icons/icons.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
faUserPlus,
faSignOutAlt,
faTrophy,
faClone,
} from '@fortawesome/free-solid-svg-icons';

library.add(
Expand All @@ -19,4 +20,5 @@ library.add(
faUserPlus,
faSignOutAlt,
faTrophy,
faClone,
);
1 change: 1 addition & 0 deletions src/icons/icons.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const List = () => {
'user-plus',
'sign-out-alt',
'trophy',
'clone',
];
const rows = icons.map((icon, index) => (
<tr key={index}>
Expand Down
7 changes: 4 additions & 3 deletions src/utils/query-params.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
function getGameSessionId(url) {
export function getGameSessionId(url) {
const urlObj = new URL(url);
const searchParams = new URLSearchParams(urlObj.search);

return searchParams.get('gameId');
}

export { getGameSessionId };
export function getGameLink(sessionId) {
return `${window.location.href}?gameId=${sessionId}`;
}

0 comments on commit 8a2bd08

Please sign in to comment.