Skip to content

Commit

Permalink
Generate client appp
Browse files Browse the repository at this point in the history
```
docker-compose exec client generate-api-platform-client
```
  • Loading branch information
bjrbhre committed Mar 20, 2019
1 parent 136b8b1 commit 6f55803
Show file tree
Hide file tree
Showing 37 changed files with 2,690 additions and 0 deletions.
45 changes: 45 additions & 0 deletions client/src/actions/book/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { SubmissionError } from 'redux-form';
import { fetch } from '../../utils/dataAccess';

export function error(error) {
return { type: 'BOOK_CREATE_ERROR', error };
}

export function loading(loading) {
return { type: 'BOOK_CREATE_LOADING', loading };
}

export function success(created) {
return { type: 'BOOK_CREATE_SUCCESS', created };
}

export function create(values) {
return dispatch => {
dispatch(loading(true));

return fetch('/books', { method: 'POST', body: JSON.stringify(values) })
.then(response => {
dispatch(loading(false));

return response.json();
})
.then(retrieved => dispatch(success(retrieved)))
.catch(e => {
dispatch(loading(false));

if (e instanceof SubmissionError) {
dispatch(error(e.errors._error));
throw e;
}

dispatch(error(e.message));
});
};
}

export function reset() {
return dispatch => {
dispatch(loading(false));
dispatch(error(null));
};
}
29 changes: 29 additions & 0 deletions client/src/actions/book/delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { fetch } from '../../utils/dataAccess';

export function error(error) {
return { type: 'BOOK_DELETE_ERROR', error };
}

export function loading(loading) {
return { type: 'BOOK_DELETE_LOADING', loading };
}

export function success(deleted) {
return { type: 'BOOK_DELETE_SUCCESS', deleted };
}

export function del(item) {
return dispatch => {
dispatch(loading(true));

return fetch(item['@id'], { method: 'DELETE' })
.then(() => {
dispatch(loading(false));
dispatch(success(item));
})
.catch(e => {
dispatch(loading(false));
dispatch(error(e.message));
});
};
}
85 changes: 85 additions & 0 deletions client/src/actions/book/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import {
fetch,
normalize,
extractHubURL,
mercureSubscribe as subscribe
} from '../../utils/dataAccess';
import { success as deleteSuccess } from './delete';

export function error(error) {
return { type: 'BOOK_LIST_ERROR', error };
}

export function loading(loading) {
return { type: 'BOOK_LIST_LOADING', loading };
}

export function success(retrieved) {
return { type: 'BOOK_LIST_SUCCESS', retrieved };
}

export function list(page = '/books') {
return dispatch => {
dispatch(loading(true));
dispatch(error(''));

fetch(page)
.then(response =>
response
.json()
.then(retrieved => ({ retrieved, hubURL: extractHubURL(response) }))
)
.then(({ retrieved, hubURL }) => {
retrieved = normalize(retrieved);

dispatch(loading(false));
dispatch(success(retrieved));

if (hubURL)
dispatch(
mercureSubscribe(
hubURL,
retrieved['hydra:member'].map(i => i['@id'])
)
);
})
.catch(e => {
dispatch(loading(false));
dispatch(error(e.message));
});
};
}

export function reset(eventSource) {
return dispatch => {
if (eventSource) eventSource.close();

dispatch({ type: 'BOOK_LIST_RESET' });
dispatch(deleteSuccess(null));
};
}

export function mercureSubscribe(hubURL, topics) {
return dispatch => {
const eventSource = subscribe(hubURL, topics);
dispatch(mercureOpen(eventSource));
eventSource.addEventListener('message', event =>
dispatch(mercureMessage(normalize(JSON.parse(event.data))))
);
};
}

export function mercureOpen(eventSource) {
return { type: 'BOOK_LIST_MERCURE_OPEN', eventSource };
}

export function mercureMessage(retrieved) {
return dispatch => {
if (1 === Object.keys(retrieved).length) {
dispatch({ type: 'BOOK_LIST_MERCURE_DELETED', retrieved });
return;
}

dispatch({ type: 'BOOK_LIST_MERCURE_MESSAGE', retrieved });
};
}
78 changes: 78 additions & 0 deletions client/src/actions/book/show.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import {
fetch,
extractHubURL,
normalize,
mercureSubscribe as subscribe
} from '../../utils/dataAccess';

export function error(error) {
return { type: 'BOOK_SHOW_ERROR', error };
}

export function loading(loading) {
return { type: 'BOOK_SHOW_LOADING', loading };
}

export function success(retrieved) {
return { type: 'BOOK_SHOW_SUCCESS', retrieved };
}

export function retrieve(id) {
return dispatch => {
dispatch(loading(true));

return fetch(id)
.then(response =>
response
.json()
.then(retrieved => ({ retrieved, hubURL: extractHubURL(response) }))
)
.then(({ retrieved, hubURL }) => {
retrieved = normalize(retrieved);

dispatch(loading(false));
dispatch(success(retrieved));

if (hubURL) dispatch(mercureSubscribe(hubURL, retrieved['@id']));
})
.catch(e => {
dispatch(loading(false));
dispatch(error(e.message));
});
};
}

export function reset(eventSource) {
return dispatch => {
if (eventSource) eventSource.close();

dispatch({ type: 'BOOK_SHOW_RESET' });
dispatch(error(null));
dispatch(loading(false));
};
}

export function mercureSubscribe(hubURL, topic) {
return dispatch => {
const eventSource = subscribe(hubURL, [topic]);
dispatch(mercureOpen(eventSource));
eventSource.addEventListener('message', event =>
dispatch(mercureMessage(normalize(JSON.parse(event.data))))
);
};
}

export function mercureOpen(eventSource) {
return { type: 'BOOK_SHOW_MERCURE_OPEN', eventSource };
}

export function mercureMessage(retrieved) {
return dispatch => {
if (1 === Object.keys(retrieved).length) {
dispatch({ type: 'BOOK_SHOW_MERCURE_DELETED', retrieved });
return;
}

dispatch({ type: 'BOOK_SHOW_MERCURE_MESSAGE', retrieved });
};
}
131 changes: 131 additions & 0 deletions client/src/actions/book/update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { SubmissionError } from 'redux-form';
import {
fetch,
extractHubURL,
normalize,
mercureSubscribe as subscribe
} from '../../utils/dataAccess';
import { success as createSuccess } from './create';
import { loading, error } from './delete';

export function retrieveError(retrieveError) {
return { type: 'BOOK_UPDATE_RETRIEVE_ERROR', retrieveError };
}

export function retrieveLoading(retrieveLoading) {
return { type: 'BOOK_UPDATE_RETRIEVE_LOADING', retrieveLoading };
}

export function retrieveSuccess(retrieved) {
return { type: 'BOOK_UPDATE_RETRIEVE_SUCCESS', retrieved };
}

export function retrieve(id) {
return dispatch => {
dispatch(retrieveLoading(true));

return fetch(id)
.then(response =>
response
.json()
.then(retrieved => ({ retrieved, hubURL: extractHubURL(response) }))
)
.then(({ retrieved, hubURL }) => {
retrieved = normalize(retrieved);

dispatch(retrieveLoading(false));
dispatch(retrieveSuccess(retrieved));

if (hubURL) dispatch(mercureSubscribe(hubURL, retrieved['@id']));
})
.catch(e => {
dispatch(retrieveLoading(false));
dispatch(retrieveError(e.message));
});
};
}

export function updateError(updateError) {
return { type: 'BOOK_UPDATE_UPDATE_ERROR', updateError };
}

export function updateLoading(updateLoading) {
return { type: 'BOOK_UPDATE_UPDATE_LOADING', updateLoading };
}

export function updateSuccess(updated) {
return { type: 'BOOK_UPDATE_UPDATE_SUCCESS', updated };
}

export function update(item, values) {
return dispatch => {
dispatch(updateError(null));
dispatch(createSuccess(null));
dispatch(updateLoading(true));

return fetch(item['@id'], {
method: 'PUT',
headers: new Headers({ 'Content-Type': 'application/ld+json' }),
body: JSON.stringify(values)
})
.then(response =>
response
.json()
.then(retrieved => ({ retrieved, hubURL: extractHubURL(response) }))
)
.then(({ retrieved, hubURL }) => {
retrieved = normalize(retrieved);

dispatch(updateLoading(false));
dispatch(updateSuccess(retrieved));

if (hubURL) dispatch(mercureSubscribe(hubURL, retrieved['@id']));
})
.catch(e => {
dispatch(updateLoading(false));

if (e instanceof SubmissionError) {
dispatch(updateError(e.errors._error));
throw e;
}

dispatch(updateError(e.message));
});
};
}

export function reset(eventSource) {
return dispatch => {
if (eventSource) eventSource.close();

dispatch({ type: 'BOOK_UPDATE_RESET' });
dispatch(error(null));
dispatch(loading(false));
dispatch(createSuccess(null));
};
}

export function mercureSubscribe(hubURL, topic) {
return dispatch => {
const eventSource = subscribe(hubURL, [topic]);
dispatch(mercureOpen(eventSource));
eventSource.addEventListener('message', event =>
dispatch(mercureMessage(normalize(JSON.parse(event.data))))
);
};
}

export function mercureOpen(eventSource) {
return { type: 'BOOK_UPDATE_MERCURE_OPEN', eventSource };
}

export function mercureMessage(retrieved) {
return dispatch => {
if (1 === Object.keys(retrieved).length) {
dispatch({ type: 'BOOK_UPDATE_MERCURE_DELETED', retrieved });
return;
}

dispatch({ type: 'BOOK_UPDATE_MERCURE_MESSAGE', retrieved });
};
}
Loading

0 comments on commit 6f55803

Please sign in to comment.