Skip to content
This repository has been archived by the owner on Jan 11, 2020. It is now read-only.

Commit

Permalink
Make responsive for closet
Browse files Browse the repository at this point in the history
fix #89
  • Loading branch information
g-plane committed Nov 6, 2017
1 parent 4d21fb5 commit 594954d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
7 changes: 5 additions & 2 deletions app/Http/Controllers/ClosetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ public function getClosetData(Request $request)
{
$category = $request->input('category', 'skin');
$page = abs($request->input('page', 1));
$per_page = (int) $request->input('perPage', 6);
$q = $request->input('q', null);

$per_page = $per_page > 0 ? $per_page : 6;

$items = collect();

if ($q) {
Expand All @@ -48,11 +51,11 @@ public function getClosetData(Request $request)
}

// pagination
$total_pages = ceil($items->count() / 6);
$total_pages = ceil($items->count() / $per_page);

return response()->json([
'category' => $category,
'items' => $items->forPage($page, 6)->values(),
'items' => $items->forPage($page, $per_page)->values(),
'total_pages' => $total_pages
]);
}
Expand Down
12 changes: 12 additions & 0 deletions resources/assets/src/js/__tests__/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,24 @@ describe('tests for "closet" module', () => {
data: {
category: 'skin',
page: 1,
perPage: 0,
q: 'q'
}
});
expect($('#closet-paginator').attr('last-skin-page')).toBe('1');
});

it('calculate capacity of closet', () => {
document.body.innerHTML = `
<div id="skin-category" style="width: 900px">
<div class="item" style="width: 50px; margin-right: 10.5px"></div>
</div>
`;

const getCapacityOfCloset = require(modulePath).getCapacityOfCloset;
expect(getCapacityOfCloset()).toBe(28);
});

it('rename item', async () => {
const fetch = jest.fn()
.mockReturnValue(Promise.resolve({ errno: 0, msg: 'success' }));
Expand Down
14 changes: 14 additions & 0 deletions resources/assets/src/js/user/closet.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ function reloadCloset(category, page, search) {
data: {
category: category,
page: page,
perPage: getCapacityOfCloset(),
q: search
}
}).then(({ items, category, total_pages }) => {
Expand All @@ -157,6 +158,18 @@ function reloadCloset(category, page, search) {
}).catch(showAjaxError);
}

/**
* Get the capacity of closet.
*
* @returns {number}
*/
function getCapacityOfCloset() {
return ~~(
$('#skin-category').width() /
($('.item').width() + parseFloat($('.item').css('margin-right')))
) * 2;
}

function renameClosetItem(tid, oldName) {
let newTextureName = '';

Expand Down Expand Up @@ -243,6 +256,7 @@ if (typeof require !== 'undefined' && typeof module !== 'undefined') {
setAsAvatar,
renderCloset,
reloadCloset,
getCapacityOfCloset,
renameClosetItem,
removeFromCloset,
};
Expand Down
13 changes: 12 additions & 1 deletion tests/ClosetControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testIndex()

public function testGetClosetData()
{
$textures = factory(Texture::class, 5)->create();
$textures = factory(Texture::class, 10)->create();
$closet = new Closet($this->user->uid);
$textures->each(function ($texture) use ($closet) {
$closet->add($texture->tid, $texture->name);
Expand All @@ -45,6 +45,17 @@ public function testGetClosetData()
'items' => [['tid', 'name', 'type', 'add_at']]
]);

// Responsive
$result = json_decode($this->call('get', '/user/closet-data?perPage=0')
->getContent(), true);
$this->assertEquals(6, count($result['items']));
$result = json_decode($this->call('get', '/user/closet-data?perPage=8')
->getContent(), true);
$this->assertEquals(8, count($result['items']));
$result = json_decode($this->call('get', '/user/closet-data?perPage=8&page=2')
->getContent(), true);
$this->assertEquals(2, count($result['items']));

// Get capes
$cape = factory(Texture::class, 'cape')->create();
$closet->add($cape->tid, 'custom_name');
Expand Down

0 comments on commit 594954d

Please sign in to comment.