Implemented loading of short URLs

This commit is contained in:
Alejandro Celaya
2018-06-15 21:49:25 +02:00
parent e4356720d7
commit c0203f1336
16 changed files with 191 additions and 33 deletions

View File

@@ -0,0 +1,33 @@
import React from 'react';
import { connect } from 'react-redux';
import { listShortUrls } from './reducers/shortUrlsList';
export class ShortUrlsList extends React.Component {
componentDidMount() {
const { match } = this.props;
this.props.listShortUrls(match.params.serverId);
}
render() {
return (
<ul>
{this.renderShortUrls()}
</ul>
);
}
renderShortUrls() {
const { shortUrlsList } = this.props;
if (! shortUrlsList) {
return '<li><i>Loading...</i></li>';
}
return shortUrlsList.map(shortUrl => (
<li key={shortUrl.shortCode}>{`${shortUrl.shortCode}`}</li>
));
}
}
export default connect(state => ({
shortUrlsList: state.shortUrlsList
}), { listShortUrls })(ShortUrlsList);

View File

@@ -0,0 +1,21 @@
import { LIST_SHORT_URLS } from '../../reducers/types';
import ServersService from '../../servers/services';
import ShlinkApiClient from '../../api/ShlinkApiClient';
export default function shortUrlsListReducer(state = [], action) {
switch (action.type) {
case LIST_SHORT_URLS:
return action.shortUrls;
default:
return state;
}
}
export const listShortUrls = (serverId) => {
return async dispatch => {
const selectedServer = ServersService.findServerById(serverId);
ShlinkApiClient.setConfig(selectedServer);
dispatch({ type: LIST_SHORT_URLS, shortUrls: await ShlinkApiClient.listShortUrls() });
};
};