Skip to content

Commit

Permalink
Add scheduled sessions to vuex store
Browse files Browse the repository at this point in the history
Can create scheduled sessions and get scheduled sessions for a
recipient.

API helper now takes query params as a separate argument instead of
including it in the URL path, as it was causing issues with tests:
ctimmerm/axios-mock-adapter#138
  • Loading branch information
Dan Tong committed Apr 26, 2019
1 parent 2794a24 commit a7e6a00
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 6 deletions.
4 changes: 2 additions & 2 deletions assets/src/store/api-helper.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import axios from "axios";
const basePath = "/api/v1";

const get = async (store, { endpoint, mutation }) => {
const get = async (store, { endpoint, mutation, params }) => {
store.commit(mutation.PENDING);

try {
const response = await axios.get(basePath + endpoint);
const response = await axios.get(basePath + endpoint, { params });
store.commit(mutation.SUCCESS, response.data);
} catch (error) {
store.commit(mutation.FAILURE, error);
Expand Down
5 changes: 3 additions & 2 deletions assets/src/store/modules/bases/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ export default {

async getSessionSlots(store, baseId) {
await apiHelper.get(store, {
endpoint: `/sessions/slots?base_id=${baseId}`,
mutation: types.API_GET_SESSION_SLOTS
endpoint: "/sessions/slots",
mutation: types.API_GET_SESSION_SLOTS,
params: { base_id: baseId }
});
}
};
16 changes: 16 additions & 0 deletions assets/src/store/modules/recipients/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ export default {
});
},

async createScheduledSession(store, session) {
await apiHelper.post(store, {
endpoint: "/sessions/scheduled",
body: { session },
mutation: types.API_CREATE_SCHEDULED_SESSION
});
},

async getScheduledSessions(store, recipientId) {
await apiHelper.get(store, {
endpoint: "/sessions/scheduled",
mutation: types.API_GET_SCHEDULED_SESSIONS,
params: { recipient_id: recipientId }
});
},

async getRecipients(store) {
await apiHelper.get(store, {
endpoint: "/recipients",
Expand Down
29 changes: 29 additions & 0 deletions assets/src/store/modules/recipients/mutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,35 @@ export default {
state.errors = [payload];
},

[types.API_GET_SCHEDULED_SESSIONS.PENDING](state) {
state.loading = true;
},

[types.API_GET_SCHEDULED_SESSIONS.SUCCESS](state, payload) {
state.loading = false;
state.scheduledSessions = payload;
},

[types.API_GET_SCHEDULED_SESSIONS.FAILURE](state, payload) {
state.loading = false;
state.errors = [payload];
},

[types.API_CREATE_SCHEDULED_SESSION.PENDING](state) {
state.loading = true;
state.errors = null;
},

[types.API_CREATE_SCHEDULED_SESSION.SUCCESS](state, payload) {
state.loading = false;
state.scheduledSessions.push(payload);
},

[types.API_CREATE_SCHEDULED_SESSION.FAILURE](state, payload) {
state.loading = false;
state.errors = payload;
},

[types.API_CREATE_RECIPIENT.PENDING](state) {
state.loading = true;
state.errors = null;
Expand Down
3 changes: 2 additions & 1 deletion assets/src/store/modules/recipients/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ export default {
mobile: "",
landline: ""
},
sortingSessions: [],
base: {
name: "",
id: null
}
},

scheduledSessions: [],

filters: {
base_id: 0,
name: "",
Expand Down
6 changes: 6 additions & 0 deletions assets/src/store/mutation-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ const createAPIMutation = type => ({
PENDING: `${type}_PENDING`
});
export const API_CREATE_RECIPIENT = createAPIMutation("CREATE_RECIPIENT");
export const API_CREATE_SCHEDULED_SESSION = createAPIMutation(
"CREATE_SCHEDULED_SESSION"
);
export const API_GET_RECIPIENT = createAPIMutation("GET_RECIPIENT");
export const API_GET_RECIPIENTS = createAPIMutation("GET_RECIPIENTS");
export const API_GET_SCHEDULED_SESSIONS = createAPIMutation(
"GET_SCHEDULED_SESSIONS"
);

export const API_GET_BASES = createAPIMutation("GET_BASES");
export const API_GET_FOOD_CATEGORIES = createAPIMutation("GET_FOOD_CATEGORIES");
Expand Down
2 changes: 1 addition & 1 deletion assets/tests/unit/store/modules/bases/actions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe("actions", () => {
{ day: "Wednesday", time: "12:00 PM", id: 2 }
];
mock
.onGet(`/api/v1/sessions/slots?base_id=${baseId}`)
.onGet("/api/v1/sessions/slots", { params: { base_id: baseId } })
.reply(200, sessionSlots);
await actions.getSessionSlots({ commit, state }, baseId);
await expect(commit).toBeCalledWith(types.API_GET_SESSION_SLOTS.PENDING);
Expand Down
49 changes: 49 additions & 0 deletions assets/tests/unit/store/modules/recipients/actions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,32 @@ describe("actions", () => {
);
});
});
describe("createScheduledSession", () => {
it("calls SUCCESS mutation with list of recipients on API success", async () => {
const session = {
session: {
session_slot_id: 1,
recipient_id: 1,
allocations_attributes: [
{
food_category_id: 1,
quantity: null
}
]
}
};
const createdSession = { ...session, id: 1 };
mock.onPost("/api/v1/sessions/scheduled").reply(201, createdSession);
await actions.createScheduledSession({ commit, state }, session);
await expect(commit).toBeCalledWith(
types.API_CREATE_SCHEDULED_SESSION.PENDING
);
await expect(commit).toBeCalledWith(
types.API_CREATE_SCHEDULED_SESSION.SUCCESS,
createdSession
);
});
});

describe("getRecipients", () => {
it("calls SUCCESS mutation with list of recipients on API success", async () => {
Expand All @@ -37,6 +63,29 @@ describe("actions", () => {
});
});

describe("getScheduledSessions", () => {
it("calls SUCCESS mutation with list of recipients on API success", async () => {
const recipient_id = 1;
const sessions = [
{ id: 1, recipient_id: recipient_id, session_slot_id: 1 },
{ id: 2, recipient_id: recipient_id, session_slot_id: 2 }
];
mock
.onGet("/api/v1/sessions/scheduled", {
params: { recipient_id: recipient_id }
})
.reply(200, sessions);
await actions.getScheduledSessions({ commit, state }, recipient_id);
await expect(commit).toBeCalledWith(
types.API_GET_SCHEDULED_SESSIONS.PENDING
);
await expect(commit).toBeCalledWith(
types.API_GET_SCHEDULED_SESSIONS.SUCCESS,
sessions
);
});
});

describe("resetFilters", () => {
it("resets name and status filter", async () => {
const statuses = [...state.filters.status];
Expand Down
22 changes: 22 additions & 0 deletions assets/tests/unit/store/modules/recipients/mutations.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,28 @@ describe("mutations", () => {
});
});

describe("API_CREATE_SCHEDULED_SESSION", () => {
it("PENDING", () => {
mutations[types.API_CREATE_SCHEDULED_SESSION.PENDING](state);
expect(state.loading).toBeTruthy;
});

it("SUCCESS", () => {
const payload = { session_slot_id: 1, recipient_id: 1, id: 1 };
mutations[types.API_CREATE_SCHEDULED_SESSION.SUCCESS](state, payload);

expect(state.scheduledSessions).toEqual([payload]);
expect(state.loading).toBeFalsy;
});

it("FAILURE", () => {
const payload = [{ recipient: ["failed to create"] }];
mutations[types.API_CREATE_SCHEDULED_SESSION.FAILURE](state, payload);
expect(state.loading).toBeFalsy;
expect(state.errors).toEqual(payload);
});
});

describe("API_GET_RECIPIENTS", () => {
it("PENDING", () => {
mutations[types.API_GET_RECIPIENTS.PENDING](state);
Expand Down

0 comments on commit a7e6a00

Please sign in to comment.