Base du projet 'application ticketing'

This commit is contained in:
2020-02-17 22:28:57 +01:00
parent 2a72ac97ac
commit afa734f96d
64 changed files with 1798 additions and 685 deletions

View File

@ -1,98 +0,0 @@
import { call, put, take } from 'redux-saga/effects';
import { eventChannel } from 'redux-saga'
import {
SEND_MESSAGE_FAILURE, SEND_MESSAGE_SUCCESS,
FETCH_MESSAGES_FAILURE, FETCH_MESSAGES_SUCCESS
} from '../actions/chat';
export function* sendMessageSaga(action) {
let result;
try {
result = yield call(sendMessage, action.channel, action.text);
} catch(err) {
yield put({ type: SEND_MESSAGE_FAILURE, err });
}
if ('Error' in result) {
yield put({type: SEND_MESSAGE_FAILURE, err: result.Error});
return
}
yield put({type: SEND_MESSAGE_SUCCESS, data: result.Data });
}
function sendMessage(channel, text) {
return fetch(`http://192.168.0.126:3000/channels/${channel}`, {
method: 'POST',
body: JSON.stringify({
Text: text,
}),
mode: 'cors',
credentials: 'include'
})
.then(res => res.json())
}
export function* fetchMessagesSaga(action) {
let result;
try {
result = yield call(fetchMessages, action.channel);
} catch(err) {
yield put({ type: FETCH_MESSAGES_FAILURE, err });
}
if ('Error' in result) {
yield put({type: FETCH_MESSAGES_FAILURE, err: result.Error});
return
}
yield put({type: FETCH_MESSAGES_SUCCESS, channel: action.channel, data: result.Data });
}
function fetchMessages(channel) {
return fetch(`http://192.168.0.126:3000/channels/${channel}`, {
mode: 'cors',
credentials: 'include'
})
.then(res => res.json())
}
function channelEvents(channel) {
return eventChannel(emitter => {
const eventSource = new EventSource(
`http://192.168.0.126:3000/channels/${channel}/stream`,
{ withCredentials: true }
);
const emit = evt => {
emitter({type: evt.type, data: evt.data});
};
eventSource.addEventListener("joined", emit);
eventSource.addEventListener("left", emit);
eventSource.addEventListener("message", emit);
return () => {
eventSource.removeEventListener("joined", emit)
eventSource.removeEventListener("left", emit)
eventSource.removeEventListener("message", emit)
eventSource.close()
}
}
)
}
export function* streamEventsSaga(action) {
const stream = yield call(channelEvents, action.channel)
while (true) {
let event = yield take(stream)
yield put({ type: 'CHANNEL_EVENT', event: event.type, data: JSON.parse(event.data) });
}
}

View File

@ -1,34 +0,0 @@
import { call, put } from 'redux-saga/effects';
import { LOGIN_FAILURE, LOGIN_SUCCESS } from '../actions/login';
export default function* loginSaga(action) {
let result;
try {
result = yield call(doLogin, action.username, action.password);
} catch(err) {
yield put({ type: LOGIN_FAILURE, err });
}
if ('Error' in result) {
yield put({type: LOGIN_FAILURE, err: result.Error});
return
}
yield put({type: LOGIN_SUCCESS, data: result.Data });
}
function doLogin(username, password) {
return fetch('http://192.168.0.126:3000/login', {
method: 'POST',
body: JSON.stringify({
Username: username,
Password: password
}),
mode: 'cors',
credentials: 'include'
})
.then(res => res.json())
}

View File

@ -1,14 +1,7 @@
import { all, takeLatest } from 'redux-saga/effects';
import loginSaga from './login';
import { LOGIN } from '../actions/login';
import { SEND_MESSAGE, FETCH_MESSAGES, STREAM_EVENTS } from '../actions/chat';
import { sendMessageSaga, fetchMessagesSaga, streamEventsSaga } from './chat';
export default function* rootSaga() {
yield all([
takeLatest(LOGIN, loginSaga),
takeLatest(SEND_MESSAGE, sendMessageSaga),
takeLatest(FETCH_MESSAGES, fetchMessagesSaga),
takeLatest(STREAM_EVENTS, streamEventsSaga)
]);
}